SingleSource Shortest Paths SingleSource Shortest Path Problem Given

Single-Source Shortest Paths

Single-Source Shortest Path • Problem: Given a weighted directed graph G, find the minimumweight path from a given source vertex s to another vertex v ➢Shortest-path • minimum weight ➢Weight of path is sum of edges ➢e. g. , a road map • what is the shortest path from Jerusalem to Gaza?

Shortest Path Properties • Again, we have optimal substructure ➢the shortest path consists of shortest subpaths: ➢Proof: suppose some subpath is not a shortest path • • There must then exist a shorter subpath It could substitute the shorter subpath for a shorter path But then overall path is not shortest path Contradiction

Shortest Path Properties • Define δ(u, v) to be the weight of the shortest path from u to v • Shortest paths satisfy the triangle inequality �δ(u, v) ≤ δ(u, x) + δ(x, v) x u v This path is no longer than any other path

Shortest Path Problems • Input: t 3 ➢Directed graph G = (V, E) 3 ➢Weight function w : E → R s 0 • Weight of path p = 〈v 0, v 1, . . . , vk〉 1 2 5 y 6 Shortest-path weight from u to v ➢ δ (u, v) = min w(p) : u ∞ p 9 4 3 5 x 6 v if there exists a path from u to v otherwise ➢Shortest path from u to v is any path p such that w(p) = δ(u, v) 2 7 11 z
![Shortest Path Representation • d[v] = δ(s, v): a shortest path estimate t ➢Initially, Shortest Path Representation • d[v] = δ(s, v): a shortest path estimate t ➢Initially,](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-6.jpg)
Shortest Path Representation • d[v] = δ(s, v): a shortest path estimate t ➢Initially, d[v]=∞ ➢Reduces as algorithms progress • π[v] = predecessor of v on a shortest path from s ➢If no predecessor, π[v] = NIL ➢π produces a tree : shortest path tree 3 3 s 0 3 5 5 y 9 4 1 2 x 6 6 • Shortest paths & shortest path trees are not unique 2 7 11 z

Negative-weight edges

Cycles Shortest paths can’t contain cycles: • Already ruled out negative-weight cycles. • Positive-weight ⇒we can get a shorter path by omitting the cycle. • Zero-weight: no reason to use them ⇒ assume that our solutions won’t use them.
![Output of single-source shortest-path algorithm For each vertex v ∈ V: • d[v] = Output of single-source shortest-path algorithm For each vertex v ∈ V: • d[v] =](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-9.jpg)
Output of single-source shortest-path algorithm For each vertex v ∈ V: • d[v] = δ(s, v). • Initially, d[v]=∞. • Reduces as algorithms progress. But always maintain d[v] ≥ δ(s, v). • Call d[v] a shortest-path estimate. • π[v] = predecessor of v on a shortest path from s. • If no predecessor, π[v] = NIL. • π induces a tree -- shortest-path tree.

Shortest-path tree.

Initialization

Relaxation • A key technique in shortest path algorithms is relaxation ➢Idea: for all v, maintain upper bound d[v] on δ(s, v) Relax(u, v, w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; } 5 2 9 5 2 Relax 5 2 6 Relax 7 5 2 6

Relaxation • Relaxing an edge (u, v) ➢Testing whether we can improve the shortest path to v found so far by going through u • If d[v] > d[u] + w(u, v) we can improve the shortest path to v ⇒ update d[v] and π[v] s s u 5 2 v u 9 5 2 RELAX(u, v, w) u 5 2 v 6 RELAX(u, v, w) v u 7 5 2 v 6

Relaxation
![Upper bound property Always have d[v] ≥ δ(s, v) for all v. Once d[v] Upper bound property Always have d[v] ≥ δ(s, v) for all v. Once d[v]](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-15.jpg)
Upper bound property Always have d[v] ≥ δ(s, v) for all v. Once d[v] = δ(s, v), it never changes. Proof Initially true. Suppose there exists a vertex such that d[v] < δ(s, v). Without loss of generality, v is first vertex for which this happens. Let u be the vertex that causes d[v] to change. Then d[v] = d[u] + w(u, v). • So, d[v] < δ(s, v) ≤ δ(s, u) + w(u, v) (triangle inequality) ≤ d[u] + w(u, v) (v is first violation) ⇒ d[v] < d[u] + w(u, v). Contradicts d[v] = d[u] + w(u, v). Once d[v] reaches δ(s, v), it never goes lower. It never goes up, since relaxations only lower shortest-path estimates.

