6 PCCH
6 PCCH
[email protected]
LS2N, bât. 34, bureau 303
tél. 02.51.12.58.16
Plan
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 2
Introduction
G is assumed to be directed.
Note. Every algorithm for paths works for undirected graphs as well.
Why ?!
𝑝 𝑐 = 𝑣(si−1𝑠𝑖 )
𝑖=1
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 5
Terminology
𝑝 𝑐 = 𝑑 𝑠, 𝑡 .
a 5 b a 5 b
3 3
5 5
s 1 1 1 3 s 1 1 1 3
5 5
c 5 t c 5 t
3 3 9
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 6
Existence of shortest paths
Property
Let 𝑠 ∈ 𝑆. We have d(s, t) > -∞ for all 𝑡 ∈ 𝑆 iff
G has no circuit of weight < 0 that is accessible from s.
5
a b
3
5
s 1 1
1 -3
5
5
c d
3
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 7
Problem and variants
(Generic) Problem : given s, t ∈ S compute d(s, t)
Variants
Paths with the same origin (or same destination/target):
(according to the hypothesis)
Dijkstra’s algorithm ϴ(n2) or ϴ((n+m)logn)
Bellman-Ford algorithm ϴ(nm)
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 9
Paths with the same origin
Weighted digraph G = (S, A, v) with v : A → R
s fixed vertex, s∈ S
5 3 5 8
a b a b
3 3
5 0 5
s 1 1 1 3 s 1 1 1 3
5 5
c 5 d c 5 d
3 3 4 9
Result : a shortest paths tree, i.e. a tree whose branches are the shortest paths.
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 10
Shortest paths tree
3 8 3 8
5 5
a b a b
3 3
0 5 0 5
s 1 1 s 1 1
1 3 1 3
5 5
5 5
c d c d
3 4 9 3 4 9
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 11
Basic properties
Property: G = (S, A, v)
Let c be a shortest path from p to r whose next-to-last vertex is q.
Then d(p, r) = d(p, q) + v(q, r).
p q r
Property: G = (S, A, v)
Let c be a path from p to r whose next-to-last vertex is q.
Then d(p, r) ≤ d(p, q) + v(q, r).
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 12
Relaxation
Compute the values d(s,t) by successive approximations
d’(r)
tS d’(t) = estimation of d(s, t)
(t) = predecessor of t, s q r
i.e. next-to-last vertex of a path
from s to t whose weight is d’(t) d’(q)
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 14
Dijkstra’s algorithm
begin
INIT;
Q S;
while Q do
let q satisfy d’(q)= min{d’(v), v ∈ Q} ;
Q Q - {q};
for each successor r of q such that r ∈ Q do
RELAX(q, r) ;
endfor
endwhile
end
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 16
Variant: Dijkstra-Moore algorithm
Condition : none
begin
INIT;
QS;
while Q do
let q satisfy d’(q)= min{d’(v), v ∈ Q} ;
Q Q - {q};
for each successor r of q do
old d’(r)
RELAX(q, r) ;
if old ≠ d’(r) then
Q Q ∪ {r}
endif
endfor Running time:
endwhile Exponential !
end
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 17
Plan
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 18
Approach
Apply RELAX once (1st) to each arc = visit (and relax along) all the paths
of length 1 of the graph.
Apply RELAX again (2nd) to each arc = visit (and relax along) all the paths
of length 2 of the graph.
…
Apply RELAX again (n-1 –th) to each arc = visit (and relax along) all the
paths of length n-1 of the graph.
→ The n-th call of RELAX cannot improve the results, unless a negative cycle
exists.
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 19
Bellman-Ford algorithm
No condition: for each arc (q, r), v(q, r) R
begin
INIT;
QS;
for i 1 to |S| -1 do
for each (q, r) A do
RELAX(q, r) ;
endfor
endfor
for each (q, r) A do
if d’(q) + v(q, r) < d’(r) then
return « the graph contains a negative cycle »
endif
endfor
Running time:
return « the weights are computed in d’() » ϴ(nm)
end
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 20
Plan
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 21
When G is a DAG
Assume the topological order has been computed, and we want the
sortest paths with origin=the first vertex in the order.
begin
INIT;
for each q S in topological order do Running time:
for each successor r of q do ϴ(n+m) !
RELAX(q, r) ;
endfor
endfor
end
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 22
Example
-6
a b
3
5
s -1
-1
5
Topological order
5
c d c, s, a, b, d
-3
5
-1
c 5 s 3 a -6 b -1 d
-3 5
What to do if our initial vertex is not c ?
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 23
Plan
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 24
Variant « All pairs of vertices »
0 if i = j
W [i,j] = v (i,j) if (i, j) A
otherwise
Approach :
❖ define a new weight ≥ 0 on the arcs, paying attention to keep
the shortest paths unchanged
❖ apply Dijkstra’s algorithm for each vertex
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 26
Reweighting
Property. G=(S,A,v) weighted digraph v: A→ R. Let h: S→ R be an arbitrary
function.
Define v’: A→ R as v’(s,t)=v(s,t)+h(s)-h(t).
+ -
+ -
+ -
+ -
+ -
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 27
Conserved features
Goal: Find a function h that yields positive or 0 weights v’, and then look
for the shortest paths with respect to v’.
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 28
Positive or 0 weights
→ v’(s,t)=v(s,t)+h(s)-h(t)≥ 0, s,t
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 29
Example
0
0
1 0 2
1/-1 1 2/0 1
0
7 2
8 2 w 0 0 3
w -1 -4 7 0
0
4 5 3
9
4/0 3/-4
4
0
Weight vv Weight v’
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 30
Algorithm Johnson(G=(S,A,v))
Johnson’s algorithm
begin
G’← (S∪ {w}, A∪ {ws | s∈ S} ) ;
for each s ∈ S do v(w,s) 0 endfor
Bellman-Ford (G’,w,v) // detects a negative cycle or computes the distances
if there exists a negative cycle then stop
else
for each vertex s∈ S∪ {w} do
h(s) ← d(w,s) endfor // computed above using Bellman-Ford
for each arc (s,t)∈ A∪ {ws | s∈ S} do
v’(s,t)← v(s,t)+h(s)-h(t) endfor //reweighting
for each vertex s∈ S do
Dijkstra(G,s,v’) // obtains the distances d’(s,u), u ∈ S, with respect to v’
for each vertex u ∈ S do
d(s,u)=d’(s,u)-h(s)+h(u) // computes the distances with respect to v
endfor endfor
endif
end
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 31
Running time
Bellman-Ford : ϴ(nm)
Dijkstra : ϴ((n+m)log n) repeated n times
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 32
Plan
Introduction
Variant « Paths with the same origin »
Dijkstra’s algorithm
Bellman-Ford algorithm
Particular case: DAGs
Variant « All pairs of vertices »
Johnson’s algorithm
Floyd-Warshall algorithm
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 33
Dynamic programming
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 34
Basic remarks
shortest
s0 si sj sk
shortest
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 35
Approach
Notation
Dk = (Dk [i, j] | 1 i, j n ) with
D0 = W
Dn = matrix of distances for G
(= the matrix D we want to compute) i k j
no k no k
Property. For each k 1,
Dk [i,j] = min{ Dk-1 [i,j] , Dk-1 [i,k] + Dk -1 [k,j] }
Computation :
of Dk using Dk-1 in ϴ(n2)
of D = Dn in ϴ(n3)
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 36
Floyd-Warshall algorithm
for k 1 to n do
for i 1 to n do
for j 1 to n do
D[i, j] min { D[i, j] , D[i, k] + D[k, j] } endfor …
k j
k c
Dk = min { a, b + c }
i b a
a
i j
k
b c
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 37
Example
1
1 2 0 1 8
8 2 D0 = W = 0 4
0 4 7 7 0 9
0 2 0
4 3
9 0 18
D1 = 0 4
7 0 9
0 1 0
0 1 58
D2 = 0 4
7 0 9
0 1 5 0
0 1 5 8
D3 = 0 4 13
7 0 9
0 1 5 0
0 1 5 8
D4 = 13 0 4 13
9 7 0 9
0 1 5 0
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 38
Recovering the paths
0 1 5 8 - 1 2 1
D4 = 13 0 4 13 4 - 2 3
9 7 0 9 P4 = 4 3 - 3
0 1 5 0 4 1 2 -
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 40
1
1 2
0 8 2 0 1 5 8 - 1 2 1
4 7 D4 = 13 0 4 13 4 - 2 3
9 7 0 9 P4 = 4 3 - 3
4 3 0 1 5 0 4 1 2 -
9
Example of a path
distance from 2 to 1 : D4[2,1] = 13
P4[2,1] = 4 ; P4[2,4] = 3 ; P4[2,3] = 2 ;
4 9 0
2 3 4 1
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 41
In conclusion
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 42