Single Source Shortest Path Unit-3
Single Source Shortest Path Unit-3
t x
6
3 9
3
4
2 1
s 0 2 7
5 3
5 6 11
y z
Example
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
14
14
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Shortest-Path Variants
No need to consider different solution or algorithm for each variant
(we reduce it into two types)
-No good solution that beats 1-M variant -Need to be solved (several algorithms)
-Thus, this problem is mapped to the 1-M -We will study this one
variant
-No good solution that beats 1-M variant -Need to be solved (several algorithms)
-Thus, this problem is mapped to the 1-M -We will study this one
variant
Weight of path p = v1 v2 … vk is
k 1
w( p) w(vi , vi 1 )
i 1
Shortest Path
Shortest Path = Path of minimum weight
δ(u,v)=
p
min{ω(p) : u v}; if there is a path from u to v,
otherwise.
Several Properties
1- Optimal Substructure Property
• Let δ(1,k) = <v1, ..i, .. j.., .. ,vk > be a shortest path from v1 to vk
• Let δ(i,j) = <vi, ... ,vj > be subpath of δ(1,k) from vi to vj
for any i, j
• Then δ(I,j) is a shortest path from vi to vj
1- Optimal Substructure Property
v0 v1 v2 v3 v4 v5 v6 v7
cycle
<0
s v
• Positive-weight cycle : can be taken out and reduces the cost
3- Negative-weight edges
u v
s
Algorithms to Cover
• Dijkstra’s Algorithm
d[v] ← ∞
π[v] ← NIL Parent node
d[s] ← 0
Relaxation
When you find an edge (u,v) then
RELAX(u, v) check this condition and relax d[v] if
if d[v] > d[u]+w(u,v) then possible
d[v] ← d[u]+w(u,v)
π[v] ← u
u v u v
2 2
Change d[v] No chnage
2 2
u v u v
Algorithms to Cover
• Dijkstra’s Algorithm
• Like BFS: If all edge weights are equal, then use BFS,
otherwise use this algorithm
DIJKSTRA(G, s)
u v
INIT(G, s)
1
S←Ø > set of discovered nodes ¥ ¥
10
Q←V[G]
2 3 9
while Q ≠Ø do s 0 4 6
u←EXTRACT-MIN(Q) 7
5
S←S U {u} ¥ ¥
2
for each v in Adj[u] do
x y
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v])
Example
u v
1
10
2 3 9
s 4 6
7
5
2
x y
Example
DIJKSTRA(G, s) u v
INIT(G, s)
1
S←Ø > set of discovered nodes
Q←V[G] 10
9
while Q ≠Ø do
2 3
u←EXTRACT-MIN(Q) s 4 6
S←S U {u}
for each v in Adj[u] do 7
RELAX(u, v) > May cause 5
> DECREASE-KEY(Q, v, d[v]) 2
x y
Example
u v
DIJKSTRA(G, s)
INIT(G, s) 1
S←Ø > set of discovered nodes
10
Q←V[G]
while Q ≠Ø do 2 3 9
u←EXTRACT-MIN(Q) s 4 6
S←S U {u}
for each v in Adj[u] do 7
5
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v]) 2
x y
Example
DIJKSTRA(G, s) u v
INIT(G, s) 1
S←Ø > set of discovered nodes
10
Q←V[G]
while Q ≠Ø do 2 3 9
u←EXTRACT-MIN(Q) s 4 6
S←S U {u}
for each v in Adj[u] do 7
5
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v])
2
x y
Example
DIJKSTRA(G, s) u v
INIT(G, s) 1
S←Ø > set of discovered nodes
Q←V[G] 10
while Q ≠Ø do
2 3 9
u←EXTRACT-MIN(Q) s
S←S U {u}
4 6
for each v in Adj[u] do 7
RELAX(u, v) > May cause
5
> DECREASE-KEY(Q, v, d[v]) 2
x y
Example
u v
1
10
2 3 9
s 4 6
7
5
2
x y
Dijkstra’s Algorithm Analysis
O(V)
DIJKSTRA(G, s)
INIT(G, s)
S←Ø > set of discovered nodes
Q←V[G] O(V Log V)
while Q ≠Ø do
u←EXTRACT-MIN(Q) Total in the loop: O(V Log V)
S←S U {u}
for each v in Adj[u] do
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v])
Total in the loop: O(E Log V)
• Dijkstra’s Algorithm
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
Single-Source Shortest Paths in
DAGs: Analysis
O(V+E)
DAG-SHORTEST PATHS(G, s)
TOPOLOGICALLY-SORT the vertices of G
INIT(G, s) O(V)
for each vertex u taken in topologically sorted order do
for each v in Adj[u] do
RELAX(u, v) Total O(E)
Time Complexity: O (V + E)