slides adapted from alex...

38

Upload: others

Post on 30-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his
Page 2: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

SLIDES ADAPTED FROM ALEX MARIAKAKIS

WITH MATERIAL KELLEN DONOHUE, DAVID MAILHOT, AND DAN GROSSMAN

Sec$on6:Dijkstra’sAlgorithm

Page 3: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Review: Shortest Paths with BFS

Des$na$on 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

1 1

1

1

Page 4: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Review: Shortest Paths with BFS

Des$na$on 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

1 1

1

1

Page 5: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Shortest Paths with Weights

A

B

C D

E

Des$na$on 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 B 2

100

2

6 2

3

100

Paths are not the same!

Page 6: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Shortest Paths with Weights

A

B

C D

E

Des$na$on 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 B 2

100

2

6 2

3

100

Paths are not the same!

Page 7: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Goal: Smallest cost? Or fewest edges?

Page 8: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

BFS vs. Dijkstra’s

 BFSdoesn’tworkbecausepathwithminimalcost≠pathwithfewestedges

 Also,Dijkstra’sworksiftheweightsarenon-negaIve

 WhathappensifthereisanegaIveedge?◦  MinimizecostbyrepeaIngthecycleforever

500

100100 100

1005

-10

11

Page 9: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Dijkstra’s Algorithm  NamedaReritsinventorEdsgerDijkstra(1930-2002)◦  Trulyoneofthe“founders”ofcomputerscience;◦  ThisisjustoneofhismanycontribuIons

 Theidea:reminiscentofBFS,butadaptedtohandleweights◦  Growthesetofnodeswhoseshortestdistancehasbeencomputed◦  Nodesnotinthesetwillhavea“bestdistancesofar”◦  APRIORITYQUEUEwillturnouttobeusefulforefficiency–We’llcoverthislaterintheslidedeck

Page 10: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Dijkstra’s Algorithm 1.  Foreachnodev,setv.cost = ∞ andv.known = false

2.  Setsource.cost = 0

3.  Whilethereareunknownnodesinthegrapha)  Selecttheunknownnodevwithlowestcostb)  Markvasknownc)  Foreachedge(v,u)withweightw,

c1 = v.cost + w

c2 = u.cost

if(c1 < c2)

u.cost = c1

u.path = v

// cost of best path through v to u

// cost of best path to u previously known

// if the new path through v is better, update

Page 11: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 � � �

2 2 3

1 10 2 3

1 11

7

1 9

2

4 5

OrderAddedtoKnownSet:

Example #1

vertex known? cost path A Y 0 B ∞ C ∞ D ∞ E ∞ F ∞ G ∞ H ∞

Goal: Fully explore the graph

Page 12: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 � �

4

1

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A

3

10

1 11

1

Example #1

vertex known? cost path A Y 0 B ≤ 2 A C ≤ 1 A D ≤ 4 A E ∞ F ∞ G ∞ H ∞

Page 13: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 � �

4

1 �

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C

3

10

1 11

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 14: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 � �

4

1

12

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C

3

10

1 11

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 15: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 �

4

1

12

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B

3

10

1 11

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 16: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 �

4

1

12

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B

3

10

1 11

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 17: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 �

4

1 �

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B,D

12

3

10

1 11

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 18: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4

4

1 �

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B,D,F

12

3

10

1 11

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 19: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1 �

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B,D,F

12

3

10

1 11

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 20: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B,D,F,H

12

3

10

1 11

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 21: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1 8

2 2

1 2 3

7

9 2

4 5

OrderAddedtoKnownSet:A,C,B,D,F,H

12

3

10

1 11

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 22: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1 8

2 2

1 2 3

7

9 2

4 5

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

12

3

10

1 11

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 23: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1 8

2 2

1 2 3

7

9 2

4 5

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

11

3

10

1 11

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 24: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2

1 2 3

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

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

3

10

1 11

1

Example #1

Page 25: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

DC

F H

E

G

0 2 4 7

4

1

11

8

2 2 3

110 23

111

7

19

2

4 5

InterpreYng the Results 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

A B

DC

F H

E

G

2 2 3

13

14

Page 26: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

CD

F

E

G

0 �

2

1 2 5

1 1

1

2 6 5 3

10

