0% found this document useful (0 votes)
25 views

Single Source Shortest Path Algorithms

This document discusses different variants of the single-source shortest path problem on graphs including single-source single-destination, single-source all-destination, single-destination shortest-paths, and all-pairs shortest-paths. It also covers properties of shortest paths including optimal substructure and negative weight cycles.

Uploaded by

Salitha K K
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)
25 views

Single Source Shortest Path Algorithms

This document discusses different variants of the single-source shortest path problem on graphs including single-source single-destination, single-source all-destination, single-destination shortest-paths, and all-pairs shortest-paths. It also covers properties of shortest paths including optimal substructure and negative weight cycles.

Uploaded by

Salitha K K
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/ 47

Single-Source Shortest Path

2 Shortest Path Problems


• Directed weighted graph.

• Path length is sum of weights of edges on path.

• The vertex at which the path begins is the source vertex.

• The vertex at which the path ends is the destination vertex.

t x
6
3 9
3
4
2 1
s 0 2 7
5 3
5 6
11
y z
3 Shortest Path Variants
 Single-source single-destination (1-1): Find the shortest path
from source s to destination v.

 Single-source all-destination(1-Many): Find the shortest path


from s to each vertex v.

 Single-destination shortest-paths (Many-1): Find a shortest path


to a given destination vertex t from each vertex v.

 All-pairs shortest-paths problem (Many-Many): Find a shortest


path from u to v for every pair of vertices u and v.
4 Shortest Path Variants

Single-Source Single-Destination (1-1) Single-Source All-Destination (1-M)

- No good solution that beats 1-M - Need to be solved (several


variant algorithms)
- Thus, this problem is mapped to - We will study this one
the 1-M variant

All-Sources Single-Destination (M-1) All-Sources All-Destinations (M-M)

- Reverse all edges in the graph - Need to be solved (several


- Thus, it is mapped to the (1-M) algorithms)
variant - We will study it (if time permits)
5 Single-Source Shortest Path
 Problem: given a weighted directed graph G, find the
minimum-weight path from a given source vertex s to
another vertex v

 “Shortest-path” = minimum weight

 Weight of path is sum of edges

 E.g., a road map: what is the shortest path from Kunnakulam to


Thrissur?
6 Shortest Path Properties
 Optimal substructure: the shortest path consists of shortest
subpaths:

 suppose some subpath is not a shortest path

 There must then exist a shorter subpath

 Could substitute the shorter subpath for a shorter path

 But then overall path is not shortest path. Contradiction


7 Shortest Path Properties
 Define (u,v) to be the weight of the shortest path from u to v

 Triangle inequality: (u,v)  (u,x) + (x,v)

u v

This path is no longer than any other path


8 Shortest Path Properties
 In graphs with negative weight cycles, some shortest paths will
not exist

<0
Relaxation
Algorithms keep track of d[v], [v]. Initialized as follows:

Initialize(G, s)
for each v  V[G] do u v u v
d[v] := ;
2 2
5 9 5 6

[v] := NIL Change No change


d[v]
od; 5
2
7 5
2
6
u v u v
d[s] := 0

These values are changed when an edge (u, v) is relaxed:

Relax(u, v, w)
if d[v] > d[u] + w(u, v) then
d[v] := d[u] + w(u, v);
[v] := u
fi
Bellman-Ford Algorithm
Can have negative-weight edges.
Will “detect” reachable negative-weight cycles.

Initialize(G, s);
for i := 1 to |V[G]| –1 do
for each (u, v) in E[G] do
Relax(u, v, w) Time
od Complexity
od; is O(VE).
for each (u, v) in E[G] do
if d[v] > d[u] + w(u, v) then
return false
fi
od;
return true
Contd…

 So if Bellman-Ford has not converged after


V(G) - 1 iterations, then there cannot be a
shortest path tree, so there must be a negative
weight cycle.
Example
u v
5
 
–2
6 –3
8
z 0 7
–4
7 2

 
9
x y
Example
u v
5
6 
–2
6 –3
8
z 0 7
–4
7 2

7 
9
x y
Example
u v
5
6 4
–2
6 –3
8
z 0 7
–4
7 2

7 2
9
x y
Example
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2

7 2
9
x y
Example
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2

7 -2
9
x y
17 Bellman-Ford Algorithm

s -1 B
2

A 3 2 E

1
4 -3

C D
5
Dijkstra’s Algorithm For Shortest Paths

 Non-negative edge weight

 Like BFS: If all edge weights are equal, then use


BFS, otherwise use this algorithm

 Use Q = min-priority queue keyed on d[v] values


Dijkstra’s Algorithm
Assumes no negative-weight edges.
Maintains a set S of vertices whose SP from s has been determined.
Repeatedly selects u in V–S with minimum SP estimate (greedy choice).
Store V–S in priority queue Q.
Initialize(G, s);
S := ;
Q := V[G];
while Q   do
u := Extract-Min(Q);
S := S  {u};
for each v  Adj[u] do
Relax(u, v, w)
od
od
Example
u v
1
 

10
9
2 3
s 0 4 6

5 7

 
2
x y
Example
u v
1
10 

10
9
2 3
s 0 4 6

5 7

5 
2
x y
Example
u v
1
8 14

