a shortest path algorithm. motivation given a connected, positive weighted graph find the length of...
TRANSCRIPT
A Shortest Path AlgorithmA Shortest Path Algorithm
MotivationMotivation
Given a connected, positive weighted graph
Find the length of a shortest path from vertex a to vertex z.
Dijkstra’s Shortest Path AlgorithmDijkstra’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)=03. for all vertices x ≠a4. L(x)=∞5. T=set of all vertices6. while(z є T){7. choose v є T with minimum L(v)8. T=T-{v}9. for each x є T adjacent to v10. L(x)=min{L(x),L(v)+w(v,x)}11. }12. }
Example 8.4.2Example 8.4.2
Find L(z)
b
ze
f
d
c
a
2
2
1
5
43
4 13
7
2
g6
InitializationInitialization
b
ze
f
d
c
a
2
2
1
5
43
4 13
7
2
g6
∞
∞
∞
∞
∞
∞
∞0
Iteration 1Iteration 1
b
ze
f
d
c
a
2
2
1
5
43
4 13
7
2
g6
∞
∞
∞
∞
∞
∞
∞0
2
1
Iteration 2Iteration 2
b
ze
f
d
c
a
2
2
1
5
43
4 13
7
2
g6
∞
∞
∞
∞
∞0
2
1
4
6
Iteration 3Iteration 3
b
ze
f
d
c
a
2
2
1
5
43
4 13
7
2
g6
4
∞
∞
6
∞0
2
1
4
6
Iteration 4Iteration 4
b
ze
f
d
c
a
2
2
1
5
43
4 16
7
2
g6
4
4
6
6
∞0
2
1
5
Proof of Dijkstra’s AlgorithmProof 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).
a ux w… …
P
Example 2Example 2
b
z
d
c
a
2
3
1
1
1
2
e2
Find L(z)
InitializationInitialization
b
z
d
c
a
2
3
1
1
2
e2
∞
∞
∞
∞
∞01
Iteration 1Iteration 1
b
z
d
c
a
2
3
1
1
2
e2
∞
∞
∞
∞
∞0
a,2
a,1
1
Iteration 2Iteration 2
b
z
d
c
a
2
3
1
1
2
e2
∞
∞
∞0
a,2
a,1 d,2
1
Iteration 3Iteration 3
b
z
d
c
a
2
3
1
1
2
e2
∞
d,2
∞0
a,2
a,1
b,5
1
Iteration 4Iteration 4
b
z
d
c
a
2
3
1
1
2
e2
d,2
∞0
a,2
a,1
b,5
1e,4
Iteration 5Iteration 5
b
z
d
c
a
2
3
1
1
2
e2
d,2
0
a,2
a,1
b,5
1e,4
Theorem 8.4.5Theorem 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.