Presentation is loading. Please wait.

Presentation is loading. Please wait.

Owen Astrachan Jeff Forbes October 19, 2017

Similar presentations


Presentation on theme: "Owen Astrachan Jeff Forbes October 19, 2017"— Presentation transcript:

1 Owen Astrachan Jeff Forbes October 19, 2017
CompSci 201, Linked Lists Owen Astrachan Jeff Forbes October 19, 2017 10/19/17 Compsci 201, Fall 2017, Linked

2 N is for … new Allocating memory from the heap next
Moving through elements in a list Network The power of Metcalfe’s Law 10/18/17 Compsci 201, Fall 2017, Linked

3 Plan for the Week APIs and Efficiency Linear structures
Linked lists explained, uncovered, explored How does java.util.LinkedList work from both implementation and performance perspective How are linked lists used to implement other data structures What are tradeoffs in using linked lists Code: 10/19/17 CompSci 201, Fall 2017, Linked

4 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());

5 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!

6 Queue

7 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()); comp sci is great! comp sci is great!

8 Stacks & Queues Reasoning about stacks & queues WOTO:
Useful abstraction How to implement? 10/19/17 CompSci 201, Fall 2017, Linked

9 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

10 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?

11 What's the Difference Here?
How does find-a-track work? Fast forward?

12 Analyze Data Structures
public double removeFirst(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 = new LinkedList<String>(); List<String> array = new ArrayList<String>(); double ltime = splicer.removeFirst(splicer.create(linked,100000)); double atime = splicer.removeFirst(splicer.create(array,100000)); Time taken to remove the first element?

13 Remove First Why are timings good? Why are timings bad? 10 0.0036
Size 103 link array 10 0.0036 0.0102 20 0.0016 0.0375 30 0.0012 0.0756 40 0.0003 0.2228 50 0.235 60 0.0004 0.2945 70 0.0005 0.3975 80 0.0007 0.5456 90 0.0006 0.7091 100 0.8827

14 Remove Middle Index What operations could be expensive here?
public double removeMiddleIndex(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)

15 Remove Middle size link array 10 0.0635 0.0057 20 0.2644 0.0131 30 0.4808 0.0345 40 0.8524 0.0531 50 1.4025 0.0844 60 1.8418 0.1245 70 2.9064 0.1777 80 3.7237 0.2224 90 4.6833 0.3102 100 7.8717 0.3824

16 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

17 ArrayList and LinkedList as ADTs
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

18 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

19 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

20 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) + … = O(?)

21 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.

22 Concrete Implementation: Linked List

23 Pointers, References, Structures
Study LinkedList and linked lists from basics Useful in understanding Java implementations Useful to understand C, C++ Useful in understanding trees Required in other courses, interviews, etc. Low-level abstraction, high-order abstraction How can you implement structures that allow arbitrary splicing in the middle?

24 Goldilocks and the Hashtable
A hashtable is a collection of buckets Find the right bucket and search it Bucket organization? Array, linked list, search tree

25 Structuring Data: The inside story
How does a HashSet work? SimpleHashStringSet, almost the same as HashMap What happens with add(key) in a HashSet? What happens with contains(key)? What happens with remove(key)? In diagram below, what's in each cell of myTable? ArrayList: advantages? Disadvantages?

26 Set Implementations SetDriver.java
Size Unique Array Util.hash HashArray HashLink Melville 14353 4103 0.43 0.15 0.216 0.104 hawthorne 85753 13542 1.84 0.21 0.288 0.188 kjv10 823135 32674 14.77 0.71 0.558 0.584 Array: search entire array for each add Class java.util.HashSet HashArray: buckets are ArrayList objects HashLink: buckets are low-level linked lists

27 Set Implementations, SetStress.java
Array Util.hash HashArray HashLink 10,000 0.857 0.037 0.023 0.020 20,000 3.384 0.015 0.014 0.010 30,000 6.884 0.024 0.016 40,000 14.833 0.012 0.030 0.019 Can we run without edit/recompile/run cycle? Benefits? Drawbacks? Why Java interfaces are a good idea for allowing different concrete implementations

28 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 Node{ Node p = new Node(); String info; p.info = "hello"; Node next; p.next = null; } p

29 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?

30 Adding to End (iterative)
void addAtEnd(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? How can we do this recursively?

31 Adding to End (recursive)
public Node addAtEnd(Node head, String value) { if (head == null) return new Node(value, null); head.next = addAtEnd(head.next, value); return head; } What is the base case?

32 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 list A list = new Node(word,list); B Node(String s, Node link) { info = s; next = link;} rhs of operator = completely evaluated before assignment

33 Standard list processing (iterative)
Visit all nodes once, e.g., count them or process them public int size(Node 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?

34 Removing Node from list, "cat"
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; list "dog" "cat" "pig"

35 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!!!

36 Link Questions http://bit.ly/201-f17-1018-2
Why is the parameter in remove method Object and not String?


Download ppt "Owen Astrachan Jeff Forbes October 19, 2017"

Similar presentations


Ads by Google