what do you call 8 hobbits? · 2018-08-20 · named after its inventor edsger dijkstra (1930-2002)...

45
What do you call 8 Hobbits?

Upload: others

Post on 29-Feb-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

What do you call 8 Hobbits?

Page 2: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

What do you call 8 HobBITs?

A HobBYTE!

Page 3: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

SLIDES ADAPTED FROM ALEX MARIAKAKIS WITH MATERIAL KELLEN DONOHUE, DAVID

MAILHOT, AND DAN GROSSMAN

Section 7:Dijkstra’s Algorithm

Page 4: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Agenda• Reminders

• HW 6 due tonight (7/26)• HW 7 due next Thursday (8/2)

• Regression Testing• Subtyping• Dijkstra’s Algorithm• HW 7

Page 5: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Regression testingo It’s important to make sure software still performs well

after changes!

o Regression Testing is the practice of running existing

tests after making changeso Your HW5 tests should still pass after you update your graph for HW6!

o You will be graded on this!

o You may update your HW5 tests

Page 6: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Subtyping: Liskov Substitution Principleo Refer to your worksheet...

o LSP: If B is a true subtype of A, then B is

substitutable for A.o Follows Principle of Least Surprise

o Is it enough to say “every B is an A”?

o This is necessary but not sufficient for B to

be a true subtype of A

Hello

again!

Barbara Liskov

Page 7: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Subtyping: Specifications

o If B is a true subtype of A, then B must have a stronger specification than Ao Each method of B must have a stronger specification

than the corresponding method of A (if any)o Stronger method spec means:

o Requires less (weaker precondition)

o Promises more (stronger postcondition)

o Worksheet time!

Page 8: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Review: Shortest Paths with BFS

Destination Path Cost

A <B,A> 1

B <B> 0

C <B,A,C> 2

D <B,D> 1

E <B,D,E> 2

From Node B

A

B

C D

E

1

1

1

11

1

1

Page 9: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Destination Path Cost

A <B,A> 1

B <B> 0

C <B,A,C> 2

D <B,D> 1

E <B,D,E> 2

From Node B

A

B

C D

E

1

1

1

11

1

1

Review: Shortest Paths with BFS

Page 10: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A

B

C D

E

2

100

2

62

3

100

Shortest Paths with Weights

Page 11: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A

B

C D

E

Destination Path Cost

A <B,A> 2

B <B> 0

C <B,A,C> 5

D <B,A,C,D> 7

E <B,A,C,E> 7

From Node B2

100

2

62

3

100

Paths are not the same!

Shortest Paths with Weights

Page 12: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

BFS vs. Dijkstra’s

BFS doesn’t work because path with minimal cost ≠ path with fewest edges

Also, Dijkstra’s works if the weights are non-negative

What happens if there is a negative edge?◦ Minimize cost by repeating the cycle forever (this is bad)

◦ How could we fix this?

500

100100 100

1005

-10

11

Page 13: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Dijkstra’s AlgorithmNamed after its inventor Edsger Dijkstra (1930-2002)

◦ Truly one of the “founders” of computer science;

◦ This is just one of his many contributions

The idea: reminiscent of BFS, but adapted to handle weights◦ Grow the set of nodes whose shortest distance has been computed

◦ Nodes not in the set will have a “best distance so far”

◦ A PRIORITY QUEUE will turn out to be useful for efficiency – We’ll cover this later in the slide deck

Page 14: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Dijkstra’s Algorithm1. For each node v, set v.cost = ∞ and v.known = false

2. Set source.cost = 0

3. While there are unknown nodes in the grapha) Select the unknown node vwith lowest cost

b) Mark v as known

c) For each edge (v,u) with weight w,

c1 = v.cost + w // cost of best path through v to u

c2 = u.cost // cost of best path to u previously known

if(c1 < c2) // if the new path through v is better,update

u.cost = c1

u.path = v

Page 15: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 2 3

110 23

111

7

1

9

2

4 5

Order Added to Known Set:

Example #1

vertex known? cost path

A Y 0

B ∞

C ∞

D ∞

E ∞

F ∞

G ∞

H ∞

Goal: Fully explore

the graph

Page 16: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B ≤ 2 A

C ≤ 1 A

D ≤ 4 A

E ∞

F ∞

G ∞

H ∞

Page 17: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B ≤ 2 A

C Y 1 A

D ≤ 4 A

E ∞

F ∞

G ∞

H ∞

Page 18: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2

4

1

12

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B ≤ 2 A

C Y 1 A

D ≤ 4 A

E ≤ 12 C

F ∞

G ∞

H ∞

Page 19: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2

4

1

12

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D ≤ 4 A

