I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018,...

10
10/4/18 1 Compsci 201 Linked Lists + Hashing Midterm Details Owen Astrachan [email protected] October 3, 2018 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 1 I is for … Interface LinkedList implements List Inheritance LinkedList extends AbstractSequentialList 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 2 H is for … Hashing What better way to have a bucket list? HackDuke Oct 13/14, hackduke.org Social good 9/22/17 Compsci 201, Fall 2017, Analysis+Markov 3 PFtWBFB Interfaces and Inheritance In the context of ArrayList and LinkedList In code we use in assignments and class Toward Markov Part 2 Linked lists as implementation technique Why nodes are the basis for interview questions Midterm and performance explained 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 4

Transcript of I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018,...

Page 1: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

1

Compsci 201Linked Lists + Hashing

Midterm DetailsOwen Astrachan [email protected] 3, 2018

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 1

I is for …

• Interface• LinkedList implements List

• Inheritance• LinkedList extends AbstractSequentialList

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

2

H is for …

• Hashing• What better way to have a bucket list?

• HackDuke• Oct 13/14, hackduke.org

• Social good

9/22/17 Compsci 201, Fall 2017, Analysis+Markov

3

PFtWBFB

• Interfaces and Inheritance• In the context of ArrayList and LinkedList• In code we use in assignments and class• Toward Markov Part 2

• Linked lists as implementation technique• Why nodes are the basis for interview questions

• Midterm and performance explained

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

4

Page 2: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

2

Some Catch-up and Review• What is the purpose of x.hashCode() ?• Convert x to a number for hashing• Must make sure it’s valid index, e.g., |% size|

• Multiple objects can have same .hashCode• Use .equals to find that which you seek• If a.equals(b) then a.hashCode() == b.hashCode()

• Converse is NOT true

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 5

DIY Set with ArrayList

• https://github.com/astrachano/diyad-hashing

• Benchmarking with synthetic data

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

6

y = 0.7591x2 + 0.1958x - 1.1903R² = 0.9868

-100

102030405060708090

1000

0

2000

0

3000

0

4000

0

5000

0

6000

0

7000

0

8000

0

9000

0

1000

00

ArraySet

Array Set P oly. (Arra ySe t)

# elts, array, hash10000, 0.732, 0.00920000, 2.273, 0.00330000, 5.371, 0.00340000, 10.701, 0.00450000, 19.573, 0.00660000, 23.968, 0.00770000, 41.555, 0.01180000, 46.163, 0.01190000, 67.684, 0.013100000,73.093, 0.015

Just Say No.. When you can

O(n2)

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

7

SimpleHashSet Constructor

ArrayList<ArrayList<Type>> myElements;

• What is each element in the list myElements?

• What is myElements.get(k).size()

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

8

Page 3: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

3

SimpleHashSet.add()

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

9

ArrayList, Why?

WOTO Reviewed

http://bit.ly/201fall18-sept28-1• What can we learn from responses• What is the big idea with hashing?

• Why are buckets with ArrayLists good?• Easy to implement

• Why are buckets with ArrayLists bad?• Remove is O(n) but have to shift once found• Add is O(1), but 1 + 2 + 4 + 2n = 2x2n-1• Adding 2n elements is O(2n), but …

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 10

Summary of O-notation for ArrayList

• ArrayList allocates array[] storage for k items• When full? Allocate 2*k, copy over, continue• Total storage? 1 + 2 + 4 + … + 2k

• Note: 1 + 2 + 4 + 8 + 16 = 31• In general: 2x2k-1 or 2k+1 – 1

• If N = 2k, then sum is O(N) since 2x2k is 2N• Coefficients do not matter• Amortized analysis: some .add calls? $$$$$

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 11

Luis von Ahn• Duke 2000, Math• Duke Honorary Degree 2017• CEO Duolingo• Macarthur Award, 2006• MIT-Lemelson Prize, 2018

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 12

“It’s amazing how motivating it is to sit with somebody and say, ‘What you’re doing is really important.’ I use that a lot.”

Page 4: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

4

Java Interface Iterable

• Classes can only extend one class: inheritance

• Inherit method implementations• and state, e.g., instance variables, but cannot

access private, only protected or public

• Limiting: single inheritance

• Classes can implement many interfaces

• Must supply all methods from each interface• Iterable interface: use for-each loop

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

13

Existing ArraySet class

• Cannot use for-each loop, Java complains

• Can only iterate over an array or an instance of java.lang.Iterable

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

14

How to implement Iterable

• Must have a method named iterator• Method must return an Iterator object• Interface with .hasNext() and .next()

• This is easy for ArraySet since ArrayList is an Iterable• Simply return its Iterator

• Demo: implements Iterable<Type>

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

15

SimpleHashSet implements Iterable

• Make a new class, don’t change one that works• Must return an Iterator, that’s a class/object• Nested aka inner class• Track: bucket # and index # in the bucket

