Single Source Shortest Path A directed graph G = ( V, E ) and a pair - - PDF document

single source shortest path
SMART_READER_LITE
LIVE PREVIEW

Single Source Shortest Path A directed graph G = ( V, E ) and a pair - - PDF document

Single Source Shortest Path A directed graph G = ( V, E ) and a pair of nodes s, d is given. The edges have a real-valued weight W i . This time we are looking for the weight and the shortest path from s to d . The weight of the shortest path


slide-1
SLIDE 1

Single Source Shortest Path

A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight Wi. This time we are looking for the weight and the shortest path from s to d.

  • The weight of the shortest path from s to d is called δ(s, d).
  • We still have the property that a shortest path between u and

v contains shortest path between intermediate nodes.

  • The shortest path between s and d might not be defined if the

graph contains cycles with negative weight.

  • A shortest path can not contain s positive weight cycle. Hence,

path length is at most n − 1.

SSSP 1

slide-2
SLIDE 2

Shortest Path Tree

A shortest path tree G′ = (V ′, E′) is a directed subgraph of G (V ′ ⊂ V and E′ ⊂ E), such that

  • 1. V ′ is the set of nodes reachable from s in G.
  • 2. G′ forms a rooted tree with root s, and
  • 3. For all v ∈ V ′, the unique simple path from s to v in G′ is a

shortest path from s to v in G. Shortest paths and shortest path trees are not unique!

SSSP 2

slide-3
SLIDE 3

Relaxation

  • For each vertex v ∈ V we maintain an attribute d[v], which is

an upper bound on the weight of a shortest path from source s to v. The attribute is called shortest path estimate.

  • In the beginning, d[v] = ∞ for v = s and d[s] = 0.
  • We also use variables π[v] storing the predecessor of v.
  • In a relaxation step we may decrease the shortest path estimate

and update the π values.

SSSP 3

slide-4
SLIDE 4

Relaxation (u, v, w)

If d[v] > d[u] + w[u, v], then d[v] := d[u] + w(u, v) and π[v] = u The path that uses u as a last edge is shorter than the one we found previously. Bellman-Ford(G, w, s) Initialise-Single-Source(G, s) for i ← 1 to |V [G]| − 1 do for each edge (u, v) ∈ E[G] do Relax(u, v, w) for each edge (u, v) ∈ E[G] do if d[v] > d[u] + w(u, v) then return False return True

SSSP 4

slide-5
SLIDE 5

Runtime and Correctness

  • The runtime is easy. In every iteration of the for-loop we relax

every edge. We need |V | − 1 passes over all edges. This gives a runtime of O(V E).

  • First we show the following: if there are no negative-weight

cycles, the algorithm computes correct shortest path weights for all vertices reachable from s.

  • But to do that we need a couple of other results,...

Lemma 1 Let G = (V, E) be a weighted directed graph with source s and weight function w : E − → R, and assume that G contains no negative-weight cycle that is reachable from s. Then, after |V | − 1 iterations of the first for-loop of the Bellman-Ford, we have d[v] = δ(s, v) for all vertices v that are reachable from s.

SSSP 5

slide-6
SLIDE 6

Upper bound property

Lemma 2 Let G = (V, E) be a weighted, directed graph with weight function w : E − → R, and let s ∈ V be a source vertex. Assume G is initialised as we defined. Then d[v] ≥ δ(s, v) for all v ∈ V , and this invariant is maintained over any sequence of relaxation steps. Moreover, once d[v] achieves its lower bound δ(s, v), it never changes.

SSSP 6

slide-7
SLIDE 7

Proof of Lemma 2

  • We prove the invariant d[v] ≥ δ(s, v) by induction over the

number of relaxation steps.

  • After the in initialisation d[v] ≥ δ(s, v) is certainly true.
  • For the inductive step, consider the relaxation of an edge (u, v).

By the inductive hypothesis, d[x] ≥ δ(s, x) for all x ∈ V prior to the relaxation.

  • If d[v] changes we have

d[v] = d[u] + w(u, v) ≥ δ(s, u) + w(u, v) ≥ δ(s, v).

  • To see that the value of d[v] never changes once d[v] = δ(s, v)

have a look at the above equations again. We have just shown d[v] ≥ δ(s, v). It can not increase since relaxation steps do not increase values.

