Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views30 pages

Shortest Path

The document discusses various shortest-path problems in weighted directed graphs, including single-source, single-destination, single-pair, and all-pairs shortest-paths problems. It also covers algorithms for solving these problems, such as the Bellman-Ford algorithm and Dijkstra's algorithm, along with considerations for negative weight edges. Additionally, it explains the initialization and relaxation processes involved in these algorithms.

Uploaded by

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

Shortest Path

The document discusses various shortest-path problems in weighted directed graphs, including single-source, single-destination, single-pair, and all-pairs shortest-paths problems. It also covers algorithms for solving these problems, such as the Bellman-Ford algorithm and Dijkstra's algorithm, along with considerations for negative weight edges. Additionally, it explains the initialization and relaxation processes involved in these algorithms.

Uploaded by

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

Single-Source Shortest Paths

Shortest-path problem

• In a shortest paths problem, we are given a weighted,


directed graph G = (V, E) with weight function
w : ER mapping edges to real valued weight.
The weight of path p = { v0, v1, …….vk} is the sum of
the weights of its constituent edges:

w(p)= ∑ w ( vi-1, vi ).
i = 1 to k

2
Shortest-path problem

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


δ (u , v) = min{w( p ) : u → v}, ∃a path from u to v.
p

 ∞, otherwise.

A SP from vertex u to vertex v is then defined


as any path with weight w ( p ) = δ ( p ).

3
Variations in SPs

Single-source shortest-paths problem:

Given a graph G = ( V, E , W ), we want to


find shortest path from a given source
vertex s ε V to every vertex v ε V.

4
Variations in SPs

Single-destination shortest-paths problem:


Given a graph G = ( V, E , W ). Find a SP to
given destination vertex t from every vertex
v. By reversing the direction of each edge
in the graph, we can reduce this problem to
a single-source problem.

5
Variations in SPs

Single-pair shortest-paths problem:


Find a SP from u to v for given
vertices u and v. If we solve the
single-source shortest path
problem with source vertex u, we
solve this problem also.

6
Variations in SPs

All-pairs shortest-paths problem:


Find a SP from u to v for given vertices u
and v. This problem can be solved by
running a single-source algorithm once
from each vertex; but it can usually be
solved faster, and its structure is of
interest in its own right.

7
Negative Weight Edges

In some instances of single-source shortest-path


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.

8
Negative Weight Edges

If there is a negative cycle reachable 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 cycle on some
path from s to v, we define δ ( s, v ) = - infinity9
Shortest-path tree rooted at s

s s
10 10
1 1
10
1 x w 1 x w
t t
1 10 10 1 10
u
10 y y
v 1 10 v
1 1
z z

Original Graph G Shortest-path tree rooted at s