OrderAddedtoKnownSet:

Example #2

vertex known? cost path A Y 0 B ∞ C ∞ D ∞ E ∞ F ∞ G ∞

Page 27: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

A B

CD

F

E

G

0 3

4

2

12

6

2

1 2 5

1 1

1

2 6 5 3

10

OrderAddedtoKnownSet: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 28: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 while(not all nodes are known) { b = dequeue

b.known = true for each edge (b,a) in G { if(!a.known) { if(b.cost + weight((b,a)) < a.cost){ a.cost = b.cost + weight((b,a)) a.path = b } } …

Pseudocode A[empt #1

Page 29: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Can We Do Be[er?  IncreaseefficiencybyconsideringlowestcostunknownvertexwithsorInginsteadoflookingatallverIces

 PriorityQueueislikeaqueue,butreturnselementsbylowestvalueinsteadofFIFO

Page 30: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Priority Queue  IncreaseefficiencybyconsideringlowestcostunknownvertexwithsorInginsteadoflookingatallverIces

 PriorityQueueislikeaqueue,butreturnselementsbylowestvalueinsteadofFIFO

 Twowaystoimplement:1.  Comparable

a)  classNodeimplementsComparable<Node>b)  publicintcompareTo(other)

2.  Comparatora)  classNodeComparatorextendsComparator<Node>b)  newPriorityQueue(newNodeComparator())

Page 31: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap with all nodes while(heap is not empty) { b = deleteMin() if (b.known) continue; b.known = true for each edge (b,a) in G { if(!a.known) { add(b.cost + weight((b,a)) ) } …

Pseudocode A[empt #2

Page 32: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Homework 7  Modifyyourgraphtousegenerics◦  WillhavetoupdateHW#5andHW#6tests

 ImplementDijkstra’salgorithm◦  Searchalgorithmthataccountsforedgeweights◦  Note:ThisshouldnotchangeyourimplementaIonofGraph.Dijkstra’sisperformedonaGraph,notwithinaGraph.

Page 33: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Homework 7  Themorewell-connectedtwocharactersare,thelowertheweightandthemorelikelythatapathistakenthroughthem◦  Theweightofanedgeisequaltotheinverseofhowmanycomicbooksthetwocharactersshare

◦  Ex:IfAmazingAmoebaandZanyZebraappearedin5comicbookstogether,theweightoftheiredgewouldbe1/5

Page 34: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Hw7 Important Notes!!!  DONOTaccessdatafromhw6/src/data◦  Copyoverdatafilesfromhw6/src/dataintohw7/src/data,andaccessdatainhw7fromthereinstead

◦  Remembertodothis!Ortestswillfailwhengrading.

 DONOTmodifyScriptFileTests.java

Page 35: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Hw7 Test script Command Notes  HW7LoadGraphcommandisslightlydifferentfromHW6◦  ARergraphisloaded,thereshouldbeatmostonedirectededgefromonenodetoanother,withtheedgelabelbeingthemulIplicaIveinverseofthenumberofbooksshared

◦  Example:If8booksaresharedbetweentwonodes,theedgelabelwillbe1/8

◦  SincetheedgerelaIonshipissymmetric,therewouldbeanotheredgegoingtheotherdirecIonwiththesameedgelabel

Page 36: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Graph AcYvity   ListtheCharactersset,theBooks->Charactersmap,anddrawthegraphusingthesecharactersand“books”.

 HarryHP1

 HarryHP2 HarryHP3

 HarryHP4

 QuirrelHP1

  ScabbersHP1  ScabbersHP2

 VoldemortHP4

 VoldemortSharedAHead

 QuirrelSharedAHead

Page 37: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Graph AcYvity Answers  Characters Harry,Quirrel,Scabbers

 Books->Characters

 HP1->Harry,Quirrel,Scabbers

 HP2->Harry,Scabbers, HP3->Harry

 HP4->Harry,Voldemort

 SharedAHead->Voldemort,Quirrel

Page 38: SLIDES ADAPTED FROM ALEX MARIAKAKIScourses.cs.washington.edu/courses/cse331/16au/sections/sec06.pdf · Truly one of the “founders” of computer science; This is just one of his

Graph AcYvity Answers Vol

Har Qui

Sca

1 1

1

1/2 1