SlideShare a Scribd company logo
Analysis of Algorithms
Shortest Paths
Dr. AMIT KUMAR @JUET
Shortest Path Problems
• How can we find the shortest route between two
points on a road map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)
Shortest Path Problem
• Input:
– Directed graph G = (V, E)
– Weight function w : E → R
• Weight of path p = v0, v1, . . . , vk
• Shortest-path weight from u to v:
δ(u, v) = min w(p) : u v if there exists a path from u to v
∞ otherwise
• Note: there might be multiple shortest paths from u to v


k
i
ii vvwpw
1
1 ),()(
p
0
3 9
5 11
3
6
5
7
6
s
t x
y z
2
2 1
4
3
Variants of Shortest Path
• Single-source shortest paths
– G = (V, E)  find a shortest path from a given source
vertex s to each vertex v  V
• Single-destination shortest paths
– Find a shortest path to a given destination vertex t
from each vertex v
– Reversing the direction of each edge  single-source
Variants of Shortest Paths (cont’d)
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u
and v
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of
vertices u and v
Negative-Weight Edges
• Negative-weight edges may form
negative-weight cycles
• If such cycles are reachable from
the source, then δ(s, v) is not properly
defined!
– Keep going around the cycle, and get
w(s, v) = -  for all v on the cycle
0
3
-4
2
8
-6
s
a b
e f
-3
y
3
5
6
4
7
c d g
Negative-Weight Edges
• s  a: only one path
δ(s, a) = w(s, a) = 3
• s  b: only one path
δ(s, b) = w(s, a) + w(a, b) = -1
• s  c: infinitely many paths
s, c, s, c, d, c, s, c, d, c, d, c
cycle has positive weight (6 - 3 = 3)
s, c is shortest path with weight δ(s, b) = w(s, c) = 5
0
3 -1
- -
3
-4
2
8
-6
s
a b
e f
-5 11
-3
y
3
5
6
4
7
c d g
Negative-Weight Edges
• s  e: infinitely many paths:
– s, e, s, e, f, e, s, e, f, e, f, e
– cycle e, f, e has negative
weight:
3 + (- 6) = -3
– can find paths from s to e with
arbitrarily large negative
weights
– δ(s, e) = -   no shortest path
exists between s and e
– Similarly: δ(s, f) = - ,
δ(s, g) = - 
0
3 -1
- -
3
-4
2
8
-6
s
a b
e f
-5 11
-3
y
3
5
6
4
7
c d g
 

j
h i
2
3-8
δ(s, h) = δ(s, i) = δ(s, j) = 
h, i, j not
reachable
from s
Cycles
• Can shortest paths contain cycles?
• Negative-weight cycles
– Shortest path is not well defined
• Positive-weight cycles:
– By removing the cycle, we can get a shorter path
• Zero-weight cycles
– No reason to use them
– Can remove them to obtain a path with same weight
No!
No!
Optimal Substructure Theorem
Given:
– A weighted, directed graph G = (V, E)
– A weight function w: E  R,
– A shortest path p = v1, v2, . . . , vk from v1 to vk
– A subpath of p: pij = vi, vi+1, . . . , vj, with 1  i  j  k
Then: pij is a shortest path from vi to vj
Proof: p = v1 vi vj vk
w(p) = w(p1i) + w(pij) + w(pjk)
Assume  pij’ from vi to vj with w(pij’) < w(pij)
 w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p) contradiction!
p1i pij pjk
v1
vi
vj
vk
p1i
pij
pij’
pjk
Triangle Inequality
For all (u, v)  E, we have:
δ (s, v) ≤ δ (s, u) + δ (u, v)
- If u is on the shortest path to v we have the
equality sign
u v
s
v
s
u
Algorithms
• Bellman-Ford algorithm
– Negative weights are allowed
– Negative cycles reachable from the source are not
allowed.
• Dijkstra’s algorithm
– Negative weights are not allowed
• Operations common in both algorithms:
– Initialization
– Relaxation
Shortest-Paths Notation
For each vertex v  V:
• δ(s, v): shortest-path weight
• d[v]: shortest-path weight estimate
– Initially, d[v]=∞
– d[v]δ(s,v) as algorithm progresses
• [v] = predecessor of v on a shortest
path from s
– If no predecessor, [v] = NIL
–  induces a tree—shortest-path tree
0
3 9
5 11
3
6
5
7
6
s
t x
y z
2
2 1
4
3
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0
• All the shortest-paths algorithms start with
INITIALIZE-SINGLE-SOURCE
Relaxation Step
• Relaxing an edge (u, v) = testing whether we
can improve the shortest path to v found so far
by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
 d[v]=d[u]+w(u,v)
 [v] ← u
