Powerpoint exploring the locations used in television show Time Clash
My presentation all shortestpath
1. All-Pairs Shortest Path
Theory and Algorithms
Carlos Andres Theran Suarez
Program Mathematics and Scientific Computing
University of Puerto Rico
Carlos.theran@upr.edu
October – 2011
Mayaguez-Puerto Rico
Dr Marko Schütz
2. Introduction
• In this section we consider de problem of finding
shortest path between all pair of vertices in a directed
graph ������ = (������, ������).
– With a weight function ������: ������ → ℝ
������ ������ = ������−������ ������(������������ , ������������+������ ) where ������ ∈ ������ ������������������ ������ ∈ ℕ.
������=������
For this goal we are going to use the adjacency-matrix of a
graph.
• The input: is a ������ ������ ������ ������������������������������������ ������������������������������������ ������ = ������������������ which is
adjacency-matrix of a ������ = ������, ������ .
• The output: is the ������ ������ ������ matrix of SPL ������ ������, ������ , ∀������������ ∈ ������.
3. Recall
• Single-Source shortest paths.
• Nonnegative edge weight.
– Dijkstra’s algorithm: Running time Array ������ ������������
Running time Binary heap ������( ������ + ������ log ������)
Running time Fibonacci heap ������(������ + ������ log ������)
• General.
– Bellman-Ford: Running time ������ ������������
• UDG.
– Breadth for search: Running time ������ ������ + ������
4. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
5. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
All-pair shortest paths.
• Nonnegative edge weight
Dijkstra’s algorithm: Running time Array ������ ������������
6. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
All-pair shortest paths.
• Nonnegative edge weight
Dijkstra’s algorithm: Running time Array ������ ������������
Running time Binary heap ������( ������������ + ������������ log ������)
7. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
All-pair shortest paths.
• Nonnegative edge weight
Dijkstra’s algorithm: Running time Array ������ ������������
Running time Binary heap ������( ������������ + ������������ log ������)
Running time Binary heap ������ ������������ + ������������ log ������
8. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
All-pair shortest paths.
• Nonnegative edge weight
Dijkstra’s algorithm: Running time Array ������ ������������
Running time Binary heap ������( ������������ + ������������ log ������)
Running time Binary heap ������ ������������ + ������������ log ������
• General.
Bellman-Ford: Running time ������ ������������ ������
9. What do you think?
Can we solve all-pair shortest paths by running a
single source-paths algorithms?
All-pair shortest paths.
• Nonnegative edge weight
Dijkstra’s algorithm: Running time Array ������ ������������
Running time Binary heap ������( ������������ + ������������ log ������)
Running time Binary heap ������ ������������ + ������������ log ������
• General.
Bellman-Ford: Running time ������ ������������ ������
In a dense graph ������ ������������
10. Predecessor Matrix
Let ������ = ������������������ the predecessor Matrix, where
������������������ ������������ ������ = ������ ������������ ������������������������������ ������������ ������������ ������������������������ ������������������������ ������ ������������ ������
������������������ =
������������������ ������������������������������������������������������������������ ������������ ������ ������������ ������������������������ ������. ������ ������������������������ ������.
Now we define the predecessor subgraph of G for ������ as
������������,������ = (������������,������ , ������������,������ ).
Where ������������,������ = ������ ∈ ������: ������������������ ≠ ������������������ ∪ ������
������������,������ = ������������������ , ������ : ������ ∈ ������������,������ − {������}
12. Outline
1. Present a dynamic programming algorithms based on
matrix multiplication to solve the problem.
2. Dynamic programming algorithms called Floyd-Warshall
algorithms.
3. Unlike the others algorithms, Johnson's algorithms used
adjacency-list representation of a graph.
13. Shortest path and matrix multiplication
1. The structure of shortest path.
Let suppose that we have a shortest path ������ form vertix ������ to
vertex ������, and suppose that ������ have at most ������ < ∞ edge.
• If ������ = ������ then ������ have weight 0.
• If ������ ≠ ������ then ������ ↝������′ ������ → ������, where ������′ has at most ������ − ������ edge,
by lemma 24.1 ������′ is a shortest path from ������ ������������ ������.
So ������ ������, ������ = ������ ������, ������ + ������������������ .
14. Shortest path and matrix multiplication (cont.)
2. A recursive solution.
Let ������������������ (������) be the minimum weight of any path from vertex ������ to vertex
������ that contains at most ������ edge.
(������) ������ ������������ ������ = ������
• ������������������ =
∞ ������������ ������ ≠ ������
• ������������������ (������) = min(������������������ ������−������
, min ������������������ (������−������) + ������������������ )
1≤������≤������
(������−������)
= min ������������������ + ������������������
1≤������≤������
������ ������, ������ = ������������������ (������−������) = ������������������ (������) = ������������������ (������+������) = ⋯
16. Shortest path and matrix multiplication (cont.)
• Now we can see the relation to the matrix multiplication.
Let ������ = ������ ∗ ������ the matrix product of ������������������. For ������, ������ = ������, … , ������.
We have ������������������ = ������ ������������������ ∗ ������������������ .
������=������
If we set; ������(������−������) → ������ ������ → ������
������������ → ������ ������������������ → +
+→∗
������������������ ← ������������������ + ������������������ ∗ ������������������
18. Shortest path and matrix multiplication (cont.)
• Improving the running time.
Our goal, is to compute ������������−������ matrices, let go to see that we
can compute ������������−������ with only log( ������ − ������) matrix product.
21. The Floyd-Warshall algorithm
The algorithm consider a intermediate vertices of a shortest
path.
1. The structure of a shortest path.
Intermediate vertex ������ =< ������������ , ������������ , … , ������������ > in a any vertex of ������
other than ������������ or ������������ , so it can be the set ������������ , … , ������������−������ .
Let assume that the vertex of ������ are ������ = ������, ������, … , ������ and a
subset ������, ������, … , ������ for some ������.
• If ������ ∉ ������������������ of path ������, then all the vertices intermediate ������
are in the set ������, ������, … , ������ − ������ . Thus, a shortest path from
vertex ������ to vertex ������ with all intermediate vertices in the set
������, ������, … , ������ − ������ is also a shortest path form ������ to ������ with all
������������������ in the set ������, ������, … , ������ .
22. The Floyd-Warshall algorithm (cont)
• If ������ ∈ ������������������ of path ������, we break ������ down into ������ ↝������������ ������ ↝������������ ������.
������������ is a shortest path from ������ to ������, so ������ ∉ ������������������ of ������������ , thus ������������ is
a shortest path form ������ to ������ with all ������������ in the set
������, ������, … , ������ − ������ . Similarly ������������ is a shortest path form ������ to ������
with all ������������ in the set ������, ������, … , ������ − ������ .
23. The Floyd-Warshall algorithm (cont)
3. A recursive solution.
������������������ ������������ ������ = ������
(������)
������������������ = ������−������ ������−������ ������−������
min ������������������ , ������������������ + ������������������ ������������ ������ ≥ ������
Since for every path, all intermediate vertices are in the set
������, ������, … , ������ , matriz ������(������) = (������������������ (������) ) gives the final answer:
������������������ (������) = ������ ������, ������ .
24. The Floyd-Warshall algorithm (cont)
• input: A ������ ������ ������ matrix ������
• output: A ������ ������ ������ matrix ������(������) of shortest path weight.
• ������������������ (������) = min ������������������ ������−������
, ������������������ ������−������
+ ������������������ ������−������
26. The Floyd-Warshall algorithm (cont)
4. Constructing a Shortest path
We compute the predecessor matrix ������ just as the Floyd-warshall algorithm
compute the matrices ������(������) .
so ������ = ������(������) = (������������������ (������) ).
Recursive formulation.
������−������ ������−������ ������−������
������������������ (������) ������������ ������������������ ≤ ������������������ + ������������������
������������������ (������) = ������−������ ������−������ ������−������
������������������ (������) ������������ ������������������ > ������������������ + ������������������
(������)
������������������ ������������ ������ = ������ ������������ ������������������ = ∞.
������������������ =
������ ������������ ������ ≠ ������ ������������������ ������������������ < ∞.
27. Johnson's algorithm for sparse graphs.
• It is asymtoticaly better than repeated squaring of matrices
or the Floyd-Warshall algoritm.
• It use a subroutine both Dijkstra’s algorithm and Bellman-
Ford algorithm.
• Johnson's algorithm use the technique of reweighting.
28. Johnson's algorithm for sparse graphs (cont.).
Reweighting
If ������ has a negative weight edge but no negative weight cycle,
we compute a new set of nonnegative edge weight ������ that
allow as to use Dijkstra’s algorithm.
The new set of edge must satisfy two condition.
1. ������(������) is a shortest path form ������ ������ ������ ⇔ ������(������) is a shortest
path form ������ ������ ������ .
2. For all edges (������, ������), the new weight ������(������, ������) is
nonnegative.
29. Johnson's algorithm for sparse graphs (cont.).
• Lemma
Give a weighted, directed graph ������ = (������, ������) with weight
funtion ������: ������ → ℝ be any funtion mapping vertices to real
numbers. For each edge (������, ������) ∈ ������, define.
������ ������, ������ = ������ ������, ������ + ������ ������ − ������ ������ .
30. Johnson's algorithm for sparse graphs (cont.).
• Producing no negative weight by reweighting