Single Source Shortest Path Algorithms
Single Source Shortest Path Algorithms
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.
u v
<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
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…
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
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
Topological sort
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=∞ 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=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 f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 6
5
d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 7
6
∞
d=9 Let’s now call DFS visit from
f=∞ a the vertex a
d=3 f
f=4
c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 11
b c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 12
b c e d f
Topological sort
1) Call DFS(G) to compute
the finishing times f[v]
Time = 13
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