Download presentation
Presentation is loading. Please wait.
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 Dijkstra(w,a,z,L){ L(a)=0 for all vertices x ≠a L(x)=∞ T=set of all vertices while(z є T){ choose v є T with minimum L(v) T=T-{v} for each x є T adjacent to v L(x)=min{L(x),L(v)+w(v,x)} }
4
Example 8.4.2 b 2 c 1 2 4 2 3 a d e z 4 3 7 1 6 f g 5 Find L(z)
5
1.L(a)=0 2.L(x)=∞,x≠a 3.T={a,b,c,d,e,f,g,z}
Initialization ∞ ∞ b 2 c 1 2 4 2 3 ∞ ∞ a d e z ∞ 4 3 7 1 6 f g 5 ∞ ∞ 1.L(a)=0 2.L(x)=∞,x≠a 3.T={a,b,c,d,e,f,g,z}
6
1.L(a)=min{L(x),xєT} 2.T={b,c,d,e,f,g,z} 3.L(b)=2,L(f)=1
Iteration 1 ∞ 2 ∞ b 2 c 1 2 4 2 3 ∞ ∞ a d e z ∞ 4 3 7 1 6 f g 5 ∞ ∞ 1 1.L(a)=min{L(x),xєT} 2.T={b,c,d,e,f,g,z} 3.L(b)=2,L(f)=1
7
1.L(f)=min{L(x),xєT} 2.T={b,c,d,e,g,z} 3.L(d)=4,L(g)=6
Iteration 2 2 ∞ b 2 c 1 2 4 2 3 4 ∞ ∞ a d e z ∞ 4 3 7 1 6 f g 5 ∞ 1 6 1.L(f)=min{L(x),xєT} 2.T={b,c,d,e,g,z} 3.L(d)=4,L(g)=6
8
1.L(b)=min{L(x),xєT} 2.T={c,d,e,g,z} 3.L(c)=4,L(e)=6,L(d)=4
Iteration 3 2 4 ∞ b 2 c 1 2 4 2 3 4 6 ∞ a d e z ∞ 4 3 7 1 6 f g 5 6 1 1.L(b)=min{L(x),xєT} 2.T={c,d,e,g,z} 3.L(c)=4,L(e)=6,L(d)=4
9
Iteration 4 1.L(c)=L(d)=min{L(x),xєT},begin with c first 2.T={d,e,g,z}
6 4 6 a d e z 5 ∞ 4 3 7 1 6 f g 5 6 1 1.L(c)=L(d)=min{L(x),xєT},begin with c first 2.T={d,e,g,z} 3.L(e)=6,L(z)=5 …… We can continue, but L(z) will not change in this example.
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 ith 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). a u x w … P
11
Example 2 b 3 c 2 2 a 1 z 1 2 d e 1 Find L(z)
12
1.L(a)=0 2.L(x)=∞,x≠a 3.T={a,b,c,d,e,z} Initialization ∞ ∞ b 3 c 2 2 1
∞ 1 2 d e 1 ∞ ∞ 1.L(a)=0 2.L(x)=∞,x≠a 3.T={a,b,c,d,e,z}
13
1.L(a)=min{L(x),xєT} 2.T={b,c,d,e,z} 3.L(b)=2,L(d)=1
Iteration 1 ∞ a,2 ∞ b 3 c 2 2 1 a z ∞ 1 2 d e 1 ∞ ∞ a,1 1.L(a)=min{L(x),xєT} 2.T={b,c,d,e,z} 3.L(b)=2,L(d)=1
14
1.L(d)=min{L(x),xєT} 2.T={b,c,e,z} 3.L(e)=2
Iteration 2 a,2 ∞ b 3 c 2 2 1 a z ∞ 1 2 d e 1 ∞ a,1 d,2 1.L(d)=min{L(x),xєT} 2.T={b,c,e,z} 3.L(e)=2
15
1.L(b)=min{L(x),xєT},(a,2)<(d,2)
Iteration 3 a,2 b,5 ∞ b 3 c 2 2 1 a z ∞ 1 2 d e 1 d,2 a,1 1.L(b)=min{L(x),xєT},(a,2)<(d,2) 2.T={c,e,z} 3.L(c)=5,L(e)=2
16
1.L(e)=min{L(x),xєT} 2.T={c,z} 3.L(z)=4
Iteration 4 a,2 b,5 b 3 c 2 2 1 a z ∞ e,4 1 2 d e 1 d,2 a,1 1.L(e)=min{L(x),xєT} 2.T={c,z} 3.L(z)=4
17
1.L(z)=min{L(x),xєT} 2.T={c} 3.L(c)=5.Stop.
Iteration 5 a,2 b,5 b 3 c 2 2 1 a z e,4 1 2 d e 1 d,2 a,1 1.L(z)=min{L(x),xєT} 2.T={c} 3.L(c)=5.Stop.
18
Theorem 8.4.5 For input consisting of an n-vertex, simple, connected, weighted graph, Dijkstra’s algorithm has worst-case run time Ѳ(n2). Proof: The while loop will take Ѳ(n2) worst-case running time.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.