SSSP 7

slide-8
SLIDE 8

Lemma 3: Convergence property Let G = (V, E) be a

weighted, directed graph with weight function w : E − → R, and let s ∈ V be a source vertex. Let s = ⇒ u → v be a shortest path in G for some vertices u, v ∈ V . Suppose that G is initialised as we

  • defined. Then, a sequence of relaxation steps that includes the call

RELAX(u, v, w) is executed on the edges of G. If 4d[u] = δ(s, u) at any time prior to the call, then d[v] = δ(s, v) at all times after the call.

SSSP 8

slide-9
SLIDE 9

Proof of Lemma 3

  • By the upper bound property: if d[u] = δ(s, u) at some

point prior to relaxing edge (u, v), then this equality holds thereafter.

  • After relaxing edge (u, v) we have we have

d[v] ≤ d[u] + w(u, v) = δ(s, u) + w(u, v) = δ(s, v).

  • The first inequality is due to the fact that, after relaxing edge

(u, v), we have d[v] ≤ d[u] + w(u, v).

  • The last equality is due to the fact that subpath of shortest

path are also shortest paths.

  • By the upper bound property, d[v] ≥ δ(s, v), from which we

can conclude that d[v] = δ(s, v), and this equality is maintained thereafter.

SSSP 9

slide-10
SLIDE 10

Path-relaxation Property

Lemma 4 Let G = (V, E) be a weighted, directed graph with weight function w : E − → R, and let s ∈ V be a source vertex. Consider any shortest path p = (v1, v2, . . . , vk) from s = v0 to vk. If G is initialised as we defined, and then a sequence of relaxation steps occurs that includes, in order, relaxations of the edges (v0, v1), (v1, v2), . . . (vk−1, vk), then d[vk] = δ(s, vk). This holds no matter which other edge relaxations occur.

SSSP 10

slide-11
SLIDE 11

Proof of Lemma 4

  • We show by induction that after the ith edge of path p is

relaxed, we have d[vi] = δ(s, vi).

  • For the basis, i = 0 we have from the initialisation

d[v0] = d[s] = 0 = δ(s, s).

  • By the upper bound property, the value of d[s] is never

changes after initialisation.

  • For the inductive step, we assume that d[vi−1] = δ(s, vi−1), and

we examine the relation of edge (vi−1, vi).

  • By the convergence property, we have after the relation

d[vi] = δ(v, vi). Again, this equality is maintained thereafter.

SSSP 11

slide-12
SLIDE 12

Proof of Lemma 1

  • Consider a vertex v that is reachable from s. Let

p = (v0, v1, . . . vk) (with v0 = s and vk = v) be a shortest acyclic path from s to v.

  • The path has at most |V | − 1 edges, giving k ≤ |V | − 1.
  • Each of the iterations of the for loop relaxes all edges of E.

Among the edges relaxed in the ith iteration we have the edge (vi−1, vi).

  • By the relaxation property we have

d[v] = d[vk] = δ(s, vk) = δ(v, v)

SSSP 12

slide-13
SLIDE 13

Predecessor-subpath property

Lemma 5 Let G = (V, E) be a weighted, directed graph with weight function w : E − → R, and let s ∈ V be a source vertex. Assume that G does not contain a negative cycle reachable from s. Then, if we execute relaxation steps resulting in d[v] = δ(s, v) for all v ∈ V . Then the predecessor subgraph Gπ is a shortest-path tree rooted at s.

SSSP 13

slide-14
SLIDE 14

Proof of Lemma 5

We have to show that all three shortest-paths tree properties hold.

  • The vertices that are reachable from s are the ones with finite

d[v]. A vertex v ∈ V − {s} has s finite value d[v] if and only if π[v] = nil. Thus, the vertices in Vπ are those reachable from s.

  • It remains to show that the unique path in Gπ are shortest
  • paths. Let p = (s = v0, v1, . . . vk = v) be a path in Gπ.
  • For i = 1, 2, . . . k we have both d[vi] = δ(s, vi) and

d[vi] ≥ d[vi−1] + w(vi−1, vi). Hence, w(vi−1, vi) ≤ δ(s, vi) − δ(s, vi−1).

  • Summing the weights along p yields

w(p) = k

