0% found this document useful (0 votes)
6 views42 pages

6 PCCH

The document discusses the Shortest Paths Problem in graph theory, focusing on algorithms such as Dijkstra's, Bellman-Ford, Johnson's, and Floyd-Warshall for finding optimal paths between vertices. It covers the concepts of weighted graphs, shortest path trees, and the conditions under which these algorithms operate effectively. Additionally, it highlights the importance of handling negative cycles and provides a structured plan for addressing the problem through various algorithmic approaches.
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)
6 views42 pages

6 PCCH

The document discusses the Shortest Paths Problem in graph theory, focusing on algorithms such as Dijkstra's, Bellman-Ford, Johnson's, and Floyd-Warshall for finding optimal paths between vertices. It covers the concepts of weighted graphs, shortest path trees, and the conditions under which these algorithms operate effectively. Additionally, it highlights the importance of handling negative cycles and provides a structured plan for addressing the problem through various algorithmic approaches.
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/ 42

Graphs and Complexity

6) Shortest Paths Problem

[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

 Find a « best » path between two


cities/villages A and B.

 « Best » may be:


 Shortest (km)
 Fastest (minutes)
 Chepeast (€)
 With a minimum total difference in
height (meters)
© Anders Elias, www.data.gouv.fr, m.a.j. 25
mai 2023  With a minimum fuel consumption
(liters).
 …
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 3
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 4
Convention/Terminology

G = (S, A, v) with weight function v : A → R

G is assumed to be directed.

Note. Every algorithm for paths works for undirected graphs as well.

Why ?!

Weight of a path c = ( (s0,s1), (s1,s2), …, (sk-1,sk) ):


𝑘

𝑝 𝑐 = ෍ 𝑣(si−1𝑠𝑖 )
𝑖=1
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 5
Terminology

(Weighted) Distance from s to t :

𝑑 𝑠, 𝑡 = min 𝑝 𝑐 , 𝑐 path from 𝑠 to 𝑡 ∪ +∞

Shortest path from s to t : when 𝑑 𝑠, 𝑡 ≠ +∞, path c from s to t such that

𝑝 𝑐 = 𝑑 𝑠, 𝑡 .

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)

All pairs of vertices


Johnson’s algorithm ϴ(n2logn+nm) or ϴ(nmlog n)
(sparse graphs)

Floyd-Warshall algorithm ϴ(n3)


_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 8
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 9
Paths with the same origin
Weighted digraph G = (S, A, v) with v : A → R
s fixed vertex, s∈ S

Problem : for each t  S compute


d(s, t) = min { p(c) ; c path from s to t }  {+}

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

Tree with root s whose branches are the shortest paths

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)
tS 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)

Initialisation of d’ and  function INIT


for each t  S do
d’(t)  ;(t)  -1;
endfor
d’(s)  0;

Relaxation of the arc (q, r) function RELAX(q, r)


if d’(q) + v(q, r) < d’(r) then
d’(r)  d’(q) + v(q, r); (r)  q;
endif
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 13
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 14
Dijkstra’s algorithm

Condition : v(q, r)  0 for each arc (q, r)

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

This is a greedy algorithm.


_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 15
Running time

Using an adjacency matrix


global running time ϴ(n2)

Using adjacency list(s)


Q : implemented as a balanced search tree (AVL, fr.), ordered
according to the values d’(.)
|S| operations min : ϴ(n log n)
|A| operations RELAX : ϴ(m log n)
Global running time ϴ((n+m) log n)

_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 16
Variant: Dijkstra-Moore algorithm
Condition : none

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 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 longest path without a cycle has n-1 arcs.

→ 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;
QS;
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

No condition : for each arc (q, r), v(q, r)  R

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 »

G = (S, A, v) weighted digraph


S = {1,2,…,n} v : A → R

Matrix of weights: W = (W [i,j]) with

0 if i = j
W [i,j] = v (i,j) if (i, j)  A
 otherwise

Problem. Compute the matrix of distances:


D = ( d(i, j) | 1 ≤ i, j ≤ n )
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 25
Johnson’s algorithm

 Returns the matrix of distances, or


 Indicates that the input graph contains a negative cycle

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).

