The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2...
Transcript of The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2...
![Page 1: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/1.jpg)
1Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
The Shortest Path ProblemJesper Larsen & Jens Clausen
Informatics and Mathematical Modelling
Technical University of Denmark
![Page 2: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/2.jpg)
2Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
The Shortest Path Problem3
4
7
6
2
5
3
15
1
1
3
1
4
13
1
7
1
1
Given a directed network G = (V,E, c) for which theunderlying undirected graph is connected.Furthermore, a source vertex r is given. Find foreach v ∈ V a dipath from r to v (if such one exists).
![Page 3: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/3.jpg)
3Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Mathematical Programming FormulationSuppose r is vertex 1. For r n − 1 paths have toleave r. For any other vertex, the number ofpaths entering the vertex must be exactly 1larger than the number of paths leaving thevertex.
Let xe denote the number of paths using eachedge e ∈ E.
![Page 4: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/4.jpg)
4Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
This gives the following mathematical model:
min∑
e∈E
cexe
s.t.∑
v∈V
xv1 −∑
v∈V
x1v = −(n − 1)
∑
v∈V
xvu −∑
v∈V
xuv = 1 u ∈ {2, . . . , n}
xe ∈ Z+ e ∈ E
![Page 5: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/5.jpg)
5Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Feasible potentialsConsider an n-vector y = y[1], . . . , y[n]. If y satisfiesthat y[r] = 0 and
∀(v, w) ∈ E : y[v] + c[v, w] ≥ y[w]
then y is called a feasible potential. If P is a pathfrom r to v ∈ V , then if y is a feasible potential,c(P ) ≥ y[v]:
c(P ) =k∑
i=1
cei≥
k∑
i=1
(y[vi]−y[vi−1]) = y[vk]−y[v0] = y[v]
![Page 6: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/6.jpg)
6Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Basic algorithmic ideaStart with the potential with y[r] = 0
Check for each edge if the potential is feasible
If YES - Stop - the potentials identify shortestpaths
If an edge (v, w) violates the feasibilitycondition, update y[w] - this is sometimes called“correct (v, w)” or “relax (v, w)”
![Page 7: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/7.jpg)
7Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford’s Shortest Path AlgorithmStart with p[1] = 0; y[1] = 0; y[v] = ∞; p[v] = −1for all other v.
The predecessor vector p[1], . . . , p[n] is used forpath identification.
while an edge exists (v, w) ∈ E such thaty[w] > y[v] + c[v, w]: sety[w] := y[v] + c[v, w]; p[w] := v
![Page 8: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/8.jpg)
8Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford’s Shortest Path AlgorithmInput: A distance matrix C for a digraph G = (V,E)with n vertices. If the edge (i, j) belongs to E thec(i, j) equals the distance from i to j, otherwisec(i, j) equals ∞.
Output: Two n-vectors, y[.] og p[.], containing thelength of the shortest path from 1 to i resp. thepredecessor vertex for i on the path for each vertexin {1, ..., n}.
![Page 9: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/9.jpg)
9Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford’s algorithm1. Start with p[1] = 0; y[1] = 0; y[v] = ∞ ; p[v] = -1
for all other v;
2. Choose an edge (v,w) ∈ E with y[w] > y[v] +c[v,w]- note that no particular sequence is required ...
3. Set y[w] := y[v] + c[v,w]; p[w] := v;##“correct(v,w)”
4. Stop when no edge (v,w) ∈ E exists withy[w] > y[v] + c[v,w].
![Page 10: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/10.jpg)
10Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Problem with Ford’s AlgorithmComplexity ! Beware of negative length circuits -these may lead to an non-finite computation.Solution: Use the same sequence for the edges ineach iteration.
![Page 11: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/11.jpg)
11Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford-Bellman’s Shortest Path AlgorithmInput: A distance matrix C for a digraph G = (V,E)with n vertices. If the edge (i, j) belongs to E thec(i, j) equals the distance from i to j, otherwisec(i, j) equals ∞.
Output: Two n-vectors, y[.] og p[.], containing thelength of the shortest path from 1 to i resp. thepredecessor vertex for i on the path for each vertexin {1, ..., n}.The vector y[.] is called feasible if for any (i, j) ∈ Eit holds that y[j] ≤ y[i] + c[i,j].
![Page 12: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/12.jpg)
12Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford-Bellman’s Algorithm1. Start with p[1] = 0; y[1] = 0; y[v] = ∞ ; p[v] = -1
for all other v;
2. Set i := 0;
3. while i < n and ¬(y feasible):i := i + 1 ;For (v,w) ∈ E with y[w] > y[v] + c[v,w]:
Set y[w] := y[v] + c[v,w]; p[w] := v;##“correct(v,w)”
![Page 13: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/13.jpg)
13Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Ford-Bellman’s Algorithm with “scan”1. Start with p[1] = 0; y[1] = 0; y[v] = ∞ ; p[v] = -1
for all other v;
2. Set i := 0;
3. while i < n and ¬(y feasible):i := i + 1 ;for v ∈ V
for w ∈ V +(v): if y[w] > y[v] + c[v,w]:Set y[w] := y[v] + c[v,w]; p[w] := v;
![Page 14: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/14.jpg)
14Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Complexity of Ford-Bellman’s AlgorithmInitialization: O(n). Outer loop: (n − 1) times. In theloop: each edge is considered one time - O(m). Allin all: O(nm).
![Page 15: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/15.jpg)
15Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Ford-Bellman’s AlgorithmInduction: After iteration k of the main loop, y[v]contains the length of a shortest path with at most kedges from 1 to v for any v ∈ V . If all distances arenon-negative, a shortest path containing at most(n − 1) edges exists for each v ∈ V . If negativeedge lengths are present, the algorithm still works.If a negative length circuit exists, this can bediscovered by an extra iteration in the main loop. Ifany y[.] changes, there is such a cycle.
![Page 16: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/16.jpg)
16Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Shortest Path in an acyclic graphInput: A distance matrix C for a digraph G = (V,E)with n vertices. If the edge (i, j) belongs to E thec(i, j) equals the distance from i to j, otherwisec(i, j) equals ∞.
Output: Two n-vectors, y[.] og p[.], containing thelength of the shortest path from 1 to i resp. thepredecessor vertex for i on the path for each vertexin {1, ..., n}.V +(v) denotes the edges out of v, i.e.{(v, w) ∈ E|w ∈ V }.
![Page 17: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/17.jpg)
17Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Shortest Path Algorithm for acyclic graphsA topological sorting of the vertices is a numberingnumber : V 7→ {1, ..., n} such that for any(v, w) ∈ V : number(v) < number(w).
1. Find a topological sorting of v1, ..., vn.
2. Start with p[1] = 0; y[1] = 0; y[v] = ∞ ; p[v] = -1for all other v;
3. for i = 1 to n-1:For each w ∈ V +(vi) with y[w] > y[vi] + c[vi, w]:
Set: y[w] := y[vi] + c[vi, w]; p[w] := vi;##“scan vi”
![Page 18: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/18.jpg)
18Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Time Complexity of SP for acyclic graphsEach edge is considered only once in the main loopdue to the topological sorting. Hence, thecomplexity is O(m).
![Page 19: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/19.jpg)
19Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Topological sortingInput: An acyclic digraph G = (V,E) with n vertices.
Output: A numbering number[.] of the vertices in V
so for each edge (v, w) ∈ E it holds that number[v]< number[w].
![Page 20: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/20.jpg)
20Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Algorithm for topological sorting1. Start with all edges unmarked; number[v] = 0
for all v ∈ V ;i = 1;
2. while i ≤ n do
- find v with all incoming edges marked;- if no such v exists : STOP;number[v] := i ; i := i + 1;- mark all (v, w) ∈ E;endwhile
![Page 21: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/21.jpg)
21Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Dijkstra’s Shortest Path AlgorithmInput: A distance matrix C for a digraph G = (V,E)with n vertices. If the edge (i, j) belongs to E thec(i, j) equals the distance from i to j, otherwisec(i, j) equals ∞.
Output: Two n-vectors, y[.] og p[.], containing thelength of the shortest path from 1 to i resp. thepredecessor vertex for i on the path for each vertexin {1, ..., n}.P is the set, for which the shortest path is alreadyfound.
![Page 22: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/22.jpg)
22Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
1. Start with S = {r}; p[r] = 0, y[r] = 0;p[v] = -1, y[v] = ∞ for all other v;P = ∅;
2. Select a v ∈ S such that y[v] is minimal;For {w|(v, w) ∈ E} – P with y[w] > y[v] + c[v,w]
set: y[w] := y[v] + c[v,w]; p[w] := v; S := S ∪{w};
When all vertices in {w|(v, w) ∈ E} – P hasbeen examined:S := S – {v}; P:= P ∪ {v};
3. Stop when S is empty.
![Page 23: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/23.jpg)
23Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Complexity of Dijkstras AlgorithmThe only difference to Prim-Dijkstra’s algorithm forMinimum Spanning Trees is the update step in theinner loop, and this step takes - like in the MSTalgorithm - O(1). Hence the complexity of thealgorithm is O(n2) if a list representation of the y[.]’sis used, and a complexity of O(mlogn) can beobtained if the heap data structure is used for therepresentation of y[.]’s.
![Page 24: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/24.jpg)
24Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Dijkstras AlgorithmThis proof is made by induction:
Suppose that before an operation it holds that 1) foreach vertex u in P, the shortest path from r hasbeen found and is of length y[u], and 2) for eachvertex u not in P, y[u] is the shortest path from fromr to u with all vertices except u belonging to P. Thisis obviously true initially.
![Page 25: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/25.jpg)
25Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Dijkstras Algorithm IILet v be the element with least y[.] value pickedinitially in the inner loop of iteration k. y[v] is thelength of a path Q from r to v passing only throughvertices in P. Suppose that this is not the shortestpath from r to v - then another path R from r to v isshorter.
![Page 26: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/26.jpg)
26Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Dijkstras Algorithm III
R
Qv
wu
r
y[w] >= y[v](= length(Q)) ⇒ length(R) ≥ length(Q)
![Page 27: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/27.jpg)
27Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Dijkstras Algorithm IIR starts in r, which is in P. Since v is not in P, R hasan edge from a vertex in P to a vertex not in P. Let(u,w) be the first edge of this type. w is a candidatefor vertex choice is the current iteration, where v ispicked. Hence y[w] ≥ y[v]. If all edge lengths arenon-negative, the length of the part of R from w to vis non-negative, and hence the total length of R isat least the length of Q.
![Page 28: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/28.jpg)
28Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Dijkstras Algorithm IIIThis is a contradiction – hence Q is a shortest pathfrom r to v. Furthermore, the update step in theinner loop ensures that after the current iteration itagain holds for u not in P (which is now the “old” Paugmented with v) that y[u] is the shortest path fromfrom r to u with all vertices except u belonging to P.
![Page 29: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/29.jpg)
29Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Floyd-Warshall’s all-to-all AlgorithmInput: A distance matrix C for a digraph G = (V,E)with n vertices. If the edge (i, j) belongs to E thec(i, j) equals the distance from i to j, otherwisec(i, j) equals ∞. c(i, i) equals 0 for all i.
Output: Two n × n-vectors, y[.,.] and p[.,.],containing the length of the shortest path from i to jresp. the predecessor vertex for j on the shortestpath for all pairs of vertices in {1, ..., n} × {1, ..., n}.
![Page 30: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/30.jpg)
30Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Floyd-Warshalls Algorithm1. Start with y[i,j] = c(i, j), p[i,j] = i for all (i, j) with
c(i, j) 6= ∞, p[i,j] = 0 otherwise.
2. for k = 1 to n do
for i = 1 to n dofor j = 1 to n doif i 6= k ∧ j 6= k ∧ y[i,j] > y[i,k] + y[k,j] theny[i,j] = y[i,k] + y[k,j]; p[i,j] := p[k,j];
enddo
enddo
enddo
![Page 31: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/31.jpg)
31Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Complexity of Floyd-Warshall’s AlgorithmIn addition to the initialisation, which takes O(n2),the algorithm has three nested loops each of whichis performed n times. The overall complexity ishence O(n3).
![Page 32: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/32.jpg)
32Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Floyd-Warshall’s AlgorithmThis proof is made by induction:
Suppose that prior to iteration k it holds that fori, j ∈ v y[i,j] contains length of the shortest path Qfrom i to j in G containing only vertices in the vertexset {1, ..., k − 1}, and that p[i,j] contains theimmediate predecesor of j on Q. This is obviouslytrue after the initialisation.
![Page 33: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/33.jpg)
33Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Floyd-Warshall’s Algorithm IIIn iteration k, the length of Q is compared to thelength of a path R composed of two subpaths, R1and R2. R1 is an i, k path with “intermediatevertices” only in {1, ..., k − 1}, and R1 is a k, j pathwith “intermediate vertices” only in {1, ..., k − 1}.The shorter of these two is chosen.
![Page 34: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/34.jpg)
34Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Floyd-Warshall’s Algorithm IIIThe shortest path from i to j in G containing onlyvertices in the vertex set {1, ..., k} either a) does notcontain k - and hence is the one found in iterationk − 1 - or b) contains k - and then can bedecomposed into an i, k followed by a k, j path,each of which has been found in iteration k − 1.Hence the update ensures the correctness of theinduction hypothesis after iteration k.
![Page 35: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/35.jpg)
35Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
Correctness of Floyd-Warshall’s Algorithm IV
contains only vertices from {1,..., k−1}
Q
R2
R1
p[i,j]
j
p[k,j]
k
i
![Page 36: The Shortest Path Problem - SDUjbj/DM85/lec6a.pdf · 2019-05-10 · Jesper Larsen & Jens Clausen 2 Informatics and Mathematical Modelling / Operations Research The Shortest Path Problem](https://reader033.fdocuments.in/reader033/viewer/2022041811/5e57f3a9336bb216ae1bfcb7/html5/thumbnails/36.jpg)
36Jesper Larsen & Jens Clausen
Informatics and Mathematical Modelling / Operations Research
34
7
6
2
5
3
15
1
1
3
1
4
13
1
7
1
1