



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Dijkstra’s algorithm is a simple greedy algorithm for computing the single-source shortest-paths to all other vertices. Dijkstra’s algorithm works on a weighted directed graph G = ( V, E) in which all edge weights are non-negative, i.e., w(u, v) ≥ 0 for each edge (u, v) ∈ E.
Negative edges weights maybe counter to intuition but this can occur in real life problems. However, we will not allow negative cycles because then there is no shortest path. If there is a negative cycle between, say, s and t, then we can always find a shorter path by going around the cycle one more time.
Figure 8.61: Negative weight cycle
The basic structure of Dijkstra’s algorithm is to maintain an estimate of the shortest path from the source vertex to each vertex in the graph. Call this estimate d[v]. Intuitively, d[v] will the length of the shortest path that the algorithm knows of from s to v. This value will always be greater than or equal to the true shortest path distance from s to v. I.e., d[v] ≥ δ(u, v). Initially, we know of no paths, so d[v] = ∞. Moreover, d[s] = 0 for the source vertex.
As the algorithm goes on and sees more and more vertices, it attempts to update d[v] for each vertex in the graph. The process of updating estimates is called relaxation. Here is how relaxation works.
Intuitively, if you can see that your solution is not yet reached an optimum value, then push it a little closer to the optimum. In particular, if you discover a path from s to v shorter than d[v], then you need to update d[v]. This notion is common to many optimization algorithms.
Consider an edge from a vertex u to v whose weight is w(u, v). Suppose that we have already computed current estimates on d[u] and d[v]. We know that there is a path from s to u of weight d[u]. By taking
illustrated in the following figure. We should also remember that the shortest way back to the source
cannot change its value.
path algorithm is to perform the updates in a judicious manner, so the convergence is as fast as possible. Dijkstra’s algorithm is based on the notion of performing repeated relaxations. The algorithm operates
perform
fig:dijlast
8.6.2 Correctness of Dijkstra’s Algorithm
We will prove the correctness of Dijkstr’s algorithm by Induction. We will use the definition that δ( s, v) denotes the minimal distance from s to v.
For the base case
for every u′ ∈ V then d(u) = δ(s, u), and we can transfer u from V to S, after which d(v) = δ(s, v) for every v ∈ S. We do this as a proof by contradiction. Suppose that d(u) > δ(s, u). The shortest path from s to u, p(s, u), must pass through one or more vertices exterior to S. Let x be the last vertex inside S and y be the first vertex outside S on this path to u. Then p(s, u) = p(s, x) ∪ {(x, y)} [ p(y, u).
Figure 8.69: Correctness of Dijkstra’s algorithm
The length of p( s, u) is δ( s, u) = δ( s, y) + δ(y, u). Because y was relaxed when x was put into S, d(y) = δ(s,y) by the convergence property. Thus d(y) ≤ δ(s,u) ≤ d(u). But, because d(u) is the smallest among vertices not in S, d(u) ~ d(y ). The only possibility is d(u) = d(y ), which requires d(u) = δ(s,u) contradicting the assumption. By the upper bound property, d(u) ≥ δ(s, u). Since d(u) > δ(s, u) is false, d(u) = δ(s, u), which is what we wanted to prove. Thus, if we follow the algorithm’s pr ocedure, transferring from V to S, the vertex in V with the smallest value of d(u) then all vertices in S have d(v) = δ(s,v)
8.6.3 Bellman-Ford Algorithm
Dijkstra’s single-source shortest path algorithm works if all edges weights are non-negative and there are no negative cost cycles. Bellman-Ford allows negative weights edges and no negative cost cycles. The algorithm is slower than Dijkstra’s, running in Θ(VE) time.
Like Dijkstra’s algorithm, Bellman-Ford is based on perform ing repeated relaxations. Bellman- Ford applies relaxation to every edge of the graph and repeats this V- 1 times. Here is the algorithm; its is illustrated in Figure 8.70.
= δ(s,vi )
Recall from Dijkstra’s algorithm that d[vi ] is never less than δ(s,vi ). Thus, d[vi ] is in fact equal to δ(s,vi ). This completes the induction proof.
In summary, after i passes through the for loop, all vertices that are i edges away along the shortest path tree from the source have the correct values stored in d[u]. Thus, after the (V - 1)st^ iteration of the for loop, all vertices u have correct distance values stored in d[u].