Then for every path c = ( (s0,s1), (s1,s2), …, (sk-1,sk) ) we have


p’(c)=p(c)+h(s0) - h(sk), where p’ is the path weight obtained with v’.

+ -
+ -
+ -
+ -
+ -
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 27
Conserved features

 The relative lenghts of the paths


if c, c’ are paths from s0 to sk such that p(c)≤ p(c’)
then p’(c)≤ p’(c’)
The shortest paths are the same!

 The circuits with negative weight


in this case s0=sk thus p(c)=p(c’)
The nature of the graph doesn’t change!

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

G=(S,A,v) weighted digraph

Property. Let w be a new vertex, and let G’=(S∪{w},A∪ {ws | s∈ S},v),


where by definition v(ws)=0 for each s∈ S.
Then G has a negative cycle if and only if G’ has a negative cycle.
(deduce what this means in terms of shortest paths!)

Let h(s)=d(w,s) for each s∈ S.


→ h(t) ≤ h(s) + v(s,t),  s,t (triangular inequality)

→ 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

Using adjacency (successors) list(s)

Bellman-Ford : ϴ(nm)
Dijkstra : ϴ((n+m)log n) repeated n times

Total running time : ϴ(nm logn)

May be implemented in ϴ(n2log n + nm).

_______________________________________________________________________________________________________________________________
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

 In general, for optimisation problems (min/max of a cost function)

 « Divide and conquer », but:


 Separate the search of the min/max cost and the identification of a solution
reaching the min/max cost.
 Express the min/max of the problem as a recursive formula that uses the costs
obtained for one or several subproblems.
 Solve the easiest subproblems first, and store their resulting costs in a
matrix/vector.
 Record the information necessary to construct the final solution, once the best cost
is found.
 Use iterative algorithms/programming, not recursive ones.

_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 34
Basic remarks

Condition: no negative cycle in G

Property. If ( (s0,s1), …, (si ,si+1), …, (sj -1, si), …, (sk-1,sk) ) is a


shortest path from s0 to sk in G, then
( (si ,si+1), …, (sj-1,sj) ) is a shortest path from si to sj in G.

shortest

s0 si sj sk

shortest

_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 35
Approach
Notation
Dk = (Dk [i, j] | 1  i, j  n ) with

Dk [i, j] = min{ W(c) | c path from i to j whose


intermediate vertices have numbers  k }

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 18
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

Record all the paths from i to j in an explicit way, for 1 ≤ i, j ≤ n:


n2 paths of maximal length n-1 : O(n3) memory space

Matrix recording the predecessors: ϴ(n2 ) memory space


Pk = (Pk [i, j] | 1 ≤ i, j ≤ n ) with
Pk [i, j] = predecessor of j on a shortest path from i to j whose
intermediate vertices have numbers  k
Dk-1[i, j]
Computation: i s
j
k t
i if (i, j)  A Dk-1[i, j] Dk-1[i, j]
P0 [i, j] = - otherwise

Pk [i, j] = Pk-1 [i, j] if Dk-1[i, j]  Dk-1[i, k] + Dk-1[k, j]


Pk-1 [k, j] otherwise
_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 39
1
1 2 0 18 - 1 - 1
D0 = W = 0 4 - - 2 -
0 8 2 7 0 9 P0 =
4 7 - 3 - 3
0 2 0 4 4 - -
4 3
9 0 18
0 4 - 1 - 1
D1 = P1 = - - 2 -
7 0 9
0 1 0 - 3 - 3
4 1 - -
0 1 58
0 4 - 1 2 1
D2 = P2 = - - 2 -
7 0 9
0 1 5 0 - 3 - 3
4 1 2 -
0 1 5 8 - 1 2 1
D3 = 0 4 13 - - 2 3
7 0 9 P3 =
- 3 - 3
0 1 5 0 4 1 2 -

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

 The shortest path problem has many applications :


 In real-life (all networks)
 In the modelisation of other problems (reconstruction of biological sequences
from segments, for instance)

 The algorithms are simple and as efficient as possible

_______________________________________________________________________________________________________________________________
M1 Informatique – Graphs and Complexity 42

You might also like