E ≤ 12 C

F ∞

G ∞

H ∞

Page 20: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4

4

1

12

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D ≤ 4 A

E ≤ 12 C

F ≤ 4 B

G ∞

H ∞

Page 21: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F ≤ 4 B

G ∞

H ∞

Page 22: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F Y 4 B

G ∞

H ∞

Page 23: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F Y 4 B

G ∞

H ≤ 7 F

Page 24: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F, H

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F Y 4 B

G ∞

H Y 7 F

Page 25: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

18

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F, H

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F Y 4 B

G ≤ 8 H

H Y 7 F

Page 26: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

18

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F, H, G

12

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 12 C

F Y 4 B

G Y 8 H

H Y 7 F

Page 27: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

18

2 2

123

7

9

2

4 5

Order Added to Known Set:

A, C, B, D, F, H, G

11

3

10

111

1

Example #1

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E ≤ 11 G

F Y 4 B

G Y 8 H

H Y 7 F

Page 28: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2

123

7

9

2

4

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 F

5

Order Added to Known Set:

A, C, B, D, F, H, G, E

3

10

111

1

Example #1

Page 29: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 F

Page 30: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA

Page 31: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA B

DC

2

14

Page 32: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA B

DC

F2 2

14

Page 33: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA B

DC

F H2 2 3

14

Page 34: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA B

DC

F H

G

2 2 3

114

Page 35: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 2

3

111

7

19

2

4 5

Interpreting the Resultsvertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 FA B

DC

F H

E

G

2 2 3

1

3

14

Page 36: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

CD

F

E

G

0 2

12 5

1

1

1

26

5 3

10

Order Added to Known Set:

Example #2

vertex known? cost path

A Y 0

B ∞

C ∞

D ∞

E ∞

F ∞

G ∞

Page 37: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

A B

CD

F

E

G

0 3

4

2

1

2

6

2

12 5

1

1

1

26

5 3

10

Order Added to Known Set:

A, D, C, E, B, F, G

Example #2

vertex known? cost path

A Y 0

B Y 3 E

C Y 2 A

D Y 1 A

E Y 2 D

F Y 4 C

G Y 6 D

Page 38: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

// pre-condition: start is the node to start at

// initialize things

active = new empty priority queue of paths

from start to a given node

// A path's “priority” in the queue is the total

// cost of that path.

finished = new empty set of nodes

// Holds nodes for which we know the

// minimum-cost path from start.

// We know path start->start has cost 0

Add a path from start to itself to active

Pseudocode

Page 39: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

while active is non-empty:

minPath = active.removeMin()

minDest = destination node in minPath

if minDest is in finished:

continue

for each edge e = ⟨minDest, child⟩:

if child is not in finished:

newPath = minPath + e

add newPath to active

add minDest to finished

Pseudocode (cont.)

Page 40: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Priority QueueIncrease efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices

PriorityQueue is like a queue, but returns elements by lowest value instead of FIFO

Page 41: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Priority QueueIncrease efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices

PriorityQueue is like a queue, but returns elements by lowest value instead of FIFO

Two ways to implement:

1. Comparablea) class Node implements Comparable<Node>

b) public int compareTo(other)

2. Comparatora) class NodeComparator extends Comparator<Node>

b) new PriorityQueue(new NodeComparator())

Page 42: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Homework 7Modify your graph to use generics

◦ Will have to update graph and old tests!

◦ (all of your old tests should still pass)

Implement Dijkstra’s algorithm

◦ Note: This should not change your implementation of Graph. Dijkstra’s is performed on a Graph, not within a Graph.

Page 43: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Homework 7The more well-connected two characters are, the lower the weight and the more likely that a path is taken through them

◦ The weight of an edge is equal to the inverse of how many comic books the two characters share

◦ Ex: If Amazing Amoeba and Zany Zebra appeared in 5 comic books together, the weight of their edge would be 1/5

Page 44: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Hw7 Important Notes!!!DO NOT access data from hw6/src/data

◦ Copy over data files from hw6/src/data into hw7/src/data, and access data in hw7 from there instead

◦ Remember to do this! Or tests will fail when grading.

DO NOT modify ScriptFileTests.java

Page 45: What do you call 8 Hobbits? · 2018-08-20 · Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many

Hw7 Test script Command NotesHW7 LoadGraph command is slightly different from HW6

◦ After graph is loaded, there should be at most one directed edge from one node to another, with the edge label being the multiplicative inverse of the number of books shared

◦ Example: If 8 books are shared between two nodes, the edge label will be 1/8

◦ Since the edge relationship is symmetric, there would be another edge going the other direction with the same edge label