Owen Astrachan Jeff Forbes October 19, 2017

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures: A Pseudocode Approach with C
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
Data structures Abstract data types Java classes for Data structures and ADTs.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CompSci 100 Prog Design and Analysis II Sept 14, 2010 Prof. Rodger CompSci 100, Fall
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
Week 15 – Monday.  What did we talk about last time?  Tries.
TAFTD (Take Aways for the Day)
Prefix notation in action
Using the Java Collection Libraries COMP 103 # T2
Lecture 6 of Computer Science II
Week 4 - Monday CS221.
4. Linked Lists.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Compsci 201 Linked Lists from A-Z
Building Java Programs
John Hurley Cal State LA
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
We are looking at ….
CS 1114: Implementing Search
Plan for the Week Review Big-Oh
From data to information
JAVA COLLECTIONS LIBRARY
What’s the Difference Here?
TAFTW (Take Aways for the Week)
Hashing Exercises.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Top Ten Words that Almost Rhyme with “Peas”
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
Topic 11 Linked Lists -Joel Spolsky
Building Java Programs
structures and their relationships." - Linus Torvalds
Object Oriented Programming COP3330 / CGS5409
Stacks and Queues.
Building Java Programs
Building Java Programs
Linked Lists.
Programming Abstractions
CS2013 Lecture 4 John Hurley Cal State LA.
Building Java Programs
CSE 143 Lecture 5 References and Linked Nodes
Introduction to Data Structure
Road Map CS Concepts Data Structures Java Language Java Collections
Lists.
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Topic 11 Linked Lists -Joel Spolsky
Lecture 3 – Data collection List ADT
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

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

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

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: https://coursework.cs.duke.edu/201fall17/classwork-linked/ 10/19/17 CompSci 201, Fall 2017, Linked

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

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!

Queue

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!

Stacks & Queues Reasoning about stacks & queues WOTO: http://bit.ly/201-f17-1018-1 Useful abstraction How to implement? 10/19/17 CompSci 201, Fall 2017, Linked

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

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?

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

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? https://coursework.cs.duke.edu/ola/201-linked-spring17/blob/master/src/ListSplicer.java

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

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)

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

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

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

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

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

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(?)

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

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?

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

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?

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 https://coursework.cs.duke.edu/201fall17/classwork-linked/

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 https://coursework.cs.duke.edu/201fall17/classwork-linked/

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

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? http://www.pythontutor.com/java.html

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?

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?

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

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?

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"

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

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