5 9
2
u v
5 7
2
u v
RELAX(u, v, w)
5 6
2
u v
5 6
2
u v
RELAX(u, v, w)
After relaxation:
d[v]  d[u] + w(u, v)s s
no change
Bellman-Ford Algorithm
• Single-source shortest path problem
– Computes δ(s, v) and [v] for all v  V
• Allows negative edge weights - can detect
negative cycles.
– Returns TRUE if no negative-weight cycles are
reachable from the source s
– Returns FALSE otherwise  no solution exists
Bellman-Ford Algorithm (cont’d)
• Idea:
– Each edge is relaxed |V–1| times by making |V-1|
passes over the whole edge set.
– To make sure that each edge is relaxed exactly
|V – 1| times, it puts the edges in an unordered list
and goes over the list |V – 1| times.
0
 
 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
BELLMAN-FORD(V, E, w, s)
0
 
 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
0
 
 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
6
7
Pass 1
Example
0
6 
7 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
0
6 
7 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
11
2
4
0
6 
7 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
11
2
42
0
6 
7 
6
5
7
7
9
s
t x
y z
8
-3
2
-4
-2
11
2
42
-2
Pass 1
(from
previous
slide)
Pass 2
Pass 3 Pass 4
Detecting Negative Cycles
(perform extra test after V-1 iterations)
• for each edge (u, v)  E
• do if d[v] > d[u] + w(u, v)
• then return FALSE
• return TRUE
0 

c
s b
2
3-8
0 

c
s b
2
3-8
2
5
-3 -3 2
5
c
s b
2
3-8
-1
2
-6
Look at edge (s, b):
d[b] = -1
d[s] + w(s, b) = -4
 d[b] > d[s] + w(s, b)
1st pass 2nd pass
(s,b) (b,c) (c,s)
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
2. for i ← 1 to |V| - 1
3. do for each edge (u, v)  E
4. do RELAX(u, v, w)
5. for each edge (u, v)  E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
Running time: O(V+VE+E)=O(VE)
(V)
O(V)
O(E)
O(E)
O(VE)
Shortest Path Properties
• Upper-bound property
– We always have d[v] ≥ δ (s, v) for all v.
– The estimate never goes up – relaxation only lowers the
estimate
0
6 
7 
6
5
7
7
9
s
v x
y z
8
-3
2
-4
-2
11
2
4
0
6 
7 
6
5
7
7
9
s
v x
y z
8
-3
2
-4
-2
11
2
42
Relax (x, v)
Shortest Path Properties
• Convergence property
If s u → v is a shortest path, and if d[u] = δ(s, u)
at any time prior to relaxing edge (u, v), then
d[v] = δ(s, v) at all times after relaxing (u, v).
0
6 
7
5
2
4
s
u v
1185
4
• If d[v] > δ(s, v)  after relaxation:
d[v] = d[u] + w(u, v)
d[v] = 5 + 2 = 7
• Otherwise, the value remains
unchanged, because it must have
been the shortest path value
Shortest Path Properties
• Path relaxation property
Let p = v0, v1, . . . , vk be a shortest path from
s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . .
, (vk-1, vk), even intermixed with other relaxations,
then d[vk ] = δ (s, vk).
0
6 

5
2
4
s
v1 v2
11

