180 likes | 200 Views
Learn about Dijkstra's algorithm for finding the shortest path in weighted graphs, its applications, and correctness proof. Explore the step-by-step process and implementation details.
E N D
Graph Algorithms - 2 Based on slides of Dan Suciu
Single Source, Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s V (called the source), find the shortest (lowest cost) path from s to every vertex in V • Graph may be directed or undirected • Graph may or may not contain cycles • Weights may be all positive or not • What is the problem if graph contains cycles whose total cost is negative?
The Trouble with Negative Weighted Cycles 2 A B 10 -5 1 E 2 C D
Edsger Wybe Dijkstra (1930-2002) • Invented concepts of structured programming, synchronization, weakest precondition, and "semaphores" for controlling computer processes. The Oxford English Dictionary cites his use of the words "vector" and "stack" in a computing context. • Believed programming should be taught without computers • 1972 Turing Award • “Computer Science is no more about computers than astronomy is about telescopes.”
Dijkstra’s Algorithm for Single Source Shortest Path • Classic algorithm for solving shortest path in weighted graphs (with onlypositive edge weights) • Similar to breadth-first search, but uses a priority queue instead of a FIFO queue: • Always select (expand) the vertex that has a lowest-cost path to the start vertex • a kind of “greedy” algorithm • Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges
void BFS(Node startNode) { • Queue S = new Queue; • for v in Nodes do • v.dist = ; • startNode.dist = 0; • S.enqueue(startNode); • while (!S.empty()) { • x = S.dequeue(); • for y in x.children() do • if (x.dist+1<y.dist) { • y.dist = x.dist+1; • S.enqueue(y); • } • } • } • void shortestPath(Node startNode) { • Heap S = new Heap; • for v in Nodes do • v.dist = ; • S.insert(v); • startNode.dist = 0; • S.decreaseKey(startNode); • startNode.previous = null; • while (!S.empty()) { • x = S.deleteMin(); • for y in x.children() do • if (x.dist+c(x,y) < y.dist) { • y.dist = x.dist+c(x,y); S.decreaseKey(y); • y.previous = x; • } • } • }
Dijkstra’s Algorithm:Correctness Proof Let Known be the set of nodes that were extracted from the heap S (through deleteMin) Thus at each moment S and Known are disjoint and their union is V. We show: At any stage, for every node x: (1) x.dist = the cost of the shortest path from source to x going only through nodes in Known (we call such a path a restricted path) (2) if x is in Known, then x.dist = the cost of the shortest path from source to x..
(1) and (2) are proved by induction on stage. Stage = 1 (when source enters Known): (1) and (2) are obvious. Stage k+1: Let u be the node that enters Known at this stage. We prove (2) for node u (since this is the only new node in Known). Suppose (2) is not true for u. i.e. there is a path from source to u shorter than u.dist. That path must go out of Known before reaching u. Let v be the first node out of Known on that path. Then u.dist > cost shorter path ≥ v.dist + shortest-cost(v to u). Since edges have positive cost, it follows that u.dist > v.dist, which is false. Proof of (1): if x is adjacent to u, the property follows from the way x.dist is updated. If x is not adjacent to u, the shortest restricted path from source to x cannot pass through u.
2 2 3 B A F H 1 1 2 1 4 10 9 4 G C 8 2 D 1 E 7 Dijkstra’s Algorithm in Action
Dijkstra’s Algorithm in Action 9 2 2 3 B A F H 1 1 2 1 4 10 9 0 4 G C 8 2 D 1 E 7 8 next
Dijkstra’s Algorithm in Action 9 2 2 next 3 B A F H 1 1 2 1 4 10 9 0 4 G C 9 8 2 15 D 1 E 7 8
Dijkstra’s Algorithm in Action 11 9 2 2 3 B A F H 1 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next
next Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8
Dijkstra’s Algorithm in Action next 11 9 11 2 2 3 B A F H 1 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8
Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next
Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next
Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 Done
Data Structures for Dijkstra’s Algorithm |V| times: Select the unknown node with the lowest cost findMin/deleteMin O(log |V|) |E| times: y’s cost = min(y’s old cost, …) decreaseKey O(log |V|) runtime: O(|E| log |V|)