adm i | exercise session 9 · v c (v; ) for all (v;w) 2a y a free for all a 2a a solution is called...
TRANSCRIPT
ADM I — Exercise Session 9
TU Berlin
December 11, 2013
TU Berlin Shortest Path 1 / 19
Shortest Path Problem
Given: digraph D = (V ,A), node r ∈ V , arc costs ca, a ∈ A.Task: for each v ∈ V , find dipath from r to v of least cost
Notes:
I single-source shortest path problem
I there may not be a path (unconnected pair of nodes)
I there may not be a shortest path (cycle with negative length)
TU Berlin Shortest Path 2 / 19
Shortest Path – LP
LP formulation
Idea for the LP formulation:
I yv is the shortest distance from s to v
I triangle inequality: yv + c(v ,w) ≤ yw for all (v ,w) ∈ A
Linear Program, shortest r -s path:
max ys − yrs.t. yw − yv ≤ c(v ,w) for all (v ,w) ∈ Aya free for all a ∈ A
a solution is called a feasible potential
Observation:ya, a ∈ A is a feas. pot. ⇒ y ′a = {ya + c}, c ∈ R is a feas. pot. assume yr = 0
TU Berlin Shortest Path 3 / 19
Shortest Path – LP
LP formulation
Idea for the LP formulation:
I yv is the shortest distance from s to v
I triangle inequality: yv + c(v ,w) ≤ yw for all (v ,w) ∈ A
Linear Program, shortest r -s path:
max ys − yrs.t. yw − yv ≤ c(v ,w) for all (v ,w) ∈ Aya free for all a ∈ A
a solution is called a feasible potential
Observation:ya, a ∈ A is a feas. pot. ⇒ y ′a = {ya + c}, c ∈ R is a feas. pot. assume yr = 0
TU Berlin Shortest Path 3 / 19
Shortest Path – LP
LP formulation
Idea for the LP formulation:
I yv is the shortest distance from s to v
I triangle inequality: yv + c(v ,w) ≤ yw for all (v ,w) ∈ A
Linear Program, shortest r -s path:
max ys − yrs.t. yw − yv ≤ c(v ,w) for all (v ,w) ∈ Aya free for all a ∈ A
a solution is called a feasible potential
Observation:ya, a ∈ A is a feas. pot. ⇒ y ′a = {ya + c}, c ∈ R is a feas. pot. assume yr = 0
TU Berlin Shortest Path 3 / 19
Shortest Path – dual LP
Primal and Dual LP FormulationDual and primal linear program for the shortest path problem
Primal LP:max ys − yrs.t. yw−yv ≤ c(v ,w)
∀(v ,w) ∈ A
Dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ≥ 0 for all a ∈ A
interpretation:
I ya is bound on distance to r ,
I xa is the ”number“ of time the edge a is used
TU Berlin Shortest Path 4 / 19
Shortest Path – dual LP
Primal and Dual LP FormulationDual and primal linear program for the shortest path problem
Primal LP:max yss.t. yw−yv ≤ c(v ,w)
∀(v ,w) ∈ Ayr = 0
Dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ≥ 0 for all a ∈ A
interpretation:
I ya is bound on distance to r ,
I xa is the ”number“ of time the edge a is used
TU Berlin Shortest Path 4 / 19
Example
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
I directed graph D
I shortest r -v path
I feasible solutionfor the primal LP
I set yr = 0 andobeying triangleinequality
dual solution:set of arcs
I arcs r → 1, 2→ 3 ,3→ 5, 5→ v used once
I xa = 1 for a ∈ {(r , 2), (2, 3), (3, 5), (5, v)}, all other xa = 0
TU Berlin Shortest Path 5 / 19
Example
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
I directed graph D
I shortest r -v path
I feasible solutionfor the primal LP
I set yr = 0 andobeying triangleinequality
dual solution:set of arcs
I arcs r → 1, 2→ 3 ,3→ 5, 5→ v used once
I xa = 1 for a ∈ {(r , 2), (2, 3), (3, 5), (5, v)}, all other xa = 0
TU Berlin Shortest Path 5 / 19
Example
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
-3.5
-2.5
-1.5
-0.5
0.5
16
I directed graph D
I shortest r -v path
I feasible solutionfor the primal LP
I set yr = 0 andobeying triangleinequality
dual solution:set of arcs
I arcs r → 1, 2→ 3 ,3→ 5, 5→ v used once
I xa = 1 for a ∈ {(r , 2), (2, 3), (3, 5), (5, v)}, all other xa = 0
TU Berlin Shortest Path 5 / 19
Example
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
3
4
2
I directed graph D
I shortest r -v path
I feasible solutionfor the primal LP
I set yr = 0 andobeying triangleinequality
dual solution:set of arcs
I arcs r → 1, 2→ 3 ,3→ 5, 5→ v used once
I xa = 1 for a ∈ {(r , 2), (2, 3), (3, 5), (5, v)}, all other xa = 0
TU Berlin Shortest Path 5 / 19
Example
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
3
4
2
I directed graph D
I shortest r -v path
I feasible solutionfor the primal LP
I set yr = 0 andobeying triangleinequality
dual solution:set of arcs
I arcs r → 1, 2→ 3 ,3→ 5, 5→ v used once
I xa = 1 for a ∈ {(r , 2), (2, 3), (3, 5), (5, v)}, all other xa = 0
TU Berlin Shortest Path 5 / 19
Shortest Path – Dual LP
dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ≥ 0 for all a ∈ Awith br = −1, bv = 1, bv = 0 for all v ∈ V \ {r , v}
interpretation:
I r has one outgoing edge
I v has one incoming edge
I v ∈ V \ {r , v} has samenumber of incoming andoutgoing edges
I ⇒ set {a|xa = 1} contains ar -v path
r
4
2
3
5
v6
7
TU Berlin Shortest Path 6 / 19
Shortest Path – Dual LP
dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa∈ {0, 1} for all a ∈ Awith br = −1, bv = 1, bv = 0 for all v ∈ V \ {r , v}
interpretation:I r has one outgoing edge
I v has one incoming edge
I v ∈ V \ {r , v} has samenumber of incoming andoutgoing edges
I ⇒ set {a|xa = 1} contains ar -v path
r
4
2
3
5
v6
7
TU Berlin Shortest Path 6 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
I if contains cycle with cost 0
I decrease values alongcycle, remove at least oneedge
I degree constraints still valid
I optimal solution withoutcycles
TU Berlin Shortest Path 7 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.4
0.3
0.2
I if contains cycle with cost 0
I decrease values alongcycle, remove at least oneedge
I degree constraints still valid
I optimal solution withoutcycles
TU Berlin Shortest Path 7 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.2
0.1
I if contains cycle with cost 0
I decrease values alongcycle, remove at least oneedge
I degree constraints still valid
I optimal solution withoutcycles
TU Berlin Shortest Path 7 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.2
0.1
I if contains cycle with cost 0
I decrease values alongcycle, remove at least oneedge
I degree constraints still valid
I optimal solution withoutcycles
TU Berlin Shortest Path 7 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.2
0.1
I if contains cycle with cost 0
I decrease values alongcycle, remove at least oneedge
I degree constraints still valid
I optimal solution withoutcycles
TU Berlin Shortest Path 7 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.3
0.2
0.7
0.3
0.4
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integrality of the dual LP
integrality dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vxa ∈ {0, 1} for all a ∈ A
fractional solution:
r
4
2
3
5
v6
7
0.6
0.5
0.4 0.1
I if contains parallel paths
I decrease values along onepath, increase along others
I at least one edge vanishes
I optimality all parallelpaths have the same length
I optimal solution withoutparallel paths
⇒ we constructed integral solution
TU Berlin Shortest Path 8 / 19
Shortest Path – integality of the dual LP
simplex algorithm find integral solutions:
I Lemma 5.16: basis is a spanning tree
I r -v using edges a with xa > 0 is unique
I exactly one path with xa = 1
alternative proof via solution of the dual, using proof ofFord’s Algorithm
TU Berlin Shortest Path 9 / 19
Shortest Path – LP for single source shortest path
LP model for all end nodes, also integral solution
Primal LP:
max∑
ys − yrs.t. yw−yv ≤ c(v ,w)
∀(v ,w) ∈ Ayr = 0
Dual LP:min cT · xs.t.
∑a∈δ−(v) xa −
∑a∈δ+(v) xa = bv ,
∀v ∈ Vbr = −n + 1, bv = 1 for all v ∈ V \ {r}xa ≥ 0 for all a ∈ A
I integrality ensured due to the simplex algorithm
TU Berlin Shortest Path 10 / 19
Shortest Path – Ford’s Algorithm
Algorithm 1 Ford’s Algorithm
1: yr := 0, p(r) := r2: yv :=∞ for all v ∈ V3: pv :=null for all v ∈ V4: while ∃(v ,w) ∈ A with yw > yv + c(v ,w) do5: yw := yv + c(v ,w)
6: p(w) := v7: end while
Running time: O(∑
a∈A ca · n2) not polynomial
I sequence of labeling edges is important
I running time should not depend on the weight of the edges
TU Berlin Shortest Path 11 / 19
Shortest Path – Ford’s Algorithm
Hint: exercise 45b), tight example for the bound of the runningtime
I use parallel edges
I store used edge for each vertex v ,instead of previous node (p[v ])
I parallel edges can be removed using an intermediate vertex
I but handling parallel edges is easier
I number of changes in distance label yv is exponential
1 2 1 2
3
4
5
TU Berlin Shortest Path 12 / 19
Shortest Path – Bellmanford
Algorithm 2 Bellman–Ford Algorithm
1: yr := 0, p(r) := r2: yv :=∞ for all v ∈ V3: pv :=null for all v ∈ V4: for i = 1 to n − 1 do5: for all (v ,w) ∈ A do6: if yw > yv + c(v ,w) then7: yw := yv + c(v ,w)
8: p(w) := v9: end if
10: end for11: end for
Running time: O(n ·m) polynomial!
I labeling sequence: n − 1 times all edges in a fixed order
TU Berlin Shortest Path 13 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.1: (r , 2)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0 ∞
∞
∞
∞
1
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.1: (r , 2)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0 ∞
∞
∞
∞
1
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.2: (1, 3)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
1
2Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.2: (1, 3)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
1
2Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.3: (3, 5)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
1
2
3
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.3: (3, 5)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
1
2
3
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.4: (5, v)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
1
2
3
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.4: (5, v)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
1
2
3
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.5: (r , 4)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
3
4
2
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge no.5: (r , 4)
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
3
4
2
Ford’s Algorithm:
I initialization
I checking triangleinequality
5 labeling steps
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 1
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
3
4
2
Ford’s Algorithm:
I initialization
I checking triangleinequality
5 labeling steps
all labeled edges in shortest paths tree, perfect sequence
TU Berlin Shortest Path 14 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 1: (r , 3) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
3Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 1: (r , 3) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
3Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 2: (3, v) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞∞
3
7
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 2: (3, v) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞∞
3
7
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 3: (r , 4) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
3
7
2
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 3: (r , 4) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
3
7
2
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 4: (4, 5) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
3
7
2 5
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 4: (4, 5) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
3
7
2 5
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 5: (5, v) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
3
2 5
6
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 5: (5, v) + 6 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
3
2 5
6
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 6: (r , 2) + 8 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0 3
2 5
61
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 6: (r , 2) + 8 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0 3
2 5
61
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 7: (2, 3) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2 5
61
2Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 7: (2, 3) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2 5
61
2Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 8: (2, v) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2 5
1
2
5
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 8: (2, v) + 7 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2 5
1
2
5
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 9: (3, 5) + 9 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2
1
2
5
3
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 9: (3, 5) + 9 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2
1
2
5
3
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 10: (5, v) + 9 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2
1
2
3
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
edge 10: (5, v) + 9 arcs
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Ford’s Algorithm: Sequence 2
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
2
1
2
3
4
Ford’s Algorithm:
I initialization
I checking triangleinequality
10 labeling steps
81 edges checked, bound is (2C + 1) · n2 = (2 · 4 + 1) · 62 = 324
TU Berlin Shortest Path 15 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
∞
∞
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0 ∞
∞
∞
∞
1Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞
∞
1
3
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞1
3
2
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
∞1
2
2
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
∞
1
2
2
5Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
2
5
3
Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
2
3
4Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
TU Berlin Shortest Path 16 / 19
Shortest Paths – Bellman–Ford vs. Ford
Example – Bellman Ford Algorithm:
r
4
2
3
5
v1
2
3
1
4
3
11 1
4
0
1
2
2
3
4Bellman Ford Algorithm:
I initialization
I sequence:(r , 2), (r , 3), (r , 4), (2, 3), (2, v)(4, 3), (3, v), (3, 5), (4, 5), (5, v)
I first run
no changes after first run, bound is (n − 1) ·m = 5 · 9 = 45
TU Berlin Shortest Path 16 / 19
Bellman-Ford Speed-up
differences to algorithms presented in CoMa:
I single source shortest path, instead all pairs shortest path
I running time n3 instead of n4
I improvement via faster matrix multiplication ...
I ... not possible, idea does not work
I good order to label probably better, not using old distances
TU Berlin Shortest Path 17 / 19
Bellman-Ford Speed-up
differences to algorithms presented in CoMa:
I single source shortest path, instead all pairs shortest path
I running time n3 instead of n4
I improvement via faster matrix multiplication ...
I ... not possible, idea does not work
I good order to label probably better, not using old distances
TU Berlin Shortest Path 17 / 19
Bellman-Ford Speed-up
differences to algorithms presented in CoMa:
I single source shortest path, instead all pairs shortest path
I running time n3 instead of n4
I improvement via faster matrix multiplication ...
I ... not possible, idea does not work
I good order to label probably better, not using old distances
TU Berlin Shortest Path 17 / 19
Bellman-Ford Speed-up
differences to algorithms presented in CoMa:
I single source shortest path, instead all pairs shortest path
I running time n3 instead of n4
I improvement via faster matrix multiplication ...
I ... not possible, idea does not work
I good order to label probably better, not using old distances
TU Berlin Shortest Path 17 / 19
Bellman-Ford Speed-up
differences to algorithms presented in CoMa:
I single source shortest path, instead all pairs shortest path
I running time n3 instead of n4
I improvement via faster matrix multiplication ...
I ... not possible, idea does not work
I good order to label probably better, not using old distances
TU Berlin Shortest Path 17 / 19
Shortest Path – Floyd Warshall
Idea: calculate shortest distances between all pairs of jobsuse yu,v for the distance from u to v
Algorithm 3 Floyd Warshall Algorithm
1: yu,v := c(u,v) for all u, v ∈ V2: for i = 1 to n do3: for all v ,w ∈ V do4: yv ,w := min(yv ,w , yv ,i + yi ,w )5: end for6: end for
Running time: O(n3) as fast as Bellman Ford algorithm for a single source
Correctness: ...
TU Berlin Shortest Path 18 / 19
Shortest Path – Floyd Warshall
Idea: calculate shortest distances between all pairs of jobsuse yu,v for the distance from u to v
Algorithm 4 Floyd Warshall Algorithm
1: yu,v := c(u,v) for all u, v ∈ V2: for i = 1 to n do3: for all v ,w ∈ V do4: yv ,w := min(yv ,w , yv ,i + yi ,w )5: end for6: end for
Running time:
O(n3) as fast as Bellman Ford algorithm for a single source
Correctness: ...
TU Berlin Shortest Path 18 / 19
Shortest Path – Floyd Warshall
Idea: calculate shortest distances between all pairs of jobsuse yu,v for the distance from u to v
Algorithm 5 Floyd Warshall Algorithm
1: yu,v := c(u,v) for all u, v ∈ V2: for i = 1 to n do3: for all v ,w ∈ V do4: yv ,w := min(yv ,w , yv ,i + yi ,w )5: end for6: end for
Running time: O(n3) as fast as Bellman Ford algorithm for a single source
Correctness: ...
TU Berlin Shortest Path 18 / 19
Shortest Path – Floyd Warshall
Correctness – brief sketch
I important formula yv ,w := min(yv ,w , yv ,i + yi ,w )
I considers node i as part of the path in loop i
I after i-th loop, yv ,w ≤ shortest distance between v ,wusing inner nodes from set {1, . . . , i}
I at the end: yv ,w shortest v ,w distance with all possible innernodes
u v
iyu,i yi ,v
yu,v
TU Berlin Shortest Path 19 / 19
Shortest Path – Floyd Warshall
Correctness – brief sketch
I important formula yv ,w := min(yv ,w , yv ,i + yi ,w )
I considers node i as part of the path in loop i
I after i-th loop, yv ,w ≤ shortest distance between v ,wusing inner nodes from set {1, . . . , i}
I at the end: yv ,w shortest v ,w distance with all possible innernodes
u v
iyu,i yi ,v
yu,v
TU Berlin Shortest Path 19 / 19
Shortest Path – Floyd Warshall
Correctness – brief sketch
I important formula yv ,w := min(yv ,w , yv ,i + yi ,w )
I considers node i as part of the path in loop i
I after i-th loop, yv ,w ≤ shortest distance between v ,wusing inner nodes from set {1, . . . , i}
I at the end: yv ,w shortest v ,w distance with all possible innernodes
u v
iyu,i yi ,v
yu,v
TU Berlin Shortest Path 19 / 19
Shortest Path – Floyd Warshall
Correctness – brief sketch
I important formula yv ,w := min(yv ,w , yv ,i + yi ,w )
I considers node i as part of the path in loop i
I after i-th loop, yv ,w ≤ shortest distance between v ,wusing inner nodes from set {1, . . . , i}
I at the end: yv ,w shortest v ,w distance with all possible innernodes
u v
iyu,i yi ,v
yu,v
TU Berlin Shortest Path 19 / 19