• Software Engineering and Design• Open/Closed principle for code and classes• Open for extension, closed to modification• https://stackify.com/solid-design-open-closed-principle/

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 16

Page 5: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

5

Details and Iterable

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

17

• What Bucket? ArrayList?

• ArrayList of ArrayList<Type>

• What Index in ArrayList?

• This is an index into the “current” ArrayList

Implementing Iterable/Iterator

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

18

• https://github.com/astrachano/diyad-hashing

• Think of this as read-only in 201 • Extend for inheritance, implement for interface

Extend SimpleHashSet<Type>• What does new class get as a result of extension?• Implementation of several set methods• Access to protected instance variables

• When you call x.method(..) what happens?• Find method in class for x• Find method in super-class for x• Object x can make explicit call on super(..)

even when there is an @Override

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 19

Implement Iterable<Type>

• Must supply iterator() method that returns

• Iterator<Type> object which means …• Has .next() and .hasNext() methods

• We can “mix-in” more than one interface, something to think about in software design courses• Extend once, implement many

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

20

Page 6: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

6

Barbara Liskov

Turing Award Winner in 2008 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.

What is a java.util.List in Java?

• Interface for collection of elements• 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?

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

22

What's the Difference Here?

• How does find-a-track work? Fast forward?

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

23

Review: list.remove(0)

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

24

• What is “faster”? LinkedList or ArrayList

y = -4E-05x + 0.0009

y = 0.0064x2 - 0.0156x + 0.0238R² = 0.9984

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1000

020

000

3000

040

000

5000

060

000

7000

080

000

9000

0

1000

0011

0000

1200

0013

0000

1400

0015

0000

RemoveFirst

l in ke d arra y Line ar (l in ke d) P oly. (a rray )

Page 7: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

7

From array to linked list

• How do we implement ArrayList and LinkedList?• Low-level implementation details• We use an array[] for ArrayList• We use a Node for LinkedList

• Why are we using many nodes and not an array?• Same number of nodes as Strings• Not amortized analysis, but exactly N

• Remove without shifting

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

25

What’s in a Node?

• Some information

• Place to snap another node

• In Java we’ll see • String reference: info• Node reference: next

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

26

Visualizing/Understanding Nodes

• https://github.com/astrachano/classcode201fall18

• LowLevelLinkDemo.java• Note: this not needed below

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

27

Adding New Nodes• To add to the end of a linked list• Need reference to first node• only through first node can we access entire list

• Need reference to last node• To add a new last node

• Often need initialization code• First node anchors list• Must do before loop• Loop will add over and over to end

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 28

Page 8: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

8

Adding nodes to end

• Loops: Initialize, loop, finalize

• Loop invariant: last always references last node• Note initialization and update in the loop

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

29

Visualizing Code

• Using Java Tutor: https://goo.gl/EufQp6

• See first and last: both Node variables

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

30

Adding first node to linked list

• Repeatedly add first element, initially null

• New first node points at ”old” first node• First references/points to new first node• Can use first = new Node(vg[k],first)

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

31

Array Traversal

• Visiting (printing) every value in an array

• Initialize index, print w/index, increment index• Elements of array are adjacent in memory

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

32

Page 9: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

9

List Traversal

• Visiting (printing) every value in an array

• Start with first node, print .info, advance .next• Done when current node is null

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

33

WOTO

http://bit.ly/201fall18-oct3-1

https://goo.gl/YfXsKr for visualization

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 34

LinkHashStringSet

• We’ll look more at this Friday and Beyond

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

35 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

36

Page 10: I is for - Duke University€¦ · •Demo: implements Iterable 10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 15 SimpleHashSetimplements Iterable •Make a

10/4/18

10

Midterm

• Mean 80.4%, Median 82.9%

• Interpreting grades• Historically what this means …• 70% on midterms, on track for B or above (50)

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

37

Historical Trends Midterm I

• Spring 2018: Median 74%, Mean 70%

• Fall 2017: Median 85%, Mean 83%• Fall 2016: Median 81%, Mean 77%

• Final Grades In Spring 2018• 53% A- above, 82% B, 92% B-, 96% C-

• Final Grades In Fall 2017• 66% A- above, 91% B, 94% B-, 97% C-

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

38

Learning Interpretation• Constructors, methods, this, and more (Q2)• Class as a whole at 71%, 9 total points

• What is purpose of .toString and .equals (Q4B)• Class as a whole 74%, .toString(), 2 points

• How does hashing work, performance (Q4D)• One point was a huge stretch, 3/360, 4 points

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing 39

Work Coming

• APTs with low-level linked lists

• Will review examples: Friday

• APT Quiz review• How to develop solutions: Friday

• Markov Part 2• After fall break, see calendar for details

10/3/2018 Compsci 201, Fall 2018, Linked Lists + Hashing

40