0% found this document useful (0 votes)
61 views12 pages

Algorithms and Computations Complexity: Single-Source Shortest Paths

The document summarizes algorithms for solving single-source shortest path problems on graphs: - Bellman-Ford can handle graphs with negative edge weights but has O(VE) runtime - Dijkstra's algorithm solves the problem for non-negative edge weights using a min priority queue in O(ElogV) time - Both algorithms use relaxation to iteratively update and improve shortest path estimates from the source to other vertices

Uploaded by

khil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views12 pages

Algorithms and Computations Complexity: Single-Source Shortest Paths

The document summarizes algorithms for solving single-source shortest path problems on graphs: - Bellman-Ford can handle graphs with negative edge weights but has O(VE) runtime - Dijkstra's algorithm solves the problem for non-negative edge weights using a min priority queue in O(ElogV) time - Both algorithms use relaxation to iteratively update and improve shortest path estimates from the source to other vertices

Uploaded by

khil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 12

Algorithms and Computations Complexity

Single-Source Shortest Paths


Lecture 16
References:
Introduction to Algorithms, Thomas H. Cormen, 2ed edition,

Instructor: Prashant mishra


BIAS, Bhimtal
Single-Source Shortest Paths
In a shortest-paths problem, we are given a weighted, directed
graph G = (V, E), with weight function w : E R mapping
edges to real-valued-weights. The weight of path p = v0, v1, ...,
v k is the sum of the weights of its constituent edges:

We define the shortest-path weight from u to v by

A shortest path from vertex u to vertex v is then defined as any


path p with weight w(p) = (u,v).
Negative-weight edges
In some instances of the single-source shortest-paths problem, there
may be edges whose weights are negative. If the graph G = (V,
E) contains no negative-weight cycles reachable from the source
s, then for all v V , the shortest-path weight (s, v) remains well
defined, even if it has a negative value. If there is a negative-
weight cycle reachable from s, however, shortest-path weights
are not well defined. No path from s to a vertex on the cycle can
be a shortest path-a lesser-weight path can always be found that
follows the proposed "shortest path and then traverses the
negative -weight cycle. If there is a negative-weight cycle on
some path from s to v, we define (s, v) = -.
Figure 1: Negative edge weights in a directed graph. Shown within each vertex
is its shortest-path weight from source s. Because vertices e and f form a
negative-weight cycle reachable from s, they have shortest-path weights of -.
Because vertex g is reachable from a vertex whose shortest-path weight is -,
it, too, has a shortest-path weight of -. Vertices such as h, i, and j are not
reachable from s, and so their shortest-path weights are , even though they
lie on a negative-weight cycle.
Relaxation
INITIALIZE-SINGLE-SOURCE(G, s)
1 for each vertex v V[G]
2 do d[v]
3 [v] NIL
4 d[s] 0

RELAX(u, v, w)
1 if d[v] > d[u] + w(u, v)
2 then d[v] d[u] + w(u, v)
3 [v] u
The Bellman-Ford algorithm
BELLMAN-FORD(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 for i 1 to |V[G]| - 1
3 do for each edge (u, v) E[G]
4 do RELAX(u, v, w)
5 for each edge (u, v) E[G]
6 do if d[v] > d[u] + w(u, v)
7 then return FALSE
8 return TRUE
Figure.4: The execution of the Bellman-Ford algorithm. The source is vertex s. The
d values are shown within the vertices, and shaded edges indicate predecessor
values: if edge (u, v) is shaded, then [v] = u. In this particular example, each pass
relaxes the edges in the order (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t),
(s, y). (a) The situation just before the first pass over the edges. (b)-(e) The situation
after each successive pass over the edges. The d and values in part (e) are the
final values. The Bellman-Ford algorithm returns TRUE in this example.
Dijkstra's algorithm

Dijkstra's algorithm solves the single-source shortest-paths problem on


a weighted, directed graph G = (V, E) for the case in which all edge
weights are nonnegative.

Dijkstra's algorithm maintains a set S of vertices whose final shortest-


path weights from the source s have already been determined. The
algorithm repeatedly selects the vertex u V S with the minimum
shortest-path estimate, adds u to S, and relaxes all edges leaving u.
In the following implementation, we use a min-priority queue Q of
vertices, keyed by their d values.
Dijkstra's algorithm
.

DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2S
3 Q V[G]
4 while Q
5 do u EXTRACT-MIN(Q)
6 S S {u}
7 for each vertex v Adj[u]
8 do RELAX(u, v, w)
Dijkstra's algorithm relaxes edges as shown in next Figure. Line
1 performs the usual initialization of d and values, and line 2
initializes the set S to the empty set. Line 3 initializes the min-
priority queue Q to contain all the vertices in V ; since S = at
that time, the invariant is true after line 3. Each time through
the while loop of lines 4-8, a vertex u is extracted from Q = V -
S and added to set S, thereby maintaining the invariant. (The
first time through this loop, u = s.) Vertex u, therefore, has the
smallest shortest-path estimate of any vertex in V - S. Then,
lines 7-8 relax each edge (u, v) leaving u, thus updating the
estimate d[v] and the predecessor [v] if the shortest path to v
can be improved by going through u. Observe that vertices are
never inserted into Q after line 3 and that each vertex is
extracted from Q and added to S exactly once, so that the
while loop of lines 4-8 iterates exactly |V| times.

You might also like