10
Predecessor sub-graph
During the execution of a shortest-paths algorithm,
however, the π values need not indicate shortest
paths. As in BFS, we shall be interested in the
predecessor sub-graph Gπ= (Vπ, Eπ) induced by
the π values. Here again, we define the vertex set
Vπ to be the set of vertices of G with non-NIL
predecessors, plus the source s:
Vπ = { v ∈ V: π [ v ] = NIL} U { s }
The directed edge set
π [ v ], v ) ∈ E : v ∈ Vπ- { s }}.
Eπ = { (π 11
Predecessor graph

π[s] π[t] π[u] π[v] π[w] π[x] π[y] π[z]


NIL s NIL t s t x v
s s
10 10
1 1
10
1 x w 1 x w
t t
1 10 10 1 10
u
10 y y
v 1 10 v
1 1
z z
Original Graph G Shortest-path tree rooted at s 12
Initialize-Single-Source (G, s)

Initialize-Single-Source(G,s)
{ for each vertex v ∈ V[G]
do d[v] ∞
π[v] NIL // end for

d[s]0
}

13
Relaxation

Relax(u,v,w)
{ if d[v]>d[u]+w(u,v)
then d[v]d[u]+w(u,v)
π[v]u
}

14
Relaxation

Relax(u,v,w) Relax(u,v,w)
s
sv
if w(u,v)=2 (<3) π[v]u
s

4 6
u w(u,v) v

4 7 s
u w(u,v) v sv
if w(u,v)=4 (>3)

4 7
u w(u,v) v
15
The Bellman-Ford algorithm

Bellman-Ford(G,w,s)
{ Initialize-Single-Source(G,s)
for i = 1 to |V-1|
do for each edge (u,v)∈ E
do Relex(u,v,w)
for each edge (u,v) ∈ E
do if d[v]> d[u]+w(u,v)
then return false
return true }
16
The Bellman-Ford algorithm

5 5
(a) ∞ -2 ∞ (b) 6 -2 ∞
6 6
8 -3 8 -3
s 0 7 s 0 7
-4 -4
7 7
∞ 2 ∞ 7 2 ∞

9 9

Each pass relaxes the edges in lexicographic order: (u, v), (u, x), (u, y), (v, u), (x, v),
(x, y), (y, v), (y, z), (z, u), (z, x).

17
The Bellman-Ford algorithm

5 5
(c) 6 -2 4 (d) (e) 2 -2 4
6 6
8 -3 8 -3
s 0 7 s 0 7
-4 -4
7 7
7 2 2 7 2 -2

9 9

18
The Bellman-Ford algorithm

The Bellman-Ford algorithm runs in time O(VE), since


the initialization in line 1 takes O(V) time, each |V|-1
passes, over the edges in lines 2-4 takes O(E) time, and
the for loop of lines 5-7 takes O(E) time.

19
Single-source shortest paths in DAGs

DAG-Shortest-Path(G,w,s)
{ Topologically sort V[G]
Initialize-Single-Source(G,s)
for each u taken in topological
order
do for each v ∈ adj[u]
do Relax(u,v,w)

20
Single-source shortest paths in DAGs

DAG-Shortest-Path(G,w,s)
{ Topologically sort V[G]
Initialize-Single-Source(G,s)
for each u taken in topological
order
do for each v ∈ adj[u]
do Relax(u,v,w)

21
Single-source shortest paths in DAGs

• Topological sort needs O(|V|+|E|)

• Initialize-Single-Source requires O(|V|)

• Nested for loops embedded in the


algorithm takes O(|V|+|E|)

22
DAG-Shortest-Path

(a) 6 1
5 s 2 7 -1 -2
∞ 0 ∞ ∞ ∞ ∞
3 4
2

(b) 6 1
5 s 2 7 -1 -2
∞ 0 ∞ ∞ ∞ ∞
3 4
2

23
DAG-Shortest-Path

(c) 6 1
5 s 2 7 -1 -2
∞ 0 2 6 ∞ ∞
3 4
2

(d) 6 1
5 s 2 7 -1 -2
∞ 0 2 6 6 4
3 4
2

24
DAG-Shortest-Path

(e) 6 1
5 s 2 7 -1 -2
∞ 0 2 6 5 4
3 4
2

(f) 6 1
5 s 2 7 -1 -2
∞ 0 2 6 5 3
3 4
2

(g) 6 1
5 s 2 7 -1 -2
∞ 0 2 6 5 3
3 4
2
25
Dijkstra’s algorithm

Q: Priority queue with d as the key


Dijkstra(G,w,s)
{ Initialize-Single-Source(G,s)
Q = V[G]
while Q is not empty
do u = Extract-Min(Q)
for each v ∈ adj[u]
do Relax(u,v,w)
}
26
Dijkstra’s algorithm

(a) 1 (b) 1

10 ∞ ∞ 10 10 ∞
9 9
s 2 3 6 s 2 3 6
0 4 0 4
7 7
5 5
∞ ∞ 5 ∞
2 2

27
Dijkstra’s algorithm

(c) 1 (d) 1

10 8 14 10 8 11
9 9
s 2 3 6 s 2 3 6
0 4 0 4
7 7
5 5
5 7 5 7
2 2

28
Dijkstra’s algorithm

1 1
(e) (e)

10 8 9 10 8 9
9 9
2 3 2 3
s 6 s 6
0 4 0 4
7 7
5 5
5 7 5 7
2 2

29
Dijkstra’s algorithm

• Linear array O(|V|2) when G = V ( V, E, W)


is represented as adjacency matrix.
• Binary heap O(|E|log|V|)
• Fibonacci heap O(|E|+|V|log|V|)

30

You might also like