All Pair Shortest Path
Presented By
Shubham Pandey
(220586)
All Pair shortest Path
Introduction
You must have heard about Dijkstra’s algorithm which finds out the shortest path
between a given node and all other nodes in a graph with non-negative edge
weights. Bellman Ford algorithm also serves the same purpose but in all graphs
irrespective of any conditions. In these algorithms, a given node serves as the central
node and the shortest paths to all other nodes in the graph are computed from this
node.
Let us extend this problem to something like this: Calculate the shortest path from
each node to all other nodes in the graph.
What is All Pair Shortest Path problem?
The literal meaning of this problem is that we want to compute the shortest path
from every node in the graph to every other node. Putting it formally:
Let G = (V, E) where,
G represents the given graph,
V is the set of all vertices in the graph G
E represents the set of all edges in the graph G
The All Pair Shortest Path problem wants to compute the shortest path from each
vertex v ∈ V to every other vertex u ∈ V.
Requirement of All Pair Shortest Path
There are various scenarios where we can apply the All Pair Shortest Path approach
to reach the solution. Let us see some of them:
1. This algorithm is used in finding the transitive closure of directed graphs.
2. Inversion of real matrices
3. To check if a given graph is bipartite or not.
4. Faster computation of PathFinder Networks
Algorithms to Find All Pair Shortest Path
There are mainly two algorithms to compute all pair shortest paths in a given graph.
We have elaborate articles explaining each of these algorithms. These are
Floyd Warshall Algorithm
The Floyd Warshall Algorithm is for solving all pairs of shortest-path problems. The
problem is to find the shortest distances between every pair of vertices in a given
edge-weighted directed Graph.
This algorithm follows the dynamic programming approach to find the shortest path.
How Floyd-Warshall Algorithm Works?
Let the given graph be:
Follow the steps below to find the shortest path between all the pairs of vertices.
1.Create a matrix A0 of dimension n*n where n is the number of vertices. The row
and the column are indexed as i and j respectively. i and j are the vertices of the
graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If
there is no path from ith vertex to jth vertex, the cell is left as infinity.
A4 gives the shortest path between each pair of vertices
.
Floyd Warshall Algorithm:
Time Complexity: O(n3)
cost [i][j]= cost Mat [i] [j];
for(k=0; k<N; k++)
{
for(i=0; i<N; i++)
for(j=0; j<N; j++)
if(cost [i][j]> cost [i] [k] + cost [k][j];
cost [i][j]=cost [i] [k]+'cost [k] [i]:
}
void floydwarshall()
{
int cost [N][N];
int i, j, k;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
Johnson’s algorithm for All-pairs shortest paths
The problem is to find the shortest paths between every pair of vertices in a given
weighted directed Graph and weights may be negative.
The time complexity of the Floyd Warshall Algorithm is Θ(V3).
Using Johnson’s algorithm, we can find all pair shortest paths in O(V2log V + VE)
time. Johnson’s algorithm uses both Dijkstra and Bellman-Ford as subroutines. If we
apply Dijkstra’s Single Source shortest path algorithm for every vertex, considering
every vertex as the source, we can find all pair shortest paths in O(V*VLogV) time
So using Dijkstra’s single-source shortest path seems to be a better option than Floyd
Warshall’s Algorithm , but the problem with Dijkstra’s algorithm is, that it doesn’t
work for negative weight edge. The idea of Johnson’s algorithm is to re-weight all
edges and make them all positive, then apply Dijkstra’s algorithm for every vertex.
Example: Let us consider the following graph
We add a source s and add edges from s to all vertices of the original graph. In the following diagram s is
4
We calculate the shortest distances from 4 to all other vertices using Bellman-Ford
algorithm. The shortest distances from 4 to 0, 1, 2 and 3 are 0, -5, -1 and 0
respectively, i.e., h[] = {0, -5, -1, 0}. Once we get these distances, we remove the
source vertex 4 and reweight the edges using following formula. w(u, v) = w(u, v) +
h[u] – h[v]
Algorithm:
1.Let the given graph be G. Add a new vertex s to the graph, add edges from the
new vertex to all vertices of G. Let the modified graph be G’
2. Run the Bellman-Ford algorithm on G’ with s as the source. Let the distances
calculated by Bellman-Ford be h[0], h[1], .. h[V-1]. If we find a negative weight
cycle, then return. Note that the negative weight cycle cannot be created by new
vertex s as there is no edge to s. All edges are from s.
3. Reweight the edges of the original graph. For each edge (u, v), assign the new
weight as “original weight + h[u] – h[v]”.
4. Remove the added vertex s and run Dijkstra’s algorithm for every vertex.
Time Complexity:
The main steps in the algorithm are Bellman-Ford Algorithm called once and Dijkstra called V times.
Time complexity of Bellman Ford is O(VE) and time complexity of Dijkstra is O(VLogV).
So overall time complexity is O(V2log V + VE).
The time complexity of Johnson’s algorithm becomes the same as Floyd Warshall’s Algorithm when
the graph is complete (For a complete graph E = O(V2).
But for sparse graphs, the algorithm performs much better than Floyd Warshall’s Algorithm.
THANK
YOU

DAA_Presentation - Copy.pptx

  • 1.
    All Pair ShortestPath Presented By Shubham Pandey (220586)
  • 2.
  • 3.
    Introduction You must haveheard about Dijkstra’s algorithm which finds out the shortest path between a given node and all other nodes in a graph with non-negative edge weights. Bellman Ford algorithm also serves the same purpose but in all graphs irrespective of any conditions. In these algorithms, a given node serves as the central node and the shortest paths to all other nodes in the graph are computed from this node. Let us extend this problem to something like this: Calculate the shortest path from each node to all other nodes in the graph. What is All Pair Shortest Path problem? The literal meaning of this problem is that we want to compute the shortest path from every node in the graph to every other node. Putting it formally: Let G = (V, E) where, G represents the given graph, V is the set of all vertices in the graph G E represents the set of all edges in the graph G The All Pair Shortest Path problem wants to compute the shortest path from each vertex v ∈ V to every other vertex u ∈ V.
  • 4.
    Requirement of AllPair Shortest Path There are various scenarios where we can apply the All Pair Shortest Path approach to reach the solution. Let us see some of them: 1. This algorithm is used in finding the transitive closure of directed graphs. 2. Inversion of real matrices 3. To check if a given graph is bipartite or not. 4. Faster computation of PathFinder Networks Algorithms to Find All Pair Shortest Path There are mainly two algorithms to compute all pair shortest paths in a given graph. We have elaborate articles explaining each of these algorithms. These are
  • 5.
    Floyd Warshall Algorithm TheFloyd Warshall Algorithm is for solving all pairs of shortest-path problems. The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed Graph. This algorithm follows the dynamic programming approach to find the shortest path. How Floyd-Warshall Algorithm Works? Let the given graph be: Follow the steps below to find the shortest path between all the pairs of vertices.
  • 6.
    1.Create a matrixA0 of dimension n*n where n is the number of vertices. The row and the column are indexed as i and j respectively. i and j are the vertices of the graph. Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no path from ith vertex to jth vertex, the cell is left as infinity.
  • 7.
    A4 gives theshortest path between each pair of vertices
  • 8.
    . Floyd Warshall Algorithm: TimeComplexity: O(n3) cost [i][j]= cost Mat [i] [j]; for(k=0; k<N; k++) { for(i=0; i<N; i++) for(j=0; j<N; j++) if(cost [i][j]> cost [i] [k] + cost [k][j]; cost [i][j]=cost [i] [k]+'cost [k] [i]: } void floydwarshall() { int cost [N][N]; int i, j, k; for(i=0; i<N; i++) for(j=0; j<N; j++)
  • 9.
    Johnson’s algorithm forAll-pairs shortest paths The problem is to find the shortest paths between every pair of vertices in a given weighted directed Graph and weights may be negative. The time complexity of the Floyd Warshall Algorithm is Θ(V3). Using Johnson’s algorithm, we can find all pair shortest paths in O(V2log V + VE) time. Johnson’s algorithm uses both Dijkstra and Bellman-Ford as subroutines. If we apply Dijkstra’s Single Source shortest path algorithm for every vertex, considering every vertex as the source, we can find all pair shortest paths in O(V*VLogV) time So using Dijkstra’s single-source shortest path seems to be a better option than Floyd Warshall’s Algorithm , but the problem with Dijkstra’s algorithm is, that it doesn’t work for negative weight edge. The idea of Johnson’s algorithm is to re-weight all edges and make them all positive, then apply Dijkstra’s algorithm for every vertex.
  • 10.
    Example: Let usconsider the following graph We add a source s and add edges from s to all vertices of the original graph. In the following diagram s is 4
  • 11.
    We calculate theshortest distances from 4 to all other vertices using Bellman-Ford algorithm. The shortest distances from 4 to 0, 1, 2 and 3 are 0, -5, -1 and 0 respectively, i.e., h[] = {0, -5, -1, 0}. Once we get these distances, we remove the source vertex 4 and reweight the edges using following formula. w(u, v) = w(u, v) + h[u] – h[v]
  • 12.
    Algorithm: 1.Let the givengraph be G. Add a new vertex s to the graph, add edges from the new vertex to all vertices of G. Let the modified graph be G’ 2. Run the Bellman-Ford algorithm on G’ with s as the source. Let the distances calculated by Bellman-Ford be h[0], h[1], .. h[V-1]. If we find a negative weight cycle, then return. Note that the negative weight cycle cannot be created by new vertex s as there is no edge to s. All edges are from s. 3. Reweight the edges of the original graph. For each edge (u, v), assign the new weight as “original weight + h[u] – h[v]”. 4. Remove the added vertex s and run Dijkstra’s algorithm for every vertex.
  • 13.
    Time Complexity: The mainsteps in the algorithm are Bellman-Ford Algorithm called once and Dijkstra called V times. Time complexity of Bellman Ford is O(VE) and time complexity of Dijkstra is O(VLogV). So overall time complexity is O(V2log V + VE). The time complexity of Johnson’s algorithm becomes the same as Floyd Warshall’s Algorithm when the graph is complete (For a complete graph E = O(V2). But for sparse graphs, the algorithm performs much better than Floyd Warshall’s Algorithm.
  • 14.