Advertisement
SVXX

Dijkstra's Algorithm Proof of Correctness

Feb 25th, 2014
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. Theorem 24.6 (Correctness of Dijkstra’s algorithm)
  2. Dijkstra’s algorithm, run on a weighted, directed graph G = (V, E) with non-
  3. negative weight function w and source s, terminates with d[u] = δ(s, u) for all
  4. vertices u ∈ V .
  5. Proof
  6. We use the following loop invariant:
  7. At the start of each iteration of the while loop of lines 4–8, d[v] = δ(s, v)
  8. for each vertex v ∈ S.
  9. It suffices to show for each vertex u ∈ V , we have d[u] = δ(s, u) at the time
  10. when u is added to set S. Once we show that d[u] = δ(s, u), we rely on the
  11. upper-bound property to show that the equality holds at all times thereafter.
  12. Initialization: Initially, S = ∅, and so the invariant is trivially true.
  13. Maintenance: We wish to show that in each iteration, d[u] = δ(s, u) for the
  14. vertex added to set S. For the purpose of contradiction, let u be the first vertex
  15. for which d[u] = δ(s, u) when it is added to set S. We shall focus our attention
  16. on the situation at the beginning of the iteration of the while loop in which u
  17. is added to S and derive the contradiction that d[u] = δ(s, u) at that time by
  18. examining a shortest path from s to u. We must have u = s because s is the
  19. first vertex added to set S and d[s] = δ(s, s) = 0 at that time. Because u = s,
  20. we also have that S = ∅ just before u is added to S. There must be some path
  21. from s to u, for otherwise d[u] = δ(s, u) = ∞ by the no-path property, which
  22. would violate our assumption that d[u] = δ(s, u). Because there is at least
  23. one path, there is a shortest path p from s to u. Prior to adding u to S, path p
  24. connects a vertex in S, namely s, to a vertex in V − S, namely u. Let us consider
  25. the first vertex y along p such that y ∈ V − S, and let x ∈ S be y’s predecessor.
  26.  
  27. Thus, path p can be decomposed as s ❀ x → y ❀ u. Here ❀ means following path p1 on LHS and p2 on RHS.
  28. (Either of paths p1 or p2 may have no edges.)
  29.  
  30. We claim that d[y] = δ(s, y) when u is added to S. To prove this claim,
  31. observe that x ∈ S. Then, because u is chosen as the first vertex for which
  32. d[u] = δ(s, u) when it is added to S, we had d[x] = δ(s, x) when x was
  33. added to S. Edge (x, y) was relaxed at that time, so the claim follows from the
  34. convergence property.
  35. We can now obtain a contradiction to prove that d[u] = δ(s, u). Because y oc-
  36. curs before u on a shortest path from s to u and all edge weights are nonnegative
  37. (notably those on path p2 ), we have δ(s, y) ≤ δ(s, u), and thus
  38. d[y] = δ(s, y)
  39. ≤ δ(s, u)
  40. ≤ d[u]
  41. (by the upper-bound property) .
  42. (24.2)
  43. But because both vertices u and y were in V − S when u was chosen in line 5,
  44. we have d[u] ≤ d[y]. Thus, the two inequalities in (24.2) are in fact equalities,
  45. giving
  46. d[y] = δ(s, y) = δ(s, u) = d[u] .
  47. Consequently, d[u] = δ(s, u), which contradicts our choice of u. We conclude
  48. that d[u] = δ(s, u) when u is added to S, and that this equality is maintained at
  49. all times thereafter.
  50. Termination: At termination, Q = ∅ which, along with our earlier invariant that
  51. Q = V − S, implies that S = V . Thus, d[u] = δ(s, u) for all vertices u ∈ V .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement