0% found this document useful (0 votes)
208 views25 pages

16 - Shortest Path Algorithms

Dijkstra's algorithm finds the shortest paths from a single source vertex to all other vertices in a graph with non-negative edge weights. It uses a priority queue to iteratively select the vertex with the shortest path and relax edges to its neighbors. Bellman-Ford algorithm can handle graphs with negative edge weights by relaxing all edges V-1 times to find shortest paths and one more time to detect negative cycles.

Uploaded by

Aman Rath
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)
208 views25 pages

16 - Shortest Path Algorithms

Dijkstra's algorithm finds the shortest paths from a single source vertex to all other vertices in a graph with non-negative edge weights. It uses a priority queue to iteratively select the vertex with the shortest path and relax edges to its neighbors. Bellman-Ford algorithm can handle graphs with negative edge weights by relaxing all edges V-1 times to find shortest paths and one more time to detect negative cycles.

Uploaded by

Aman Rath
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/ 25

Shortest Path Algorithms

Manmath Narayan Sahoo


Shortest-Path Problems
• Shortest-Path problems

– Single-pair. Given two vertices, find a shortest path


between them.
– Single-source. Find a shortest path from a given
source (vertex s) to each of the vertices. Solution to
single-source problem solves single-pair problem
efficiently, too.
– All-pairs. Find shortest-paths for every pair of
vertices.

2
Relaxation
• For each vertex v in the graph, we maintain Dv,
the estimate of the shortest path from s
(initialized to  at the start)
• Relaxing an edge (u,v) with cost duv means
testing whether we can improve the shortest
path to v found so far by going through u
u v u v
2 2 Relax (u,v,G)
5 9 5 6
if Dv > Du+duv then
Relax(u,v) Relax(u,v) Dv = Du+duv
Parentv = u
5 2 7 5 2 6
u v u v
3
Dijkstra's Algorithm
• Non-negative edge weights
• Use Q, a priority queue keyed by Dv (BFS used
FIFO queue, here we use a PQ, which is re-
organized whenever some D decreases)
• Basic idea
– maintain a set S of solved vertices
– at each step select "closest" vertex u, add it to S, and
relax all edges from u

4
Dijkstra’s Pseudo Code
• Input: Graph G, start vertex s
Dijkstra(G(V,E),s)
01 for each vertex u  V
02 Du  
03 Parentu  nil
04 Ds  0
05 S  // Set S : already solved vertices
06 Q.init(V) // Q, intially contains all nodes in G
07 while not Q.isEmpty()
08 u  Q.extractMin() //chose u with minimum Du
09 S  S  {u}
10 for each v  u.adjacent() do
11 Relax(u, v, G)
relaxing
edges

5
Dijkstra’s Example
Dijkstra(G(V,E),s) Pv= 
Pu= 
01 for each vertex u  V u v
1
02 Du   10  
03 Parentu  nil Ps= 
9
04 Ds  0 s 0 2 3
05 S 
4 6
06 Q.init(V) 5 7
07 while not Q.isEmpty()  2 
08 u  Q.extractMin() //grey Px=  x y Py= 
09 S  S  {u} //black
10 for each v  u.adjacent() do u s v 
1
11 Relax(u, v, G) 10 10 
 9
Relax (u,v,G) s 0 2 3
4 6
if Dv > Du+duv then 5 7
Dv = Du+duv 5 2  
s x y
Parentv = u
6
Dijkstra’s Example (2)
Dijkstra(G(V,E),s)
u x v x
01 for each vertex u  V 1
02 Du   10 8 14
03 Parentu  nil  9
04 Ds  0 s 0 2 3
4 6
05 S  7
06 Q.init(V) 5
07 while not Q.isEmpty()
5 2 7 x
08 u  Q.extractMin() //grey x s y
09 S  S  {u} //black
10 for each v  u.adjacent() do x u v y
1
11 Relax(u, v, G)
10 8 13
 9
Relax (u,v,G) s 0 2 3
4 6
7
if Dv > Du+duv then 5
Dv = Du+duv 5 2 7 x
s x y
Parentv = u
7
Dijkstra’s Example (3)
Dijkstra(G(V,E),s)
u x v u
01 for each vertex u  V
1
02 Du   10 8 9
03 Parentu  nil  9
04 Ds  0 s 0 2 3
4 6
05 S  7
06 Q.init(V) 5
07 while not Q.isEmpty() 5 2 7 x
08 u  Q.extractMin() //grey x s y
09 S  S  {u} //black
10 for each v  u.adjacent() do x u v u
1
11 Relax(u, v, G)
10 8 9
 9
Relax (u,v,G) s 0 2 3
4 6
7
if Dv > Du+duv then 5
Dv = Du+duv 5 2 7 x
s x y
Parentv = u
8
Dijkstra's Algorithm

• Dijkstra’s Complexity:
– O(|V| x |E|)

9
Bellman-Ford Algorithm
• Dijkstra’s doesn’t work when there are negative
edges:
4
s x

2 6

v y
-9

• Shortest path from s to v is sv (cost=2), but


actually the shortest path is sxyv (cost=1)

• Bellman-Ford algorithm detects negative cycles


(returns false) or returns the shortest path 10
Relaxation
• For each vertex v in the graph, we maintain Dv,
the estimate of the shortest path from s
(initialized to  at the start)
• Relaxing an edge (u,v) with cost duv means
testing whether we can improve the shortest
path to v found so far by going through u
u v u v
2 2 Relax (u,v,G)
5 9 5 6
if Dv > Du+duv then
Relax(u,v) Relax(u,v) Dv = Du+duv
Parentv = u
5 2 7 5 2 6
u v u v
11
Bellman-Ford Algorithm

Relax (u,v,G)
Bellman-Ford(G,s) if Dv > Du+duv then
01 for each vertex u  V Dv = Du+duv
02 Du  
03 Parentu  NIL Parentv = u
04 Ds  0
05 for i  1 to |V|-1 do
06 for each edge (u,v)  E do
07 Relax (u,v,G)
08 for each edge (u,v)  E do
09 if Dv > Du+duv then
10 return false
11 return true
Cycle
detection
12
Bellman-Ford Example(1)
Bellman-Ford(G,s) t 5 x
for each vertex u  V -2
01
02 Du  
6  
-3
03 Parentu  NIL
04 Ds  0 s 0 8
-4 7
2
05 for i  1 to |V|-1 do 7
06 for each edge (u,v)  E do  9 
07 Relax (u,v,G) y z
08 for each edge (u,v)  E do
09 if Dv > Du+duv then t 2 x
10 return false
11 return true  3 
1
5 8
Relax (u,v,G) s 0 4
9
if Dv > Du+duv then 6 10
Dv = Du+duv  7 
Parentv = u y z

red: edge orders 13


t x t 5 x
2
-2
 3  6 
62 
11
4
1 -3
5 8
s 0 4
9 s 0 8
-4 7
10 2
6 7
 7  14
7 9 -2

2
y z y z

Iteration 1
Iteration 2
Iteration 3 (no relax)
Iteration 4 (no relax)
14
Negative Length cycle example
s 2 x
0 -3 
5


y

15
Bellman-Ford Algorithm
• Bellman-Ford Complexity:
– (|V|-1)|E| + |E| = O(|V|x|E|)

16
Floyd-Warshall Algorithm
• Find all pair shortest paths

• Can be solved by executing Bellman Ford


algorithm |V| times with different sources
– Complexity: O(|V|2 x |E|)
– For dense network/graph O(|V|4)

• Floyd-Warshall algorithm finds in O(|V|3)


complexity
Floyd-Warshall Algorithm - Idea

ds,t(i) – the shortest path from s to t containing only vertices


v1, ..., vi

ds,t(0) = w(s,t)

w(s,t) if k = 0
ds,t(k) =
min{ds,t(k-1), ds,k(k-1) + dk,t(k-1)} if k > 0

D(k) = [ds,t(k)]
Floyd-Warshall Algorithm

FloydWarshall(matrix W, integer n)

for k  1 to n do
for i  1 to n do
for j  1 to n do
dij(k)  min(dij(k-1), dik(k-1) + dkj(k-1))
end
end
// D(k) is formed
end
return D(n)
Floyd-Warshall Algorithm – Example(1)

2
3
7
4
0 3 8  -4
1 8 3
 0  1 7
1
-4
2 -5  4 0  
5
6
4 2  -5 0 
   6 0
Floyd-Warshall Algorithm – Example(2)

0 3 8  -4 0 0 0 0
 0  1 7 0 0 0
D(0)=  4 0   (0)= 0 0
2  -5 0  0 0 0
   6 0 0 0

0 3 8  -4 0 0 0 0
 0  1 7 0 0 0
D(1)=  4 0   (1)= 0 0
2 5 -5 0 -2 0 1 0 0 1
   6 0 0 0
Floyd-Warshall Algorithm – Example(3)

0 3 8  -4 0 0 0 0
 0  1 7 0 0 0
D(1)=  4 0   (1)= 0 0
2 5 -5 0 -2 0 1 0 0 1
   6 0 0 0

0 3 8 4 -4 0 0 0 2 0
 0  1 7 0 0 0
D(2)=  4 0 5 11 (2)= 0 0 2 2
2 5 -5 0 -2 0 1 0 0 1
   6 0 0 0
Floyd-Warshall Algorithm – Example(4)

0 3 8 4 -4 0 0 0 2 0
 0  1 7 0 0 0
D(2)=  4 0 5 11 (2)= 0 0 2 2
2 5 -5 0 -2 0 1 0 0 1
   6 0 0 0

0 3 8 4 -4 0 0 0 2 0
 0  1 7 0 0 0
D(3)=  4 0 5 11 (3)= 0 0 2 2
2 -1 -5 0 -2 0 3 0 0 1
   6 0 0 0
Floyd-Warshall Algorithm – Example(5)

0 3 8 4 -4 0 0 0 2 0
 0  1 7 0 0 0
D(3)=  4 0 5 11 (3)= 0 0 2 2
2 -1 -5 0 -2 0 3 0 0 1
   6 0 0 0

0 3 -1 4 -4 0 0 4 2 0
3 0 -4 1 -1 4 0 4 0 1
D(4)= 7 4 0 5 3 (4)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0
Floyd-Warshall Algorithm – Example(6)

0 3 -1 4 -4 0 0 4 2 0
3 0 -4 1 -1 4 0 4 0 1
D(4)= 7 4 0 5 3 (4)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0

0 3 -1 2 -4 0 0 4 5 0
3 0 -4 1 -1 4 0 4 0 1
D(5)= 7 4 0 5 3 (5)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0

You might also like