Finding Shortest Paths Shortest Path Problem Shortest Path Problem - - PowerPoint PPT Presentation
Finding Shortest Paths Shortest Path Problem Shortest Path Problem - - PowerPoint PPT Presentation
Finding Shortest Paths Shortest Path Problem Shortest Path Problem Given a graph G = ( V , E ) and an edge weight function : V R . Length of a Path The length or weight ( P ) of a path P = { v 1 , v 2 , . . . , v l } with at least two
Shortest Path Problem
Shortest Path Problem
Given a graph G = (V, E) and an edge weight function ω: V → R. Length of a Path The length or weight ω(P) of a path P = {v1, v2, . . . , vl} with at least two vertices is ω(P) =
l−1
- i=1
ω(vivi+1) If |P| = 1, ω(P) = 0. Shortest Path Problem For two vertices u and v, the shortest path from u to v is the path P for which ω(P) is minimal. The distance d(u, v) from u to v is the length of a shortest path from u to v.
3 / 24
Shortest Path Problem
Variants
◮ Single Pair Shortest Path (SPSP)
Find a shortest path from a vertex u to some vertex v.
◮ Single Source Shortest Path (SSSP)
Find shortest paths from a source vertex v to all other vertices in the graph.
◮ All Pairs Shortest Path (APSP)
Find shortest paths fall vertex pairs u and v. There is no algorithm for SPSP which is in general better than a SSSP algorithm.
4 / 24
Shortest Path Properties
Theorem Each subpath of a shortest path is a shortest path. (Optimal Substructure Property) Theorem For all vertices u, v, and w, d(u, v) ≤ d(u, w) + d(w, v). (Triangle Inequality)
5 / 24
Negative Weight Edges and Cycles
Negative Cycles
◮ Natural in some application ◮ Makes finding a shortest path harder
Theorem If there is a path from u to v containing a vertex w and w is in a cycle C with ω(C) < 0, then there is no shortest path from u to v. Avoiding Cycles
◮ Only permit simple cycles, i. e., no vertex twice ◮ Follows if graph has no negative cycles ◮ With negative cycles, shortest path problem equal to longest path
problem
◮ Optimal Substructure Property no longer given
6 / 24
General Approach
General Approach
Store for each vertex v
◮ dists(v), length of currently best known path P from start vertex s
to v
◮ pars(v), parent of v in P
Relaxation
◮ Updates best known distance.
1 Procedure Relax(u, v) 2
If dists(v) > dists(u) + ω(uv) Then
3
Set par(v) := u and dists(v) := dists(u) + ω(uv).
8 / 24
General Approach
Initialization
◮ Set par(v) := null and dists(v) := ∞ for each vertex v. ◮ Set dists(s) := 0 for start vertex s.
Iteration
◮ Pick vertex pair u, v. ◮ Call Relax(u, v) ◮ Repeat
Open questions
◮ How do we pick u and v? ◮ When do we stop the iteration?
9 / 24
Single Source Shortest Path
Shortest Path for DAGs
Directed Acyclic Graphs
◮ No (negative) cycles ◮ Topological order
Algorithm Idea
◮ Find a topological order v1, v2, . . . , vn. ◮ For i := 1 to n, relax all outgoing edges of vi.
Properties
◮ Invariant: For all vj with j ≤ i, dist(vj) is optimal. ◮ Runtime: linear ◮ Works with negative edges, i. e., can be used to compute longest path.
11 / 24
Bellman-Ford
Observation
◮ A shortest path has at most |V| − 1 edges. ◮ If we know all shortest path with k edges, we can compute all
shortest with k + 1 edges by relaxing all edges once.
1 For Each v ∈ V 2
Set dist(v) := ∞ and par(v) = null.
3 Set dist(s) := 0. 4 For i := 1 To |V| − 1 5
For Each (u, v) ∈ E
6
Relax(u, v)
12 / 24
Bellman-Ford
Properties
◮ Runtime: O(|V||E|) ◮ Works with negative weight edges ◮ Can detect negative cycles
Detecting negative cycles
◮ Negative cycle → There is always an edge (u, v) for which Relax(u, v)
updates dist(v).
◮ If Relax(u, v) still updates dist(v) for i ≥ |V|, then (u, v) is part of a
negative cycle.
13 / 24
Dijkstra’s Algorithm
Idea
◮ Let S be set of vertices where shortest path is known. ◮ Relax all outgoing edges (u, v), i. e., u ∈ S and v /
∈ S.
◮ If dist(v) is minimal for all vertices not in S, then dist(v) is optimal. ◮ Add v to S and repeat.
1 Initialize(G, s) 2 Create priority Q and add all vertices in V. 3 While Q is not empty 4
Remove v with minimal dist(v) from Q.
5
For Each (v, w) ∈ E
6
Relax(v,w)
14 / 24
Dijkstra’s Algorithm
Properties
◮ Runtime: O(|E| log |V|) with binary heaps and O(|V| log |V| + |E|)
with Fibonacci-Heaps
◮ Invariant: For all vertices in S, dist(s) is optimal. ◮ Requirement: No negative edges. The algorithm assumes that
distances are always increasing. What happens if there are negative edges? 1 2
1 2
- 3
Dijkstra
- 1
1
1 2
- 3
Bellman-Ford
15 / 24
All Pairs Shortest Path
Floyd-Warshall
Idea
◮ Assume we know the shortest path from vi to vj using only the
(additional) vertices v1, v2, . . . , vk−1. Let d(k−1)
ij
be this distance.
◮ Then, we can add vk in the next iteration and get
d(k)
ij
= min
- d(k−1)
ij
, d(k−1)
ik
+ d(k−1)
kj
- ◮ If k = |V|, then d(k)
ij
= d(vi, vj) for all i and j.
◮ Initial values
d(0)
ij
= if i = j ω(vivj) if vivj ∈ E ∞ else
17 / 24
Floyd-Warshall
1 For Each pair i, j with 1 ≤ i, j ≤ |V| 2
Set d(0)
ij
:= 0 if i = j, ω(vivj) if vivj ∈ E, and ∞ otherwise.
3 For k := 1 To |V| 4
For Each pair i, j with 1 ≤ i, j ≤ |V|
5
Set d(k)
ij
= min
- d(k−1)
ij
, d(k−1)
ik
+ d(k−1)
kj
- .
To represent dij, use two |V| × |V| arrays. Detecting negative cycles
◮ Check if, for some i and some k, d(k) ii
< 0. Runtime: O(|V|3)
18 / 24
Dijkstra vs. Floyd-Warshall
Runtime for APSP
◮ Dijkstra: O(|V|2 log |V| + |V||E|) ◮ Floyd-Warshall: O(|V|3)
Observation
◮ Since |E| ≤ |V|2, Dijkstra would be better, especially for sparse
graphs.
◮ Problem: negative weight edges.
Question
◮ Is there a way to avoid these negative edges?
19 / 24
Johnson’s Algorithm
Algorithm
◮ Add a new vertex q and add, for each v ∈ V, the directed edge qv
with weight 0.
◮ Run Bellman-Ford with start vertex q. Let h(v) be the length of a
shortest path from q to v.
◮ For each edge uv, set ˜
ω(uv) := ω(uv) + h(u) − h(v).
◮ Remove q and run Dijkstra’s algorithm on each vertex using ˜
ω as edge weights. Properties
◮ Runtime O(|V|2 log |V| + |V||E|) ◮ Works with negative weight edges and can detect negative cycles.
20 / 24
A* and Branch and Bound
Single Pair Shortest Path
Single Pair Shortest Path
◮ Weighted graph ◮ Find shortest path from s to t.
Dijkstra
◮ Explores all in distance d(s, t) before terminating.
(Can be improved to d(s, t)/2 with bidirectional search)
◮ Next vertex is selected by distance from s.
Problem
◮ Some vertices go in the wrong direction.
22 / 24
A*
Idea
◮ For a vertex v, make an estimation ht(v) of d(v, t) ◮ Important: ht(v) ≤ d(v, t)
s v t dists(v) ht(v) Algorithm
◮ Basically Dijkstra ◮ For next iteration, pick vertex v for which dists(v) + ht(v) is minimal.
23 / 24
Generalised A*
Idea
◮ Take decision tree. ◮ Find a shortest path from root to leaf. ◮ Important: Do not construct whole tree. Only construct explored
parts. Branch and Bound
◮ Start at root. ◮ Branch: Determine the children of a node. ◮ Bound: Compute for every node a lower bound for the cost of the
solutions in this subtree.
◮ Select next node where estimated lower bound is minimal.
Note
◮ Finding the optimal lower bound (i. e., ht(v) = d(v, t)) is as hard as
solving the original problem.
24 / 24