3
5 7
11
14
d[v1] = δ (s, v1)
d[v2] = δ (s, v2)
d[v3] = δ (s, v3)
d[v4] = δ (s, v4)
v3
v4
Correctness of Belman-Ford Algorithm
• Theorem: Show that d[v]= δ (s, v), for every v,
after |V-1| passes.
Case 1: G does not contain negative cycles
which are reachable from s
– Assume that the shortest path from s to v is
p = v0, v1, . . . , vk, where s=v0 and v=vk, k≤|V-1|
– Use mathematical induction on the number of
passes i to show that:
d[vi]= δ (s, vi) , i=0,1,…,k
Correctness of Belman-Ford Algorithm
(cont.)
Base Case: i=0 d[v0]= δ (s, v0)= δ (s, s)= 0
Inductive Hypothesis: d[vi-1]= δ (s, vi-1)
Inductive Step: d[vi]= δ (s, vi)
w
vi-1 vi
s
d[vi-1]= δ (s, vi-1)
From the upper bound property: d[vi]≥ δ (s, vi)
Therefore, d[vi]=δ (s, vi)
After relaxing (vi-1, vi):
d[vi]≤d[vi-1]+w= δ (s, vi-1)+w= δ (s, vi)
Correctness of Belman-Ford Algorithm
(cont.)
• Case 2: G contains a negative cycle which is
reachable from s
<0
d d
d d
d dProof by
Contradiction:
suppose the
algorithm
returns a
solution
Contradiction!
Dijkstra’s Algorithm
• Single-source shortest path problem:
– No negative-weight edges: w(u, v) > 0,  (u, v)  E
• Each edge is relaxed only once!
• Maintains two sets of vertices:
d[v]=δ (s, v) d[v]>δ (s, v)
Dijkstra’s Algorithm (cont.)
• Vertices in V – S reside in a min-priority queue
– Keys in Q are estimates of shortest-path weights d[u]
• Repeatedly select a vertex u  V – S, with the
minimum shortest-path estimate d[u]
• Relax all edges leaving u
Dijkstra (G, w, s)
0
 
 
10
1
5
2
s
t x
y z
2 3
9
7
4 6
0
 
 
10
1
5
2
s
t x
y z
2 3
9
7
4 6
10
5
S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>
Example (cont.)
0
10 
5 
10
1
5
2
s
t x
y z
2 3
9
7
4 6
8 14
7
0
8 14
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
13
S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>
Example (cont.)
0
8 13
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
9
0
8 9
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
2. S ← 
3. Q ← V[G]
4. while Q  
5. do u ← EXTRACT-MIN(Q)
6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w)
9. Update Q (DECREASE_KEY)
Running time: O(VlgV + ElgV) = O(ElgV)
(V)
O(V) build min-heap
Executed O(V) times
O(lgV)
O(E) times
(total)
O(lgV)
O(VlgV)
O(ElgV)
Binary Heap vs Fibonacci Heap
Running time depends on the implementation of the heap
Correctness of Dijskstra’s Algorithm
• For each vertex u  V, we have d[u] = δ(s, u) at the time
when u is added to S.
Proof:
• Let u be the first vertex for which d[u]  δ(s, u) when
added to S
• Let’s look at a true shortest path p from s to u:
Correctness of Dijskstra’s Algorithm
Since u’ is in the shortest path of u: d[u’]<δ(s,u)
What is the value of d[u’]?
d[u’]≤d[v’]+w(v’,u’)= δ(s,v’)+w(v’,u’)
What is the value of d[u]?
d[u]≤d[v]+w(v,u)= δ(s,v)+w(v,u)
Using the upper bound property: d[u]>δ(s,u)
d[u’]<d[u]
Priority Queue Q: <u, …, u’, ….> (i.e., d[u]<…<d[u’]<… )
Contradiction!
All-Pairs Shortest Paths
• Given:
– Directed graph G = (V, E)
– Weight function w : E → R
• Compute:
– The shortest paths between all pairs
of vertices in a graph
– Result: an n × n matrix of shortest-
path distances δ(u, v)
1
2
3
5 4
3
-4 7
6
2
4
1
-5
8
All-Pairs Shortest Paths - Solutions
• Run BELLMAN-FORD once from each vertex:
– O(V2E), which is O(V4) if the graph is dense
(E = (V2))
• If no negative-weight edges, could run
Dijkstra’s algorithm once from each vertex:
– O(VElgV) with binary heap, O(V3lgV) if the graph is
dense
• We can solve the problem in O(V3), with no
elaborate data structures
Application: Feasibility Problem
Application: Feasibility Problem (cont.)
Application: Feasibility Problem (cont.)
Application: Feasibility Problem (cont.)
x1 x2
x3
x4
x5
0
-1
1
5
4
-1
-3
-3
x0
0
0
0
0
0
Application: Feasibility Problem (cont.)
Theorem: If G contains no negative cycles, then
(δ(v0,v1), δ(v0,v2),…, δ(v0,vn)) is a feasible solution.
For every (vi, vj): δ(v0,vj)≤ δ(v0,vi)+w(vi,vj)
or δ(v0,vj) - δ(v0,vi) ≤ w(vi,vj)
Setting xi= δ(v0,vi) and xj= δ(v0,vj), we have
xj-xi≤ w(vi,vj)
Application: Feasibility Problem (cont.)
• Theorem: If G contains a negative cycle, then
there is no feasible solution.
Proof by contradiction: suppose
there exist a solution, then:
Application: Feasibility Problem (cont.)
Problem 1
Write down weights for the edges of the following graph,
so that Dijkstra’s algorithm would not find the correct
shortest path from s to t.
s u
v
w1
1
1
-1
d[s]=0
d[u]=1
d[v]=1
S={s} Q={u,v,w}
d[w]=2
S={s,u} Q={v,w}
d[u]=0
S={s,u,v} Q={w}
1st iteration
2nd iteration 3rd iteration 4th iteration
S={s,u,v,w}
Q={}
• d[w] is not correct!
• d[u] should have converged when u was included in S!
Problem 2
• (Exercise 24.3-4, page 600) We are given a
directed graph G=(V,E) on which each edge
(u,v) has an associated value r(u,v), which is a
real number in the range 0≤r(u,v) ≤1 that
represents the reliability of a communication
channel from vertex u to vertex v.
• We interpret r(u,v) as the probability that the
channel from u to v will not fail, and we assume
that these probabilities are independent.
• Give an efficient algorithm to find the most
reliable path between two given vertices.
Problem 2 (cont.)
• Solution 1: modify Dijkstra’s algorithm
– Perform relaxation as follows:
if d[v] < d[u] w(u,v) then
d[v] = d[u] w(u,v)
– Use “EXTRACT_MAX” instead of “EXTRACT_MIN”
Problem 2 (cont.)
• Solution 2: use Dijkstra’s algorithm without any
modifications!
– r(u,v)=Pr( channel from u to v will not fail)
– Assuming that the probabilities are independent, the
reliability of a path p=<v1,v2,…,vk> is:
r(v1,v2)r(v2,v3) … r(vk-1,vk)
– We want to find the channel with the highest
reliability, i.e.,
( , )
max ( , )p
u v p
r u v