Some more properties

Path relaxation property
![Bellman-Ford Algorithm t • Single-source shortest paths problem Computes d[v] and π[v] for all Bellman-Ford Algorithm t • Single-source shortest paths problem Computes d[v] and π[v] for all](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-18.jpg)
Bellman-Ford Algorithm t • Single-source shortest paths problem Computes d[v] and π[v] for all v ∈ V • It allows negative edge weights • Returns: x -2 4 -3 -4 7 2 2 6 8 s 0 7 7 y 9 ➢ TRUE if no negative-weight cycles are reachable from the source s ➢FALSE otherwise ⇒ no solution exists • Idea: ➢ Traverse all the edges |V| – 1 times, every time performing a relaxation step of each edge − 2 z
![Bellman-Ford Algorithm Initialize d[], which will converge to shortest-path value δ Relaxation: Make |V|-1 Bellman-Ford Algorithm Initialize d[], which will converge to shortest-path value δ Relaxation: Make |V|-1](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-19.jpg)
Bellman-Ford Algorithm Initialize d[], which will converge to shortest-path value δ Relaxation: Make |V|-1 passes, relaxing each edge Test for solution Under what condition do we get a solution?

Bellman-Ford Algorithm e. g.

Proof • guaranteed to converge after |V| − 1 passes, assuming no negativeweight cycles.

How about the TRUE/FALSE return value? Suppose there is no negative-weight cycle reachable from s. At termination, for all (u, v) ∈ E, d[v] = δ(s, v) ≤ δ(s, u) + w(u, v) (triangle inequality) = d[u] + w(u, v). So BELLMAN-FORD returns TRUE.

How about the TRUE/FALSE return value?

Single-source shortest paths in a directed acyclic graph

Dijkstra’s Algorithm • Single-source shortest path problem: ➢No negative-weight edges: w(u, v) > 0 (u, v) ∈E • Maintains two sets of vertices: S = vertices whose final shortest-path weights have already been determined C = vertices in V – S 0 u 3 5 • Repeatedly select a vertex u ∈V – S, with the minimum shortest-path estimate d[v] 1 10 8 2 5 v x 9 9 7 4 2 6 7 y

Dijkstra’s Algorithm • Input ➢A digraph G(V, E) where edges are associated with non-negative weight (cost) and a source src • Output ➢Lengths of shortest paths from src to each node in G • Idea : without loss of general ➢V = { 1, 2, …, n } where 1 is a source node ➢We have two sets • S : set of nodes already chosen ( S = { 1 } ) • C : set of remaining node ( C = { 2, 3, …, n } ) • D[1, 2, …, n] : containing costs of shortest path

Dijkstra’s Algorithm • Repeatedly add a node v in C to S whose distance to 1 (source) is minimal until S = {1, 2, …, n}

Dijkstra’s Algorithm e. g.

Dijkstra’s Algorithm e. g.
![Correctness Loop invariant: At the start of each iteration of the while loop, d[v] Correctness Loop invariant: At the start of each iteration of the while loop, d[v]](http://slidetodoc.com/presentation_image_h/076d571189f1b0d7f083cc1236d033a1/image-30.jpg)
Correctness Loop invariant: At the start of each iteration of the while loop, d[v] = δ(s, v) for all v ∈ S. • Initialization: Initially, S = ∅, so trivially true. • Termination: At end, Q = ∅⇒S = V ⇒d[v] = δ(s, v) for all v ∈ V. • Maintenance: Need to show that d[u] = δ(s, u) when u is added to S in each iteration.

Maintenance

Maintenance

Difference constraints

Difference constraints

Theorem Given a system of difference constraints, let G = (V, E) be the corresponding constraint graph. 1. If G has no negative-weight cycles, then x = (δ(v 0, v 1), δ(v 0, v 2), . . . , δ(v 0, vn)) is a feasible solution. 2. If G has a negative-weight cycle, then there is no feasible solution

Proof

Proof (1)

How to find a feasible solution
- Slides: 38