lecture 21
description
Transcript of lecture 21
![Page 1: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/1.jpg)
David Luebke 1 04/09/23
CS 332: Algorithms
S-S Shortest Path: Dijkstra’s Algorithm
Disjoint-Set Union
Amortized Analysis
![Page 2: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/2.jpg)
David Luebke 2 04/09/23
Review: Single-Source Shortest Path
● Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges■ E.g., a road map: what is the shortest path from
Chapel Hill to Charlottesville?
![Page 3: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/3.jpg)
David Luebke 3 04/09/23
Review: Shortest Path Properties
● Optimal substructure: the shortest path consists of shortest subpaths
● Let (u,v) be the weight of the shortest path from u to v. Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v)
● In graphs with negative weight cycles, some shortest paths will not exist
![Page 4: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/4.jpg)
David Luebke 4 04/09/23
Review: Relaxation
● Key technique: relaxation■ Maintain upper bound d[v] on (s,v):Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}
952
752
Relax
652
652
Relax
![Page 5: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/5.jpg)
David Luebke 5 04/09/23
Review: Bellman-Ford Algorithm
BellmanFord()
for each v V d[v] = ; d[s] = 0;
for i=1 to |V|-1
for each edge (u,v) E Relax(u,v, w(u,v));
for each edge (u,v) E if (d[v] > d[u] + w(u,v))
return “no solution”;
Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
Initialize d[], whichwill converge to shortest-path value
Relaxation: Make |V|-1 passes, relaxing each edge
Test for solution:have we converged yet?Ie, negative cycle?
![Page 6: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/6.jpg)
David Luebke 6 04/09/23
Review: Bellman-Ford Algorithm
BellmanFord()
for each v V d[v] = ; d[s] = 0;
for i=1 to |V|-1
for each edge (u,v) E Relax(u,v, w(u,v));
for each edge (u,v) E if (d[v] > d[u] + w(u,v))
return “no solution”;
Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
What will be the running time?
![Page 7: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/7.jpg)
David Luebke 7 04/09/23
Review: Bellman-Ford
● Running time: O(VE)■ Not so good for large dense graphs■ But a very practical algorithm in many ways
● Note that order in which edges are processed affects how quickly it converges (show example)
![Page 8: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/8.jpg)
David Luebke 8 04/09/23
DAG Shortest Paths
● Problem: finding shortest paths in DAG■ Bellman-Ford takes O(VE) time. ■ How can we do better?■ Idea: use topological sort. How does it work again?
○ If were lucky and processes vertices on each shortest path from left to right, would be done in one pass
○ Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert).
○ Thus: just one pass. What will be the running time?
![Page 9: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/9.jpg)
David Luebke 9 04/09/23
Dijkstra’s Algorithm
● If no negative edge weights, we can beat BF● Similar to breadth-first search
■ Grow a tree gradually, advancing from vertices taken from a queue
● Also similar to Prim’s algorithm for MST■ Use a priority queue keyed on d[v]
![Page 10: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/10.jpg)
David Luebke 10 04/09/23
Dijkstra’s Algorithm
Dijkstra(G)
for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);
S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);RelaxationStepNote: this
is really a call to Q->DecreaseKey()
B
C
DA
10
4 3
2
15
Ex: run the algorithm
![Page 11: lecture 21](https://reader036.fdocuments.in/reader036/viewer/2022082804/5481af1ab4af9f38418b48f1/html5/thumbnails/11.jpg)
David Luebke 11 04/09/23
The End