10
9
2 3
s 0 4 6

5 7

5 7
2
x y
Example
u v
1
8 13

10
9
2 3
s 0 4 6

5 7

5 7
2
x y
Example
u v
1
8 9

10
9
2 3
s 0 4 6

5 7

5 7
2
x y
Example
u v
1
8 9

10
9
2 3
s 0 4 6

5 7

5 7
2
x y
Shortest paths in DAGs
 If there are no cycles  it is called a DAG

 In DAGs, nodes can be sorted in a linear order such that all


edges are forward edges

 Topological sort
Single-Source Shortest Paths in DAGs

 Shortest paths are always well-defined in dags


 no cycles => no negative-weight cycles even if there are
negative-weight edges

 Idea: If we were lucky


 To process vertices on each shortest path from left to
right, we would be done in 1 pass
Single-Source Shortest Paths in DAGs

DAG-SHORTEST PATHS(G, s)
TOPOLOGICALLY-SORT the vertices of G
INIT(G, s)
for each vertex u taken in topologically
sorted order do
for each v in Adj[u] do
RELAX(u, v)
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 2
1

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
b c f=∞ vertex d
f=∞

d=∞ d=∞
d e
f=∞ f=∞

d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 3
2

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
b c f=∞ vertex d
f=∞

d=2 d=∞
d e
f=∞ f=∞

d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 4
3
2) Let’s
as each
say vertex
we startis the
finished,
d=∞
insert it onto
the the front
c of a
f=∞ a DFS from vertex
linked list
d=∞ d=1
b c f=∞
f=∞ Next we discover vertex d
d=2 d=∞ Next we discover vertex f
d e
f=∞ f=∞
f is done, move back to d
d=3 f

f=4

f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 5
4

d=∞ Let’s say we start DFS from


f=∞ a the vertex c

d=1 Next we discover vertex d


d=∞
b c f=∞
f=∞ Next we discover the vertex f
d=2 d=∞ f is done, move back to d
d e
f=5 f=∞
d is done, move back to c
d=3 f
f=4

d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 6
5

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover vertex d


d=∞
b c f=∞
f=∞ Next we discover vertex f
d=2 d=∞ f is done, move back to d
d e
f=5 f=∞
d is done, move back to c
d=3 f Next we discover vertex e
f=4

d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 7
6

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover vertex d


d=∞
f=∞
b c f=∞ Both edges from e
Next we discover vertex f
are cross edges
d=2 d=6 f is done, move back to d
d e
f=5 f=∞
d is done, move back to c
d=3 f Next we discover vertex e
f=4
e is done, move back to c
e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 8
7

d=∞ Let’s say we start the DFS from


f=∞ a the vertex c

d=1 Next we discover the vertex d


d=∞ Just a note: If there was (c,f)
b c f=∞
f=∞ Next we
edge discover
in the graph,the vertexbe
it would f
classified as a forward edge
d=2 d = 6 f is done, move back to d
d e (in this particular DFS run)
f=5 f=7
d is done, move back to c
d=3 f Next we discover the vertex e
f=4
e is done, move back to c
c e d f c is done as well
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time
Time == 910


d=9 Let’s now call DFS visit from
f=∞ a the vertex a

d=1 Next we discover vertex c,


d=∞
b c f=8 but c was already processed
f=∞
=> (a,c) is a cross edge
d=2 d=6
d e Next we discover the vertex b
f=5 f=7

d=3 f
f=4

c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 11

d=9 Let’s now call DFS visit from


f=∞ a the vertex a

d=1 Next we discover vertex c,


d=
b c f=8 but c was already processed
10
=> (a,c) is a cross edge
f = 11

d=2 d=6
d e Next we discover the vertex b
f=5 f=7
b is done as (b,d) is a cross
d=3 f edge => now move back to c
f=4

b c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 12

d=9 Let’s now call DFS visit from


f=∞ a the vertex a

d=1 Next we discover the vertex c,


d=
b c f=8 but c was already processed
10
=> (a,c) is a cross edge
f = 11
d=2 d = 6 Next we discover the vertex b
d e
f=5 f=7
b is done as (b,d) is a cross
d=3 edge => now move back to c
f
f=4 a is done as well

b c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 13

d=9 Let’s now call DFS visit from


f = 12 a the vertex a
d= d=1 Next we discover the vertex c,
b c f=8 WE HAVE THE RESULT!
10 but c was already processed
f = 11 3) returnisthe
=> (a,c) linked
a cross list of
edge
d=2 d=6 vertices
d e
f=5 f = 7 Next we discover the vertex b
b is done as (b,d) is a cross
d=3 f edge => now move back to c
f=4
a is done as well
a b c e d f
Topological sort

Time = 13
The linked list is sorted in
d=9 decreasing order of finishing
f = 12 a times f[]

d= d=1
b c f=8
10
f = 11
d=2 d=6
d e
f=5 f=7

d=3 f
f=4

a b c e d f
Example

6 1

r s t u v w
5 2 7 –1 –2
 0    

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0    

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0 2 6  

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0 2 6 6 4

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0 2 6 5 4

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0 2 6 5 3

4
3
2
Example

6 1

r s t u v w
5 2 7 –1 –2
 0 2 6 5 3

4
3
2

You might also like