Download presentation
Presentation is loading. Please wait.
1
Dijkstra’s Shortest Paths CS 312 Lecture 4
2
Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week later than current calendar) Quiz 1 is ready and waiting –Open book –30 min limit –get a copy on line before you start
3
Objectives Follow up –big-O, big-Theta and big-Omega –Prim’s algorithm: outside of class. Understand the shortest paths problem. Understand Dijkstra’s algorithm Analyze Dijkstra’s algorithm
4
Big-Letters
5
THE PROBLEM Given: a directed graph G= N,A each edge has a nonnegative length one designated source node Determine the length of the shortest path from the source to each of the other nodes of the graph. THE SOLUTION A greedy algorithm called Dijkstra’s Algorithm.
6
1 2 34 5 10 100 30 20 50 5 Dijkstra’s Algorithm: Overview Maintain a set S of vertices whose minimum distance from the source is known, a set C of candidate vertices (S C=N, S C= ). Initially, S={s}. (the source) Keep adding vertices to S, choosing from C the node whose distance from the source is least. Eventually, S=N. Special path: a path from ‘s’ with all intermediate nodes in S. Maintain an array D with: If w S, then D[w] is the length of the shortest path to w. If w S, then D[w] is the length of the shortest special path from s to w.
7
Dijkstra’s algorithm L[i,j]: the weight along path from i to j. D[k]: length of the shortest special path from source to k. j in C : shortest path from source to j is not known. k in S : shortest path from source to k is known. Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C {2,3…n} for i 2 to n do D[i] L[1,i] repeat n - 2 times v some element of C minimizing D[v] C C \ {v} for each w in C do D[w] min (D[w], D[v] + L[v,w]) return D Inner loop
8
1 2 34 5 10 100 30 20 50 5 Step Init. v-v- C {2,3,4,5} D [50, 30, 100, 10] Choose node from C minimizing D (node 5)
9
1 2 34 5 10 100 30 20 50 5 Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C
10
1 2 34 5 10 100 30 20 50 5 Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C
11
1 2 34 5 10 100 30 20 50 5 Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C
12
1 2 34 5 10 100 30 20 50 5 Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Choose node from C minimizing D (node 4)
13
1 2 34 5 10 100 30 20 50 5 Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C
14
1 2 34 5 10 100 30 20 50 5 Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] Compute D for another node in C
15
1 2 34 5 10 100 30 20 50 5 Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] Choose node from C minimizing D (node 3)
16
1 2 34 5 10 100 30 20 50 5 Step Init. 1 2 3 v-543v-543 C {2,3,4,5} {2,3,4} {2,3} {2} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] [35, 30, 20, 10] Compute D for another node in C (last one)
17
To find not only the length, but where they pass: add an array P[2..n], where P[v] contains v’s predecessor follow the pointers P backwards from destination to source. Modify the code: initialize P[i] to 1, for I=2,3,…,n replace the contents of the inner for loop by if D[w]>D[v] + L[v,w] thenD[w] D[v]+L[v,w] P[w] v
18
Special Path A path from the source to node x is special if all nodes in the path from the source to x belong to S. 1 2 34 5 10 100 30 20 50 5 S = {4,5} then path 1,5,4 is special and path 1,4,3 is special but path 1,3,2 is not.
19
We prove by mathematical induction that (a) If w 1 and w S, then D[w] is the length of the shortest path to w from the source. (b) If w S, then D[w] is the length of the shortest special path from s to w. Basis: Initially, only s S, so (a) holds (vacuously). (b) is true because the only special path from s is the direct path and D is initialized accordingly. Induction hypothesis: Conditions (a) and (b) hold before a node v is added to S. Induction step for (a): For nodes already in S nothing changes, so (a) is true for them. Before adding v to S we must check that D[v] is the length of the shortest path from s to v. By the induction hypothesis, D[v] is the shortest special path, so we must verify that the shortest path from s to v does not go outside of S.
20
s v x The shortest path. The shortest special path. Suppose the contrary; the shortest path from s is not special, that is it goes outside S, and the first node encountered is x.
21
s v x The shortest path. The shortest special path. Since the path from 1…x is special, D[x] = length of shortest special path from 1 … x by (b). D[x], by induction hypothesis (b)
22
s v x So total distance from 1…x…v > 1…x (since … what?) and D[x] > D[v] since D[v] was chosen first. Therefore, D[x]+length(x…v) > D[v]. D[x], by induction hypothesis on (b)
23
s v x Total distance to v via x > D[x], (lengths are positive) D[x] > D[v], (the algorithm chose v before x)
24
s v x Suppose the contrary; the shortest path from s goes outside S, and the first node encountered is x. D[x], by induction hypothesis on (b) Total distance to v via x > D[x], (lengths are positive) D[x] > D[v], (the algorithm chose v before x) Total distance to v via x > D[v], (transitivity) Contradiction! (The shortest path is longer than the shortest special path ???)
25
Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C {2,3…n} for i 2 to n do D[i] L[1,i] repeat n - 2 times v some element of C minimizing D[v] C C \ {v} for each w in C do D[w] min (D[w], D[v] + L[v,w]) return D Analysis
26
Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C {2,3…n} for i 2 to n do D[i] L[1,i] repeat n - 2 times v some element of C minimizing D[v] C C \ {v} for each w in C do D[w] min (D[w], D[v] + L[v,w]) return D Analysis Therefore:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.