shortest-path problems single-source shortest path all-pair shortest path lecture 8 shortest path

28
Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Upload: nicholas-newton

Post on 15-Jan-2016

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

• Shortest-path problems

• Single-source shortest path

• All-pair shortest path

Lecture 8 Shortest Path

Page 2: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Overview

• Shortest-path problems

• Single-source shortest path algorithms

‣ Bellman-Ford algorithm

‣ Dijkstra algorithm

• All-Pair shortest path algorithms

‣ Floyd-Warshall algorithm

Page 3: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Weighted graph

Shanghai

Beijing

Guangzhou

QingdaoLhasa

•Single-source shortest path•All-pair shortest path

Page 4: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Shortest Path

Page 5: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Shortest Path

Optimal substructure: Subpaths of shortest paths are shortest paths.

Cycles. Can a shortest path contains cycles?

Negative weights.

Page 6: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Where are we?

• Shortest-path problems

• Single-source shortest path algorithms

‣ Bellman-Ford algorithm

‣ Dijkstra algorithm

• All-Pair shortest path algorithms

‣ Floyd-Warshall algorithm

Page 7: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Relaxing

The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u.

Page 8: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Shortest-path propertiesNotation: We fix the source to be s. λ[v]: the length of path computed by our algorithms from s to v. δ[v]: the length of the shortest path from s to v.

Triangle propertyδ[v] <= δ[u] + w(u,v) for any edge (u,v).

Upper-bound propertyδ[v] <= λ[v] for any vertex v.

Convergence propertyIf s⇒u→v is a shortest path, and if λ[u] = δ[u],

then after relax(u,v), we have λ[v] = δ[v].

Page 9: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Bellman-Ford algorithm

E = {(t,x),(t,y),(t,z),(x,t),(y,x),(y,z),(z,x),(z,s),(s,t),(s,y)}

Θ(mn)

Page 10: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Negative cycle

Page 11: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

CorrectnessCorrectness of Bellman-FordIf G contains no negative-weight cycles reachable from s, then the algorithm returns TRUE, and for all v, λ[v] = δ[v], otherwise the algorithm returns FALSE.

Proof. No negative cycle. By the fact that the length of simple paths is bounded by |V| - 1.After i-th iteration of relax, λ[vi] = δ[vi].

s vv1 Vj vi

Negative cycle. After i-th iteration of relax, λ[vi] + w(vi,vj) < λ[vj]. The length of negative cycle is at most |V|.

Page 12: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Observation 1

If there is no cycle (DAG), ...

Relax in topological order.

s vv1 vj vi Θ(m)

Page 13: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Weighted DAG application

seam

Page 14: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Observation 2

If there is no negative edge, ...

s v1

vj

vi

If (s, vi) is the lightest edge sourcing from s, then λ[vi] = δ[vi]

Page 15: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Dijkstra algorithm

Edsger Wybe Dijkstra in 2002

Page 16: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Dijkstra algorithm

Page 17: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path
Page 18: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Correctness

Assume u is chosen in Step 7, and1. λ[u] > δ[u] 2. s ⇒ x → y ⇒ u is the shortest path

δ[u] = δ[x] + w(x,y) + w(p2) = λ[x] + w(x,y) + w(p2) ≥ λ[x] + w(x,y) ≥ λ[y] ≥ λ[u]

Page 19: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Time complexity

Θ(n2)

Page 20: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

What is the time complexity? How about use d-heap?

Page 21: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Comparisons

Array Binary heap d-ary heap

Dijkstra O(n2) O(mlogn)O(dnlogdn +

mlogdn)

Page 22: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Dense graph

• dense: m = n1+ε, ε is not too small.• d-heap, d = m/n• complexity: O(dnlogdn + mlogdn) = O(m)

Page 23: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Where are we?

• Shortest-path problems

• Single-source shortest path algorithms

‣ Bellman-Ford algorithm

‣ Dijkstra algorithm

• All-Pair shortest path algorithms

‣ Floyd-Warshall algorithm

Page 24: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

All-pairs shortest path

Page 25: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Floyd-Warshall algorithm

Page 26: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Floyd-Warshall algorithm

Θ(n3)

Page 27: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

ConclusionDijkstra’s algorithm.

• Nearly linear-time when weights are nonnegative.Acyclic edge-weighted digraphs.

• Faster than Dijkstra’s algorithm.

• Negative weights are no problem.Negative weights and negative cycles.

• If no negative cycles, can find shortest paths via Bellman-Ford.

• If negative cycles, can find one via Bellman-Ford.All-pair shortest path.

• can be solved via Floyd-Warshall

• Floyd-Warshall can also compute the transitive closure of directed graph.

Page 28: Shortest-path problems Single-source shortest path All-pair shortest path Lecture 8 Shortest Path

Which of the following statements are true for shortest path?

♠. If you run Dijkstra's algorithm on an edge-weighted DAG with positive weights, the order in which the vertices are relaxed is a topological order.

♥. Let P be a shortest path from some s to t in an edge-weighted digraph G. If the weight of each edge in G is increased by one, then P will still be a shortest path from s to t in the modified digraph G'.

♣. Let G be a digraph with positive edge weights. Suppose that you increase the length of an edge by x. Then, the length of the shortest path from s to t can increase by more than x. ♦. Bellman-Ford finds the shortest simple path from s to every other vertex, even if the edge weights are positive or negative integers, provided there are no negative cycles.