Download presentation
Presentation is loading. Please wait.
Published byAleesha Douglas Modified over 9 years ago
1
A Shortest Path Algorithm
2
Motivation Given a connected, positive weighted graph Find the length of a shortest path from vertex a to vertex z.
3
Dijkstra’s Shortest Path Algorithm Input: A connected, positive weighted graph,vertices a and z Output: L(z), the length of a shortest path from a to z 1.Dijkstra(w,a,z,L){ 2. L(a)=0 3. for all vertices x ≠a 4. L(x)=∞ 5. T=set of all vertices 6. while(z є T){ 7. choose v є T with minimum L(v) 8. T=T-{v} 9. for each x є T adjacent to v 10. L(x)=min{L(x),L(v)+w(v,x)} 11. } 12.}
4
Example 8.4.2 Find L(z) b ze f d c a 2 2 1 5 4 3 4 1 3 7 2 g 6
5
Initialization b ze f d c a 2 2 1 5 4 3 4 1 3 7 2 g 6 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0
6
Iteration 1 b ze f d c a 2 2 1 5 4 3 4 1 3 7 2 g 6 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 2 1
7
Iteration 2 b ze f d c a 2 2 1 5 4 3 4 1 3 7 2 g 6 ∞ ∞ ∞ ∞ ∞ 0 2 1 4 6
8
Iteration 3 b ze f d c a 2 2 1 5 4 3 4 1 3 7 2 g 6 4 ∞ ∞ 6 ∞ 0 2 1 4 6
9
Iteration 4 b ze f d c a 2 2 1 5 4 3 4 1 6 7 2 g 6 4 4 6 6 ∞ 0 2 1 5
10
Proof of Dijkstra’s Algorithm Basic Step(i=1): we set L(a)=0, and L(a) is sure the length of a shortest path from a to a. Inductive step: For an arbitrary step i Suppose for step k<i, L(v) is the length of a shortest path from a to v. Next, suppose that at the it step we choose v in T with minimum L(v). We will seek a contradiction that if there is a w whose length is less than L(v) then w is not in T. By way of contradiction, suppose there is a w with L(w)<L(v), wєT. Then, let P be the shortest path from a to w, and let x be the vertex nearest to a on P that is in T and let u be x’s predecessor. The node u must not be in T (because x was the nearest node to a that was in T). By assumption, L(u) was the length of the shortest path from a to u. Then, L(x) ≤ L(u)+w(u,x) ≤ length of P < L(v). This is a contradiction. So w is not in T. According to our assumption, every path from a to v has length at least L(v). au x w …… P
11
Example 2 b z d c a 2 3 1 1 1 2 e 2 Find L(z)
12
Initialization b z d c a 2 3 1 1 2 e 2 ∞ ∞ ∞ ∞ ∞ 0 1
13
Iteration 1 b z d c a 2 3 1 1 2 e 2 ∞ ∞ ∞ ∞ ∞ 0 a,2 a,1 1
14
Iteration 2 b z d c a 2 3 1 1 2 e 2 ∞ ∞ ∞ 0 a,2 a,1 d,2 1
15
Iteration 3 b z d c a 2 3 1 1 2 e 2 ∞ d,2 ∞ 0 a,2 a,1 b,5 1
16
Iteration 4 b z d c a 2 3 1 1 2 e 2 d,2 ∞ 0 a,2 a,1 b,5 1 e,4
17
Iteration 5 b z d c a 2 3 1 1 2 e 2 d,2 0 a,2 a,1 b,5 1 e,4
18
Theorem 8.4.5 For input consisting of an n-vertex, simple, connected, weighted graph, Dijkstra’s algorithm has worst-case run time Ѳ (n 2 ). Proof: The while loop will take Ѳ (n 2 ) worst-case running time.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.