SLIDES ADAPTED FROM ALEX MARIAKAKIS
WITH MATERIAL KELLEN DONOHUE, DAVID MAILHOT, AND DAN GROSSMAN
Sec$on6:Dijkstra’sAlgorithm
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
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
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!
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!
Goal: Smallest cost? Or fewest edges?
BFS vs. Dijkstra’s
BFSdoesn’tworkbecausepathwithminimalcost≠pathwithfewestedges
Also,Dijkstra’sworksiftheweightsarenon-negaIve
WhathappensifthereisanegaIveedge?◦ MinimizecostbyrepeaIngthecycleforever
500
100100 100
1005
-10
11
Dijkstra’s Algorithm NamedaReritsinventorEdsgerDijkstra(1930-2002)◦ Trulyoneofthe“founders”ofcomputerscience;◦ ThisisjustoneofhismanycontribuIons
Theidea:reminiscentofBFS,butadaptedtohandleweights◦ Growthesetofnodeswhoseshortestdistancehasbeencomputed◦ Nodesnotinthesetwillhavea“bestdistancesofar”◦ APRIORITYQUEUEwillturnouttobeusefulforefficiency–We’llcoverthislaterintheslidedeck
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
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
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 ∞
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 ∞
�
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 ∞
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 ∞
�
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 ∞
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 ∞
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 ∞
�
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
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
�
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
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
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
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
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
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 ∞
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
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
Can We Do Be[er? IncreaseefficiencybyconsideringlowestcostunknownvertexwithsorInginsteadoflookingatallverIces
PriorityQueueislikeaqueue,butreturnselementsbylowestvalueinsteadofFIFO
Priority Queue IncreaseefficiencybyconsideringlowestcostunknownvertexwithsorInginsteadoflookingatallverIces
PriorityQueueislikeaqueue,butreturnselementsbylowestvalueinsteadofFIFO
Twowaystoimplement:1. Comparable
a) classNodeimplementsComparable<Node>b) publicintcompareTo(other)
2. Comparatora) classNodeComparatorextendsComparator<Node>b) newPriorityQueue(newNodeComparator())
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
Homework 7 Modifyyourgraphtousegenerics◦ WillhavetoupdateHW#5andHW#6tests
ImplementDijkstra’salgorithm◦ Searchalgorithmthataccountsforedgeweights◦ Note:ThisshouldnotchangeyourimplementaIonofGraph.Dijkstra’sisperformedonaGraph,notwithinaGraph.
Homework 7 Themorewell-connectedtwocharactersare,thelowertheweightandthemorelikelythatapathistakenthroughthem◦ Theweightofanedgeisequaltotheinverseofhowmanycomicbooksthetwocharactersshare
◦ Ex:IfAmazingAmoebaandZanyZebraappearedin5comicbookstogether,theweightoftheiredgewouldbe1/5
Hw7 Important Notes!!! DONOTaccessdatafromhw6/src/data◦ Copyoverdatafilesfromhw6/src/dataintohw7/src/data,andaccessdatainhw7fromthereinstead
◦ Remembertodothis!Ortestswillfailwhengrading.
DONOTmodifyScriptFileTests.java
Hw7 Test script Command Notes HW7LoadGraphcommandisslightlydifferentfromHW6◦ ARergraphisloaded,thereshouldbeatmostonedirectededgefromonenodetoanother,withtheedgelabelbeingthemulIplicaIveinverseofthenumberofbooksshared
◦ Example:If8booksaresharedbetweentwonodes,theedgelabelwillbe1/8
◦ SincetheedgerelaIonshipissymmetric,therewouldbeanotheredgegoingtheotherdirecIonwiththesameedgelabel
Graph AcYvity ListtheCharactersset,theBooks->Charactersmap,anddrawthegraphusingthesecharactersand“books”.
HarryHP1
HarryHP2 HarryHP3
HarryHP4
QuirrelHP1
ScabbersHP1 ScabbersHP2
VoldemortHP4
VoldemortSharedAHead
QuirrelSharedAHead
Graph AcYvity Answers Characters Harry,Quirrel,Scabbers
Books->Characters
HP1->Harry,Quirrel,Scabbers
HP2->Harry,Scabbers, HP3->Harry
HP4->Harry,Voldemort
SharedAHead->Voldemort,Quirrel
Graph AcYvity Answers Vol
Har Qui
Sca
1 1
1
1/2 1