Problem 2 (cont.)
• But Dijkstra’s algorithm computes
• Take the lg
( , )
min ( , )p
u v p
w u v


( , )( , )
lg(max ( , )) max lg( ( , ))p p
u v pu v p
r u v r u v

 
Problem 2 (cont.)
• Turn this into a minimization problem by taking
the negative:
• Run Dijkstra’s algorithm using
( , ) ( , )
min lg( ( , )) min lg( ( , ))p p
u v p u v p
r u v r u v
 
   
( , ) lg( ( , ))w u v r u v 
Ad

More Related Content

What's hot (20)

SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
faisal2204
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
MdSajjadulislamBappi
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
Tanmay Baranwal
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
Rajandeep Gill
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
taimurkhan803
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
InteX Research Lab
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
satya parsana
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
José Juan Herrera
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
ami_01
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Max flow min cut
Max flow min cutMax flow min cut
Max flow min cut
Mayank Garg
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
A. S. M. Shafi
 

Similar to Shortest path algorithms (20)

Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
shanthishyam
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
SaimaShaheen14
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Bellman Ford algorithm and shortest source path algorithm
Bellman Ford algorithm and shortest source path algorithmBellman Ford algorithm and shortest source path algorithm
Bellman Ford algorithm and shortest source path algorithm
nandhananandhu353
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
algorthm analysis from computer scince.ppt
algorthm analysis from computer scince.pptalgorthm analysis from computer scince.ppt
algorthm analysis from computer scince.ppt
AbrehamHgiorgis
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Amit Kumar Rathi
 
Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
Sania Nisar
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
Amit Kumar Rathi
 
Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
shanthishyam
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
SaimaShaheen14
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Bellman Ford algorithm and shortest source path algorithm
Bellman Ford algorithm and shortest source path algorithmBellman Ford algorithm and shortest source path algorithm
Bellman Ford algorithm and shortest source path algorithm
nandhananandhu353
 
algorthm analysis from computer scince.ppt
algorthm analysis from computer scince.pptalgorthm analysis from computer scince.ppt
algorthm analysis from computer scince.ppt
AbrehamHgiorgis
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
Sania Nisar
 
Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
Ad

More from Amit Kumar Rathi (20)

Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
Amit Kumar Rathi
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
Amit Kumar Rathi
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
Amit Kumar Rathi
 
Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
Amit Kumar Rathi
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
Amit Kumar Rathi
 
