Docsity

Dijkstra Algorithm - Design and Analysis - Study Notes, Study notes of Digital Systems Design

Toggle sidebar
bg1
Lecture No. 38
8.6.1 Dijkstra’s Algorithm
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
this path and following it with the edge (u, v) we get a path to v of length d[u] + w(u, v). If this
path is better than the existing path of length d[v] to v, we should take it. The relaxation process is
illustrated in the following figure. We should also remember that the shortest way back to the source
is through u by updating the predecessor pointer.
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Dijkstra Algorithm - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No. 38

8.6.1 Dijkstra’s Algorithm

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

this path and following it with the edge (u , v) we get a path to v of length d[u] + w(u, v). If this

path is better than the existing path of length d[v] to v, we should take it. The relaxation process is

illustrated in the following figure. We should also remember that the shortest way back to the source

is through u by updating the predecessor pointer.

RELAX( (u, v) )

1 if (d[u] +w(u,v) < d[v])

2 then d[v] ← d[u] + w(u, v)

3 pred[v] = u

Observe that whenever we set d[v] to a finite value, there is always evidence of a path

of that length. Therefore d[v] ≥ δ(s,v). If d[v] = δ(s,v), then further relaxations

cannot change its value.

It is not hard to see that if we perform RELAX(U,V) repeatedly over all edges of the graph, the d[v]

values will eventually converge to the final true distance value from s. The cleverness of any shortest

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

by maintaining a subset of vertices, S ⊆ V, for which we claim we know the true distance, d[v] =

δ(s,v). Initially S= ∅, the empty set. We set d[u] = 0 and all others to ∞. One by one we select

vertices from V-S to add toS.

How do we select which vertex among the vertices of V- S to add next to S? Here is greediness

comes in. For each vertex u ∈ (V - S), we have computed a distance estimate d[u].

The greedy thing to do is to take the vertex for which d[u] is minimum, i.e., take the unprocessed

vertex that is closest by our estimate to s. Later, we justify why this is the proper choice. In order to

perform

this selection efficiently, we store the vertices of V- S in a priority queue.

  • Figure 8.67: Dijkstra’s algorithm: select
  • Figure 8.68: Dijkstra’s algorithm: select

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

  1. S = {s}
  2. d(s) = 0, which is δ(s, s) Assume that d(v) = δ(s, v) for every v 2 S and all neighbors of v have been relaxed. If d(u) ∈ d(u′)

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].