310 likes | 570 Views
CompSci 201, LinkedLists. Nat Huffman for Jeff Forbes February 23, 2018. K is for …. K-means Unsupervised learning can help predict a user’s preferences or identify users with similar properties Key Pairs Public & private key make encryption happen Knuth, Donald
E N D
CompSci 201,LinkedLists Nat Huffman for Jeff Forbes February 23, 2018 CompSci 201, Spring 2018, LinkedLists
K is for … • K-means • Unsupervised learning can help predict a user’s preferences or identify users with similar properties • Key Pairs • Public & private key make encryption happen • Knuth, Donald • Wrote The Art of Computer Programming CompSci 201, Spring 2018, LinkedLists
Stack • Last In First Out (LIFO) • Stack<String> q = new Stack<String>(); • q.push("comp "); • q.push("sci "); • q.push("is "); • q.push("great!"); • while(!q.isEmpty()) • System.out.print(q.pop()); out.print(q.pop()); CompSci 201, Spring 2018, LinkedLists
Stack • Last In First Out (LIFO) • Stack<String> st = new Stack<String>(); • st.push("comp "); • st.push("sci "); • st.push("is "); • st.push("great!"); • while(!st.isEmpty()) • System.out.print(st.pop()); great! is sci comp comp sci is great! CompSci 201, Spring 2018, LinkedLists
Queue CompSci 201, Spring 2018, LinkedLists
Queue • First In First Out (FIFO) • Queue<String> q = new LinkedList<String>(); • q.add("comp "); • q.add("sci "); • q.add("is "); • q.add("great!"); • while(!q.isEmpty()) • System.out.print(q.remove()); http://bit.ly/201-s18-0223-1 comp sci is great! comp sci is great!
Empirical and Analytical Analysis • We can run programs to look at "efficiency" • Depends on machine, environment, programs • We can analyze mathematically to look at efficiency from a different point of view • Depends on being able to employ mathematics • What works in theory may not … • We will work on doing both, leading to a better understanding in many dimensions CompSci 201, Spring 2018, LinkedLists
Array vs. ArrayList • Run the code ArrayVsArrayList.java CompSci 201, Spring 2018, LinkedLists
Amortization: Expanding ArrayLists • Expand capacity of list when add() called • Calling addN times, doubling capacity as needed • Big-Oh of adding n elements? • What if we grow size by one each time? CompSci 201, Spring 2018, LinkedLists
What is a java.util.List in Java? • Collection of elements, operations? • Add, remove, traverse, … • What can a list do to itself? • What can we do to a list? • Why more than one kind of list: Array and Linked? • Useful in different applications • How do we analyze differences? • How do we use them in code? CompSci 201, Spring 2018, LinkedLists
What's the Difference Here? • How does find-a-track work? Fast forward? CompSci 201, Spring 2018, LinkedLists
Analyze Data Structures publicdoubleremoveFirst(List<String> list) { double start = System.nanoTime(); while (list.size() != 1){ list.remove(0); } double end = System.nanoTime(); return (end-start)/1e9; } List<String> linked = newLinkedList<String>(); List<String> array = newArrayList<String>(); doubleltime = splicer.removeFirst(splicer.create(linked,100000)); doubleatime = splicer.removeFirst(splicer.create(array,100000)); • Time taken to remove the first element? CompSci 201, Spring 2018, LinkedLists
Remove First • Why are timings good? • Why are timings bad? CompSci 201, Spring 2018, LinkedLists
Remove Middle Index publicdoubleremoveMiddleIndex(List<String> list) { double start = System.nanoTime(); while (list.size() != 1){ list.remove(list.size()/2); } double end = System.nanoTime(); return (end-start)/1e9; } • What operations could be expensive here? • Explicit: size, remove (only one is expensive) • Implicit: find nth element (may be very costly) CompSci 201, Spring 2018, LinkedLists
Remove Middle CompSci 201, Spring 2018, LinkedLists
ArrayList and LinkedList as ADTs • As an ADT (abstract data type) ArrayList supports • Constant-time or O(1) access to the k-th element • Amortized linear or O(n) storage/time with add • Total storage used in n-element vector is approx. 2n, spread over all accesses/additions (why?) • Add/remove in middle is "expensive" O(n), why? • What's underneath here? How Implemented? • Concrete: array – contiguous memory, must be contiguous to support random access • Element 20 = beginning + 20 x size of a pointer CompSci 201, Spring 2018, LinkedLists
ArrayList and LinkedList as ADTs • LinkedList as ADT • Constant-time or O(1) insertion/deletion anywhere, but… • Linear or O(n) time to find where, sequential search • Linked good for add/remove at front • Splicing into middle, also for 'sparse' structures • What's underneath? How Implemented • Low-level linked lists, self-referential structures • More memory intensive than array: two pointers CompSci 201, Spring 2018, LinkedLists
Remove Middle in Pictures for(int k=middle; … a[k] = alist[k+1] • Find middle element: happens instantly or O(1) • alist(location) + n/2 * sizeof(pointer) since ArrayList holds pointers • Shifting requires moving n/2 pointers, but they are all contiguous in memory: cache performance ArrayList<> alist CompSci 201, Spring 2018, LinkedLists
Remove Middle in Pictures • Find middle element: have to follow pointers between elements • Follow n/2 pointers, but all over memory, so takes time to move from memory->cache->use • Removing middle: instantaneous, no shifting, just re-assign a couple of pointers (back pointers too) • Blue points to Yellow Linked<> llist CompSci 201, Spring 2018, LinkedLists
Analytical Analysis • Since LinkedList is roughly linear • Time to remove first element is constant, but must be done N times • Time for one removal is O(1) --- constant and doesn't depend on N • Time for all removals is O(N) – linear in N, but slope doesn't matter • For ArrayList, removing first element entails … • Shifting N-1 elements, so this is O(N) • All: (N-1) + (N-2) + … + 3 + 2 + 1 = O(?) CompSci 201, Spring 2018, LinkedLists
Barbara Liskov Turing Award Winner in 2008 for For contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing. The advice I give people in general is that you should figure out what you like to do, and what you can do well—and the two are not all that dissimilar, because you don’t typically like doing something if you don’t do it well. … So you should instead watch—be aware of what you’re doing, and what the opportunities are, and step into what seems right, and see where it takes you.
Concrete Implementation: Linked List CompSci 201, Spring 2018, LinkedLists
p Linked lists, CDT and ADT • As an ADT • A list is empty, or contains an element and a list • ( ) or (x, (y, ( ) ) ) • As a picture • CDT (concrete data type) pojo: plain old Java object public class ListNode{ ListNode p = new ListNode(); String value; p.value = "hello"; ListNode next; p.next = null; } CompSci 201, Spring 2018, LinkedLists
What about LinkedList? • Why is access of Nth element linear time? • Keep pointer to last, does that help? • Why is adding to front constant-time O(1)? head A C D B CompSci 201, Spring 2018, LinkedLists
Building linked lists • Add words to the front of a list (draw a picture) • Create new node pointing to list, reset start of list public class Node { String info; Node next; public Node(String s, Node link){ value = s; next = link; } } // … declarations here ListNode list = null; while (scanner.hasNext()) { list = new ListNode(scanner.next(), list); } • What about adding to the end of the list? CompSci 201, Spring 2018, LinkedLists
Adding to End (iterative) publicvoidaddAtEnd(Node head, String value) { if (head == null) head = new Node(value, null); else { Node current = head; while (current.next != null) current = current.next; // what’s true here? current.next = new Node(value, null); } } • What is true after the while loop? • ? CompSci 201, Spring 2018, LinkedLists
list list A B Dissection of add-to-front • List initially empty • First node has first word • Each new word causes new node to be created • New node added to front list = new Node(word,list); Node(String s, Node link) { info = s; next = link;} • rhs of operator = completely evaluated before assignment
Standard list processing (iterative) • Visit all nodes once, e.g., count them or process them public int size(ListNode list){ int count = 0; while (list != null) { count += 1; list = list.next; } return count; } • Should we be concerned that list is null on return? • Can we change the data in the list nodes? • Append “s” to all strings in list? • Complete the ListCount APT! CompSci 201, Spring 2018, LinkedLists
Removing Node from list, "cat" list public Node remove(Node list, String s){ Node start = list; // special case 'cat' first, not shown while (list.next != null) { if (list.next.info.equals(s)) { list.next = list.next.next; break; } list = list.next; } return start; } "dog" "cat" "pig" CompSci 201, Spring 2018, LinkedLists
Linked List idioms • Sometimes check list == null and list.next == null • Short-circuit evaluation in how to do this? • First node can be tricky to process, e.g., remove • Has no node before it. • Solution: put a "header" or "empty" node first • Typically loop: while(list != null) • Can be useful to do while (list.next != null) • Must be sure list != null in writing this!!! http://bit.ly/201-s18-0223-2 CompSci 201, Spring 2018, LinkedLists
Week’s Recap • Calculating Big-Oh from code • java.util.LinkedListvs. java.util.ArrayList • Performance • Implementation • Reflect • What’s clear? What’s still muddy? • http://bit.ly/201-s18-reflect CompSci 201, Spring 2018, LinkedLists