i=1 w(vi−1, vi) ≤ k i=1(δ(s, vi) − δ(s, vi−1))) =

δ(s, vk) − δ(s, v0) = δ(s, vk)

  • Thus, w(p) ≤ δ(s, vk) and we can conclude w(p) = δ(s, vk)

SSSP 14

slide-15
SLIDE 15

since δ(s, vk) is a lower bound. p is a shortest path.

SSSP 15

slide-16
SLIDE 16

Correctness of Bellman-Ford

Lemma 6 Let G = (V, E) be a weighted, directed graph with weight function w : E − → R, and let s ∈ V be a source vertex. Consider any shortest path p = (v1, v2, . . . , vk) from s = v0 to vk.

  • 1. If G contains no negative cycles that are reachable from s, then

the algorithm returns true and we have d[v] = δ(s, v) for all vertices v ∈ V and the Bellman-Ford algorithm computes a shortest path tree rooted at s.

  • 2. If G does contains a negative weight cycle reachable from s,

then the algorithm returns false.

SSSP 16

slide-17
SLIDE 17

Proof of Lemma 6

  • Suppose the graph does not contain a negative cycle that is

reachable for s. – If a vertex v is reachable from s, then Lemma 1 shows that d[v] = δ(s, v). – In v is not reachable from v, d[v] = δ(s, v) = ∞. This follows from the upper-bound property since we always have ∞ = δ(u, v) ≤ d[v], Hence, we have d[v] = ∞. – The predecessor-subgraph property implies that Gπ is a shortest-path tree. – It remains to show that the Bellman-Ford returns true. At termination we have for all edges (u, v) ∈ E d[v] = δ(s, v) ≤ δ(s, u) + w(u, v) = d[u] + w(u, v)

SSSP 17

slide-18
SLIDE 18

Proof of Lemma 6 cont

  • Suppose the graph does contain a negative cycle

c = (v0, v1, . . . , vk) (v0 = vk) that is reachable for s. – Assume for contradiction that Bellman-Ford returns true. – Thus, d[vi] ≤ d[vi−1] + w(vi−1, vi) for 1 ≤ i ≤ k and k

i=1 w(vi−1, vi) < 0.

– k

i=1 d[vi] ≤ k i=1(d[vi−1] + w(vi−1, vi)) =

k

i=1 d[vi−1] + k i=1 w(vi−1, vi).

– Each vertex appears exactly once in each of the above summations and k

i=1 d[vi] = k i=1 d[vi−1].

– This gives 0 < k

i=1 w(vi−1, vi) and a contradiction.

SSSP 18

slide-19
SLIDE 19

Dijkstra’s Algorithm

  • The algorithm solves the single-source shortest path problem

for the case of non-negative edge weight.

  • It maintains a set of vertices S whose final shortest path has

already been determined.

  • It repeatedly selects the vertex u ∈ V − S with minimum

shortest pats estimate adds it to S, and relaxes all edges leaving u.

  • We will use a min-priority queue Q for the vertices, keyed by

their d values.

SSSP 19

slide-20
SLIDE 20

Dijkstra’s Algorithm

Dijkstra(G, w, s) Initialise-Single-Source(G, s) S := ∅ and Q := V [G] while Q = ∅ do u := Extract − Min(Q) S := S ∪ {u} for each vertex v adjacent to u do Relax(u,v,w)

SSSP 20

slide-21
SLIDE 21

Idea of Dijkstra’s Algorithm

  • The algorithm maintains the invariant that Q = V − S that

holds at the start of each iteration of the while-loop.

  • The invariant is certainly true at the beginning.
  • Each time we go through the while loop, a vertex u is extracted

from Q = V − S and added to S. u has the smallest shortest path estimate d[u].

  • Then each outgoing edge (u, v) is relaxed. The shortest path

estimate is updated if the shortest path to v can be improved by going through u.

  • Because Dijkstra’s algorithm always choses the lightest vertex

to add to set S, it is a greedy strategy.

  • Dijkstra calculates an optimal solution.

SSSP 21

slide-22
SLIDE 22

Idea of Dijkstra’s Algorithm II

  • The key of the correctness proof is to show that each time a

vertex u is added to S we have d[u] = δ(s, u).

  • The runtime is O((V + E) log V ).

SSSP 22