cse 326: data structures dijkstra’s algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • classic...
TRANSCRIPT
![Page 1: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/1.jpg)
CSE 326: Data StructuresDijkstra’s Algorithm
James FogartyAutumn 2007
![Page 2: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/2.jpg)
2
Dijkstra, Edsger Wybe
Legendary figure in computer science; was a professor at University of Texas.
Supported teaching introductory computer courses without computers (pencil and paper programming)
Supposedly wouldn’t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box.
E.W. Dijkstra (1930-2002)
1972 Turning Award Winner, Programming Languages, semaphores, and …
![Page 3: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/3.jpg)
3
Dijkstra’s Algorithm: Idea
Adapt BFS to handle weighted graphs
Two kinds of vertices:– Finished or known
vertices• Shortest distance
hasbeen computed
– Unknown vertices• Have tentative
distance
![Page 4: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/4.jpg)
4
Dijkstra’s Algorithm: Idea
At each step:1) Pick closest unknown
vertex2) Add it to known
vertices3) Update distances
![Page 5: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/5.jpg)
5
Dijkstra’s Algorithm: Pseudocode
Initialize the cost of each node to ∞
Initialize the cost of the source to 0
While there are unknown nodes left in the graphSelect an unknown node b with the lowest costMark b as knownFor each node a adjacent to b
a’s cost = min(a’s old cost, b’s cost + cost of (b, a))a’s prev path node = b
![Page 6: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/6.jpg)
6
Important Features
• Once a vertex is made known, the cost of the shortest path to that node is known
• While a vertex is still not known, another shorter path to it might still be found
• The shortest path itself can found by following the backward pointers stored in node.path
![Page 7: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/7.jpg)
7
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 2 3
110 23
111
7
1
9
2
4
Vertex Visited? Cost Found byA 0B ??C ??D ??E ??F ??G ??H ??
![Page 8: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/8.jpg)
8
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2
1
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAA
B <=2C <=1D <=4E ??F ??G ??H ??
![Page 9: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/9.jpg)
9
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2
1
12
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAAC
B <=2C Y 1D <=4E <=12F ??G ??H ??
![Page 10: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/10.jpg)
10
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4
1
12
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAACB
B Y 2C Y 1D <=4E <=12F <=4G ??H ??
![Page 11: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/11.jpg)
11
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4
1
12
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAACB
B Y 2C Y 1D Y 4E <=12F <=4G ??H ??
![Page 12: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/12.jpg)
12
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4 7
1
12
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAACB
F
B Y 2C Y 1D Y 4E <=12F Y 4G ??H <=7
![Page 13: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/13.jpg)
13
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4 7
1
12
8
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAACBHF
B Y 2C Y 1D Y 4E <=12F Y 4G <=8H Y 7
![Page 14: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/14.jpg)
14
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4 7
1
11
8
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAAGBHF
B Y 2C Y 1D Y 4E <=11F Y 4G Y 8H Y 7
![Page 15: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/15.jpg)
15
Dijkstra’s Algorithm in actionA B
DC
F H
E
G
0 2 4 7
1
11
8
2 2 3
110 23
111
7
1
9
2
4
4
Vertex Visited? Cost Found byA Y 0
AAAGBHF
B Y 2C Y 1D Y 4E Y 11F Y 4G Y 8H Y 7
![Page 16: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/16.jpg)
16
Your turn
v3
v6
v1
v2 v4
v5
v0s
1
2
2
21
1 1
5 3
5
6
10
V Visited? Cost Found by
v0
v1
v2
v3
v4
v5
v6
![Page 17: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/17.jpg)
17
Dijkstra’s Alg: Implementation
Initialize the cost of each node to ∞Initialize the cost of the source to 0While there are unknown nodes left in the graph
Select the unknown node b with the lowest costMark b as knownFor each node a adjacent to b
a’s cost = min(a’s old cost, b’s cost + cost of (b, a))a’s prev path node = b (if we updated a’s cost)
What data structures should we use?
Running time?
![Page 18: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/18.jpg)
18
void Graph::dijkstra(Vertex s){Vertex v,w;
Initialize s.dist = 0 and set dist of all other vertices to infinity
while (there exist unknown vertices, find the one b with the smallest distance)b.known = true;
for each a adjacent to bif (!a.known)
if (b.dist + weight(b,a) < a.dist){a.dist = (b.dist + weight(b,a));a.path = b;
}}
}
Sounds like deleteMin on
a heap…Sounds like adjacency
listsSounds like
decreaseKey
Running time: O(|E| log |V|) – there are |E| edges to examine, and each one causes a heap operation of time O(log |V|)
![Page 19: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/19.jpg)
19
Dijkstra’s Algorithm: Summary
• Classic algorithm for solving SSSP in weighted graphs without negative weights
• A greedy algorithm (irrevocably makes decisions without considering future consequences)
• Intuition for correctness:– shortest path from source vertex to itself is 0– cost of going to adjacent nodes is at most edge weights– cheapest of these must be shortest path to that node– update paths for new node and continue picking cheapest path
![Page 20: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/20.jpg)
20
The Known Cloud
V
Next shortest path from inside the known cloud
W
Better path to V? No!
Correctness: The Cloud Proof
Source
How does Dijkstra’s decide which vertex to add to the Known set next?• If path to V is shortest, path to W must be at least as long
(or else we would have picked W as the next vertex)• So the path through W to V cannot be any shorter!
![Page 21: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/21.jpg)
21
Correctness: Inside the CloudProve by induction on # of nodes in the cloud:
Initial cloud is just the source with shortest path 0
Assume: Everything inside the cloud has the correct shortest path
Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud
When does Dijkstra’s algorithm not work?
![Page 22: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/22.jpg)
22
The Trouble with Negative Weight Cycles
A B
C D
E
2 10
1-5
2
What’s the shortest path from A to E?
Problem?
![Page 23: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/23.jpg)
23
Dijkstra’s vs BFSAt each step:
1) Pick closest unknown vertex2) Add it to finished vertices3) Update distances
Dijkstra’s Algorithm
At each step:1) Pick vertex from queue2) Add it to visited vertices3) Update queue with neighbors
Breadth-first Search
Some Similarities:
![Page 24: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/24.jpg)
24
Single-Source Shortest Path• Given a graph G = (V, E) and a single
distinguished vertex s, find the shortest weighted path from s to every other vertex in G.
All-Pairs Shortest Path:• Find the shortest paths between all
pairs of vertices in the graph.• How?
![Page 25: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/25.jpg)
25
Analysis• Total running time for Dijkstra’s:
O(|V| log |V| + |E| log |V|) (heaps)
What if we want to find the shortest path from each point to ALL other points?
![Page 26: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/26.jpg)
26
Dynamic Programming
Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them).
Simple Example: Calculating the Nth Fibonacci number.
Fib(N) = Fib(N-1) + Fib(N-2)
![Page 27: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/27.jpg)
27
Floyd-Warshallfor (int k = 1; k =< V; k++)for (int i = 1; i =< V; i++)for (int j = 1; j =< V; j++)if ( ( M[i][k]+ M[k][j] ) < M[i][j] )
M[i][j] = M[i][k]+ M[k][j]
Invariant: After the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices
![Page 28: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/28.jpg)
28
a b c d e
a 0 2 - -4 -
b - 0 -2 1 3
c - - 0 - 1
d - - - 0 4
e - - - - 0
b
c
d e
a 2-2
1
31-4
4
Initial state of the matrix:
M[i][j] = min(M[i][j], M[i][k]+ M[k][j])
![Page 29: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm](https://reader036.vdocument.in/reader036/viewer/2022081522/5fb99284e74abc4e4518fcc3/html5/thumbnails/29.jpg)
29
a b c d ea 0 2 0 -4 0b - 0 -2 1 -1c - - 0 - 1d - - - 0 4e - - - - 0
b
c
d e
a 2-2
1
31-4
4
Floyd-Warshall -for All-pairs shortest path
Final Matrix Contents