Merge sort analysis
Merge sort analysisMerge sort analysis
Merge sort analysis
Amit Kumar Rathi
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
Amit Kumar Rathi
 
Linear sort
Linear sortLinear sort
Linear sort
Amit Kumar Rathi
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Greedy algorithm activity selection fractional
Greedy algorithm activity selection fractionalGreedy algorithm activity selection fractional
Greedy algorithm activity selection fractional
Amit Kumar Rathi
 
Graph representation
Graph representationGraph representation
Graph representation
Amit Kumar Rathi
 
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
Amit Kumar Rathi
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
Amit Kumar Rathi
 
Greedy algorithm activity selection fractional
Greedy algorithm activity selection fractionalGreedy algorithm activity selection fractional
Greedy algorithm activity selection fractional
Amit Kumar Rathi
 
Ad

Recently uploaded (20)

Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 

Shortest path algorithms

  • 1. Analysis of Algorithms Shortest Paths Dr. AMIT KUMAR @JUET
  • 2. Shortest Path Problems • How can we find the shortest route between two points on a road map? • Model the problem as a graph problem: – Road map is a weighted graph: vertices = cities edges = road segments between cities edge weights = road distances – Goal: find a shortest path between two vertices (cities)
  • 3. Shortest Path Problem • Input: – Directed graph G = (V, E) – Weight function w : E → R • Weight of path p = v0, v1, . . . , vk • Shortest-path weight from u to v: δ(u, v) = min w(p) : u v if there exists a path from u to v ∞ otherwise • Note: there might be multiple shortest paths from u to v   k i ii vvwpw 1 1 ),()( p 0 3 9 5 11 3 6 5 7 6 s t x y z 2 2 1 4 3
  • 4. Variants of Shortest Path • Single-source shortest paths – G = (V, E)  find a shortest path from a given source vertex s to each vertex v  V • Single-destination shortest paths – Find a shortest path to a given destination vertex t from each vertex v – Reversing the direction of each edge  single-source
  • 5. Variants of Shortest Paths (cont’d) • Single-pair shortest path – Find a shortest path from u to v for given vertices u and v • All-pairs shortest-paths – Find a shortest path from u to v for every pair of vertices u and v
  • 6. Negative-Weight Edges • Negative-weight edges may form negative-weight cycles • If such cycles are reachable from the source, then δ(s, v) is not properly defined! – Keep going around the cycle, and get w(s, v) = -  for all v on the cycle 0 3 -4 2 8 -6 s a b e f -3 y 3 5 6 4 7 c d g
  • 7. Negative-Weight Edges • s  a: only one path δ(s, a) = w(s, a) = 3 • s  b: only one path δ(s, b) = w(s, a) + w(a, b) = -1 • s  c: infinitely many paths s, c, s, c, d, c, s, c, d, c, d, c cycle has positive weight (6 - 3 = 3) s, c is shortest path with weight δ(s, b) = w(s, c) = 5 0 3 -1 - - 3 -4 2 8 -6 s a b e f -5 11 -3 y 3 5 6 4 7 c d g
  • 8. Negative-Weight Edges • s  e: infinitely many paths: – s, e, s, e, f, e, s, e, f, e, f, e – cycle e, f, e has negative weight: 3 + (- 6) = -3 – can find paths from s to e with arbitrarily large negative weights – δ(s, e) = -   no shortest path exists between s and e – Similarly: δ(s, f) = - , δ(s, g) = -  0 3 -1 - - 3 -4 2 8 -6 s a b e f -5 11 -3 y 3 5 6 4 7 c d g    j h i 2 3-8 δ(s, h) = δ(s, i) = δ(s, j) =  h, i, j not reachable from s
  • 9. Cycles • Can shortest paths contain cycles? • Negative-weight cycles – Shortest path is not well defined • Positive-weight cycles: – By removing the cycle, we can get a shorter path • Zero-weight cycles – No reason to use them – Can remove them to obtain a path with same weight No! No!
  • 10. Optimal Substructure Theorem Given: – A weighted, directed graph G = (V, E) – A weight function w: E  R, – A shortest path p = v1, v2, . . . , vk from v1 to vk – A subpath of p: pij = vi, vi+1, . . . , vj, with 1  i  j  k Then: pij is a shortest path from vi to vj Proof: p = v1 vi vj vk w(p) = w(p1i) + w(pij) + w(pjk) Assume  pij’ from vi to vj with w(pij’) < w(pij)  w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p) contradiction! p1i pij pjk v1 vi vj vk p1i pij pij’ pjk
  • 11. Triangle Inequality For all (u, v)  E, we have: δ (s, v) ≤ δ (s, u) + δ (u, v) - If u is on the shortest path to v we have the equality sign u v s v s u
  • 12. Algorithms • Bellman-Ford algorithm – Negative weights are allowed – Negative cycles reachable from the source are not allowed. • Dijkstra’s algorithm – Negative weights are not allowed • Operations common in both algorithms: – Initialization – Relaxation
  • 13. Shortest-Paths Notation For each vertex v  V: • δ(s, v): shortest-path weight • d[v]: shortest-path weight estimate – Initially, d[v]=∞ – d[v]δ(s,v) as algorithm progresses • [v] = predecessor of v on a shortest path from s – If no predecessor, [v] = NIL –  induces a tree—shortest-path tree 0 3 9 5 11 3 6 5 7 6 s t x y z 2 2 1 4 3
  • 14. Initialization Alg.: INITIALIZE-SINGLE-SOURCE(V, s) 1. for each v  V 2. do d[v] ←  3. [v] ← NIL 4. d[s] ← 0 • All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE
  • 15. Relaxation Step • Relaxing an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u If d[v] > d[u] + w(u, v) we can improve the shortest path to v  d[v]=d[u]+w(u,v)  [v] ← u 5 9 2 u v 5 7 2 u v RELAX(u, v, w) 5 6 2 u v 5 6 2 u v RELAX(u, v, w) After relaxation: d[v]  d[u] + w(u, v)s s no change
  • 16. Bellman-Ford Algorithm • Single-source shortest path problem – Computes δ(s, v) and [v] for all v  V • Allows negative edge weights - can detect negative cycles. – Returns TRUE if no negative-weight cycles are reachable from the source s – Returns FALSE otherwise  no solution exists
  • 17. Bellman-Ford Algorithm (cont’d) • Idea: – Each edge is relaxed |V–1| times by making |V-1| passes over the whole edge set. – To make sure that each edge is relaxed exactly |V – 1| times, it puts the edges in an unordered list and goes over the list |V – 1| times. 0     6 5 7 7 9 s t x y z 8 -3 2 -4 -2 (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
  • 18. BELLMAN-FORD(V, E, w, s) 0     6 5 7 7 9 s t x y z 8 -3 2 -4 -2 0     6 5 7 7 9 s t x y z 8 -3 2 -4 -2 E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) 6 7 Pass 1
  • 19. Example 0 6  7  6 5 7 7 9 s t x y z 8 -3 2 -4 -2 (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) 0 6  7  6 5 7 7 9 s t x y z 8 -3 2 -4 -2 11 2 4 0 6  7  6 5 7 7 9 s t x y z 8 -3 2 -4 -2 11 2 42 0 6  7  6 5 7 7 9 s t x y z 8 -3 2 -4 -2 11 2 42 -2 Pass 1 (from previous slide) Pass 2 Pass 3 Pass 4
  • 20. Detecting Negative Cycles (perform extra test after V-1 iterations) • for each edge (u, v)  E • do if d[v] > d[u] + w(u, v) • then return FALSE • return TRUE 0   c s b 2 3-8 0   c s b 2 3-8 2 5 -3 -3 2 5 c s b 2 3-8 -1 2 -6 Look at edge (s, b): d[b] = -1 d[s] + w(s, b) = -4  d[b] > d[s] + w(s, b) 1st pass 2nd pass (s,b) (b,c) (c,s)
  • 21. BELLMAN-FORD(V, E, w, s) 1. INITIALIZE-SINGLE-SOURCE(V, s) 2. for i ← 1 to |V| - 1 3. do for each edge (u, v)  E 4. do RELAX(u, v, w) 5. for each edge (u, v)  E 6. do if d[v] > d[u] + w(u, v) 7. then return FALSE 8. return TRUE Running time: O(V+VE+E)=O(VE) (V) O(V) O(E) O(E) O(VE)
  • 22. Shortest Path Properties • Upper-bound property – We always have d[v] ≥ δ (s, v) for all v. – The estimate never goes up – relaxation only lowers the estimate 0 6  7  6 5 7 7 9 s v x y z 8 -3 2 -4 -2 11 2 4 0 6  7  6 5 7 7 9 s v x y z 8 -3 2 -4 -2 11 2 42 Relax (x, v)
  • 23. Shortest Path Properties • Convergence property If s u → v is a shortest path, and if d[u] = δ(s, u) at any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times after relaxing (u, v). 0 6  7 5 2 4 s u v 1185 4 • If d[v] > δ(s, v)  after relaxation: d[v] = d[u] + w(u, v) d[v] = 5 + 2 = 7 • Otherwise, the value remains unchanged, because it must have been the shortest path value
  • 24. Shortest Path Properties • Path relaxation property Let p = v0, v1, . . . , vk be a shortest path from s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . , (vk-1, vk), even intermixed with other relaxations, then d[vk ] = δ (s, vk). 0 6   5 2 4 s v1 v2 11  3 5 7 11 14 d[v1] = δ (s, v1) d[v2] = δ (s, v2) d[v3] = δ (s, v3) d[v4] = δ (s, v4) v3 v4
  • 25. Correctness of Belman-Ford Algorithm • Theorem: Show that d[v]= δ (s, v), for every v, after |V-1| passes. Case 1: G does not contain negative cycles which are reachable from s – Assume that the shortest path from s to v is p = v0, v1, . . . , vk, where s=v0 and v=vk, k≤|V-1| – Use mathematical induction on the number of passes i to show that: d[vi]= δ (s, vi) , i=0,1,…,k
  • 26. Correctness of Belman-Ford Algorithm (cont.) Base Case: i=0 d[v0]= δ (s, v0)= δ (s, s)= 0 Inductive Hypothesis: d[vi-1]= δ (s, vi-1) Inductive Step: d[vi]= δ (s, vi) w vi-1 vi s d[vi-1]= δ (s, vi-1) From the upper bound property: d[vi]≥ δ (s, vi) Therefore, d[vi]=δ (s, vi) After relaxing (vi-1, vi): d[vi]≤d[vi-1]+w= δ (s, vi-1)+w= δ (s, vi)
  • 27. Correctness of Belman-Ford Algorithm (cont.) • Case 2: G contains a negative cycle which is reachable from s <0 d d d d d dProof by Contradiction: suppose the algorithm returns a solution Contradiction!
  • 28. Dijkstra’s Algorithm • Single-source shortest path problem: – No negative-weight edges: w(u, v) > 0,  (u, v)  E • Each edge is relaxed only once! • Maintains two sets of vertices: d[v]=δ (s, v) d[v]>δ (s, v)
  • 29. Dijkstra’s Algorithm (cont.) • Vertices in V – S reside in a min-priority queue – Keys in Q are estimates of shortest-path weights d[u] • Repeatedly select a vertex u  V – S, with the minimum shortest-path estimate d[u] • Relax all edges leaving u
  • 30. Dijkstra (G, w, s) 0     10 1 5 2 s t x y z 2 3 9 7 4 6 0     10 1 5 2 s t x y z 2 3 9 7 4 6 10 5 S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>
  • 31. Example (cont.) 0 10  5  10 1 5 2 s t x y z 2 3 9 7 4 6 8 14 7 0 8 14 5 7 10 1 5 2 s t x y z 2 3 9 7 4 6 13 S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>
  • 32. Example (cont.) 0 8 13 5 7 10 1 5 2 s t x y z 2 3 9 7 4 6 9 0 8 9 5 7 10 1 5 2 s t x y z 2 3 9 7 4 6 S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>
  • 33. Dijkstra (G, w, s) 1. INITIALIZE-SINGLE-SOURCE(V, s) 2. S ←  3. Q ← V[G] 4. while Q   5. do u ← EXTRACT-MIN(Q) 6. S ← S  {u} 7. for each vertex v  Adj[u] 8. do RELAX(u, v, w) 9. Update Q (DECREASE_KEY) Running time: O(VlgV + ElgV) = O(ElgV) (V) O(V) build min-heap Executed O(V) times O(lgV) O(E) times (total) O(lgV) O(VlgV) O(ElgV)
  • 34. Binary Heap vs Fibonacci Heap Running time depends on the implementation of the heap
  • 35. Correctness of Dijskstra’s Algorithm • For each vertex u  V, we have d[u] = δ(s, u) at the time when u is added to S. Proof: • Let u be the first vertex for which d[u]  δ(s, u) when added to S • Let’s look at a true shortest path p from s to u:
  • 36. Correctness of Dijskstra’s Algorithm Since u’ is in the shortest path of u: d[u’]<δ(s,u) What is the value of d[u’]? d[u’]≤d[v’]+w(v’,u’)= δ(s,v’)+w(v’,u’) What is the value of d[u]? d[u]≤d[v]+w(v,u)= δ(s,v)+w(v,u) Using the upper bound property: d[u]>δ(s,u) d[u’]<d[u] Priority Queue Q: <u, …, u’, ….> (i.e., d[u]<…<d[u’]<… ) Contradiction!
  • 37. All-Pairs Shortest Paths • Given: – Directed graph G = (V, E) – Weight function w : E → R • Compute: – The shortest paths between all pairs of vertices in a graph – Result: an n × n matrix of shortest- path distances δ(u, v) 1 2 3 5 4 3 -4 7 6 2 4 1 -5 8
  • 38. All-Pairs Shortest Paths - Solutions • Run BELLMAN-FORD once from each vertex: – O(V2E), which is O(V4) if the graph is dense (E = (V2)) • If no negative-weight edges, could run Dijkstra’s algorithm once from each vertex: – O(VElgV) with binary heap, O(V3lgV) if the graph is dense • We can solve the problem in O(V3), with no elaborate data structures
  • 42. Application: Feasibility Problem (cont.) x1 x2 x3 x4 x5 0 -1 1 5 4 -1 -3 -3 x0 0 0 0 0 0
  • 43. Application: Feasibility Problem (cont.) Theorem: If G contains no negative cycles, then (δ(v0,v1), δ(v0,v2),…, δ(v0,vn)) is a feasible solution. For every (vi, vj): δ(v0,vj)≤ δ(v0,vi)+w(vi,vj) or δ(v0,vj) - δ(v0,vi) ≤ w(vi,vj) Setting xi= δ(v0,vi) and xj= δ(v0,vj), we have xj-xi≤ w(vi,vj)
  • 44. Application: Feasibility Problem (cont.) • Theorem: If G contains a negative cycle, then there is no feasible solution. Proof by contradiction: suppose there exist a solution, then:
  • 46. Problem 1 Write down weights for the edges of the following graph, so that Dijkstra’s algorithm would not find the correct shortest path from s to t. s u v w1 1 1 -1 d[s]=0 d[u]=1 d[v]=1 S={s} Q={u,v,w} d[w]=2 S={s,u} Q={v,w} d[u]=0 S={s,u,v} Q={w} 1st iteration 2nd iteration 3rd iteration 4th iteration S={s,u,v,w} Q={} • d[w] is not correct! • d[u] should have converged when u was included in S!
  • 47. Problem 2 • (Exercise 24.3-4, page 600) We are given a directed graph G=(V,E) on which each edge (u,v) has an associated value r(u,v), which is a real number in the range 0≤r(u,v) ≤1 that represents the reliability of a communication channel from vertex u to vertex v. • We interpret r(u,v) as the probability that the channel from u to v will not fail, and we assume that these probabilities are independent. • Give an efficient algorithm to find the most reliable path between two given vertices.
  • 48. Problem 2 (cont.) • Solution 1: modify Dijkstra’s algorithm – Perform relaxation as follows: if d[v] < d[u] w(u,v) then d[v] = d[u] w(u,v) – Use “EXTRACT_MAX” instead of “EXTRACT_MIN”
  • 49. Problem 2 (cont.) • Solution 2: use Dijkstra’s algorithm without any modifications! – r(u,v)=Pr( channel from u to v will not fail) – Assuming that the probabilities are independent, the reliability of a path p=<v1,v2,…,vk> is: r(v1,v2)r(v2,v3) … r(vk-1,vk) – We want to find the channel with the highest reliability, i.e., ( , ) max ( , )p u v p r u v  
  • 50. Problem 2 (cont.) • But Dijkstra’s algorithm computes • Take the lg ( , ) min ( , )p u v p w u v   ( , )( , ) lg(max ( , )) max lg( ( , ))p p u v pu v p r u v r u v   
  • 51. Problem 2 (cont.) • Turn this into a minimization problem by taking the negative: • Run Dijkstra’s algorithm using ( , ) ( , ) min lg( ( , )) min lg( ( , ))p p u v p u v p r u v r u v       ( , ) lg( ( , ))w u v r u v 
  翻译: