SlideShare a Scribd company logo
1 of 45
z
MADE BY
SANIA NISAR
ALL PAIR SHORTEST PATH
Shortest Path Problem
In data structures,
• Shortest path problem is a problem of finding the shortest path(s) between
vertices of a given graph.
• Shortest path between two vertices is a path that has the least cost as compared
to all other existing paths.
Shortest Path Algorithms
• Shortest path algorithms are a family of algorithms used for solving the shortest
path problem.
Types of Shortest Path Problem
Various types of shortest path problem are
S
1. Single-pair shortest path problem.
2. Single-source shortest path problem.
3. Single-destination shortest path problem.
4. All pairs shortest path problem.
Single-Pair Shortest Path Problem
• It is a shortest path problem where the shortest path between a given pair of
vertices is computed.
Single-Source Shortest Path Problem
• It is a shortest path problem where the shortest path from a given source
vertex to all other remaining vertices is computed.
• Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous
algorithms used for solving single-source shortest path problem.
Single-Destination Shortest Path Problem
• It is a shortest path problem where the shortest path from all the vertices to a
single destination vertex is computed.
• By reversing the direction of each edge in the graph, this problem reduces to
single-source shortest path problem.
• Dijkstra’s Algorithm is a famous algorithm adapted for solving single-
destination shortest path problem.
All Pairs Shortest Path Problem
• It is a shortest path problem where the shortest path between every pair of
vertices is computed.
• Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous
algorithms used for solving All pairs shortest path problem.
Some Algorithms
When no negative edges
• Using Dijkstra’s algorithm: O(V3)
• Using Binary heap implementation: O(VE lg V)
• Using Fibonacci heap: O(VE + V2 log V)
When no negative cycles
• Floyd-Warshall : O(V3) time
When negative cycles
• Using Bellman-Ford algorithm: O(V2 E) = O(V4 )
• Johnson : O(VE + V2 log V) time based on a clever combination of Bellman-Ford and
Dijkstra
Floyd-Warshall Algorithm
• It is used to solve All Pairs Shortest Path Problem.
• It computes the shortest path between every pair of vertices of the given graph.
• Floyd-Warshall Algorithm is an example of dynamic programming approach.
Advantages
Floyd-Warshall Algorithm has the following main
advantages
• It is extremely simple.
• It is easy to implement.
Floyd-Warshall Psuedocode
1. n = W . rows
2. D = W
3. for k = 1 to n
4. for i = 1 to n
5. for j = 1 to n
6.
7. return D
Time Complexity
• Floyd-Warshall Algorithm consists of three loops over all the nodes.
• The inner most loop consists of only constant complexity operations.
• Hence, the asymptotic complexity of Floyd-Warshall algorithm is
O(n3).
• Here, n is the number of nodes in the given graph.
When Floyd-Warshall Algorithm is Used?
• Floyd-Warshall Algorithm is best suited for dense graphs.
• This is because its complexity depends only on the number of
vertices in the given graph.
• For sparse graphs, Johnson’s Algorithm is more suitable.
Johnson’s Algorithm
• Johnson's algorithm is a way to find the shortest paths between all pairs
of vertices in an edge-weighted, directed graph.
• It allows some of the edge weights to be negative numbers, but no
negative-weight cycles may exist.It turns all negative values into non-
negative.
• It works by using the Bellman–Ford algorithm to compute a transformation
of the input graph that removes all negative weights, allowing Dijkstra's
algorithm to be used on the transformed graph.
Algorithm description
Johnson's algorithm consists of the following steps:
• First, a new node s is added to the graph, connected by zero-weight edges to
each of the other nodes.
• Second, the Bellman–Ford algorithm is used, starting from the new vertex s,
to find for each vertex v the minimum weight h(v) of a path from s to v.
• If this step detects a negative cycle, the algorithm is terminated.
• Next the edges of the original graph are reweighted using the values
computed by the Bellman–Ford algorithm: an edge from u to v ,having
length w(u,v) , is given the new length w(u,v) + h(u) − h(v).
• Finally, s is removed, and Dijkstra's algorithm is used to find the shortest
paths from each node s to every other vertex in the reweighted graph.
Psuedocode
Input : Graph G
Output : List of all pair shortest paths
• for G Johnson(G){
• G'.V = G.V + {n}
• G'.E = G.E + ((s,u) for u in G.V)
• weight(n,u) = 0 in G.V
• Dist = BellmanFord(G’.V , G’.E)
• for edge(u,v) in G'.E do
• weight(u,v) += h[u] - h[v]
• End
• L = [] /*for storing result*/
• for vertex v in G.V do
• L += Dijkstra(G, G.V)
• End
• return L
• }
Example
| V | = 5
| E | = 9
Step : 1
Add source vertex to the given graph
| V’ | = 6
| E’ | = 14
Step : 2
Find shortest path from “ S ” to all
vertices.
h ( a ) = δ ( s , a ) = 0
h ( b ) = δ ( s , b ) = s  d  c  b
= 0 – 5 + 4 = -1
h ( c ) = δ ( s , c ) = s  d  c
= 0 – 5 = -5
h ( d ) = δ ( s , d ) = s  d = 0
h ( e ) = δ ( s , e ) = s a  e
= 0 – 4 = -4
h ( a ) 0
h ( b ) -1
h ( c ) -5
h ( d ) 0
h ( e ) -4
Step : 3
• Find W’
• Formula for re-weighting edges
• W’ ( u , v ) = W ( u , v ) + h ( u ) – h ( v )
• Re-weight all vertices using this formula.
• After re-weighting we got non-negative values.
• W’( a , b )=W( a , b ) + h( a ) – h( b )
= 3 + 0 – (-1)= 4
• W’( a , c )=W( a , c ) + h( a ) – h( c )
= 8 + 0 – (-5)= 13
• W’( a , e )=W( a , e ) + h( a ) – h( e )
= -4 + 0 – (-4)= 0
• W’( b , d )=W( b , d ) + h( b ) – h( d )
= 1 + (-1) – 0= 0
• W’( b , e )=W( b , e ) + h( b ) – h( e )
= 7 + (-1) – (-4)= 10
• W’( c , b )=W( c , b ) + h( c ) – h( b )
= 4 + (-5) – (-1)= 0
• W’( d , c )=W( d , c ) + h( d ) – h( c )
= -5 + 0 – (-5)= 0
h ( a ) 0
h ( b ) -1
h ( c ) -5
h ( d ) 0
h ( e ) -4
• W’( d , a )=W( d , a ) + h( d ) – h( a )
= 2 + 0 – 0= 2
• W’( e , d )=W( e , d ) + h( e ) – h( d )
= 6 + (-4) – 0= 2
• W’( s , a )=W( s , a ) + h( s ) – h( a )
= 0 + 0 – 0= 0
• W’( s , b )=W( s , b ) + h( s ) – h( b )
= 0 + 0 – (-1)= 1
• W’( s , c )=W( s , c ) + h( s ) – h( c )
= 0 + 0 – (-5)= 5
• W’( s , d )=W( s , d ) + h( s ) – h( d )
= 0 + 0 – 0= 0
• W’( s , e )=W( s , e ) + h( s ) – h( e )
= 0 + 0 – (-4)= 4
h ( a ) 0
h ( b ) -1
h ( c ) -5
h ( d ) 0
h ( e ) -4
Step : 4
• Put new values of edges in the graph.
Step : 5
• Remove source
Step : 6
• Now find shortest distance from each vertex to every other vertex in the graph.
Applying Dijkstra Algorithm.
For Vertex A :
δ’( a , a )=0
δ’( a , b
)=0+2+0+0= 2
δ’( a , c )=0+2+0=
2
δ’( a , d )=0+2= 2
δ’( a , e )=0
To find “ δ ”
δ( u , v ) = δ’( u, v ) – h( u ) + h( v )
For Vertex A :
δ( a ,a ) = δ’( a , a ) – h( a ) + h( a )
= 0 – 0 + 0 = 0
δ( a ,b ) = δ’( a , b ) – h( a ) + h( b )
= 2 – 0 + (-1) = 1
δ( a ,c ) = δ’( a , c ) – h( a ) + h( c )
= 2 – 0 + (-5) = -3
δ( a ,d ) = δ’( a , d ) – h( a ) + h( d )
= 2 – 0 + 0 = 2
δ( a ,e ) = δ’( a , e ) – h( a ) + h( e )
= 0 – 0 + (-4) = -4
δ’( a , a ) 0 h ( a ) 0
δ’( a , b ) 2 h ( b ) -1
δ’( a , c ) 2 h ( c ) -5
δ’( a , d ) 2 h ( d ) 0
δ’( a , e ) 0 h ( e ) -4
• Shortest path from vertex A to every other vertex
For Vertex B :
δ’( b ,
a )= 0+2 = 2
δ’( b
, b )= 0
δ’( b
, c )= 0+0 = 0
δ’( b
, d )= 0
δ’( b
, e )= 0+2+0= 2
To find “ δ ”
δ( u , v ) = δ’( u, v ) – h( u ) + h( v )
For Vertex B :
δ( b ,a ) = δ’( b , a ) – h( b ) + h( a )
= 2 – (-1) + 0 = 3
δ( b ,b ) = δ’( b , b ) – h( b ) + h( b )
= 0 – (-1) + (-1) = 0
δ( b ,c ) = δ’( b , c ) – h( b ) + h( c )
= 0 – (-1) + (-5) = -4
δ( b ,d ) = δ’( b , d ) – h( b ) + h( d )
= 0 – (-1) + 0 = 1
δ( b ,e ) = δ’( b , e ) – h( b ) + h( e )
= 2 – (-1) + (-4) = -1
δ’( b , a ) 2 h ( a ) 0
δ’( b , b ) 0 h ( b ) -1
δ’( b , c ) 0 h ( c ) -5
δ’( b , d ) 0 h ( d ) 0
δ’( b , e ) 2 h ( e ) -4
• Shortest path from vertex B to every other vertex
For Vertex C :
δ’( c , a
)= 0+0+2 = 2
δ’( c , b
)= 0
δ’( c , c
)= 0
δ’( c , d
)= 0+0= 0
δ’( c , e
)= 0+0+2+0= 2
To find “ δ ”
δ( u , v ) = δ’( u, v ) – h( u ) + h( v )
For Vertex C :
δ( c ,a ) = δ’( c , a ) – h( c ) + h( a )
= 2 – (-5) + 0 = 7
δ( c ,b ) = δ’( c , b ) – h( c ) + h( b )
= 0 – (-5) + (-1) = 4
δ( c ,c ) = δ’( c , c ) – h( c ) + h( c )
= 0 – (-5) + (-5) = 0
δ( c ,d ) = δ’( c , d ) – h( c ) + h( d )
= 0 – (-5) + 0 = 5
δ( c ,e ) = δ’( c , e ) – h( c ) + h( e )
= 2 – (-5) + (-4) = 3
δ’( c , a ) 2 h ( a ) 0
δ’( c , b ) 0 h ( b ) -1
δ’( c , c ) 0 h ( c ) -5
δ’( c , d ) 0 h ( d ) 0
δ’( c , e ) 2 h ( e ) -4
• Shortest path from vertex C to every other vertex
For Vertex D :
δ’( d , a
)= 2
δ’( d , b
)= 0+0= 0
δ’( d , c
)= 0
δ’( d , d
)= 0
δ’( d , e
)= 2+0= 2
To find “ δ ”
δ( u , v ) = δ’( u, v ) – h( u ) + h( v )
For Vertex D :
δ( d ,a ) = δ’( d , a ) – h( d ) + h( a )
= 2 – 0 + 0 = 2
δ( d ,b ) = δ’( d , b ) – h( d ) + h( b )
= 0 – 0 + (-1) = -1
δ( d ,c ) = δ’( d , c ) – h( d ) + h( c )
= 0 – 0 + (-5) = -5
δ( d ,d ) = δ’( d , d ) – h( d ) + h( d )
= 0 – 0 + 0 = 0
δ( d ,e ) = δ’( d , e ) – h( d ) + h( e )
= 2 – 0 + (-4) = -2
δ’( d , a ) 2 h ( a ) 0
δ’( d , b ) 0 h ( b ) -1
δ’( d , c ) 0 h ( c ) -5
δ’( d , d ) 0 h ( d ) 0
δ’( d , e ) 2 h ( e ) -4
• Shortest path from vertex D to every other vertex
For Vertex E :
δ’( e , a
)= 2+2= 4
δ’( e , b
)= 2+0+0= 2
δ’( e , c
)= 2+0= 2
δ’( e , d
)= 2
δ’( e , e
)= 0
To find “ δ ”
δ( u , v ) = δ’( u, v ) – h( u ) + h( v )
For Vertex E :
δ( e ,a ) = δ’( e , a ) – h( e ) + h( a )
= 2 – 0 + 0 = 2
δ( e ,b ) = δ’( e , b ) – h( e ) + h( b )
= 0 – 0 + (-1) = -1
δ( e ,c ) = δ’( e , c ) – h( e ) + h( c )
= 0 – 0 + (-5) = -5
δ( e ,d ) = δ’( e , d ) – h( e ) + h( d )
= 0 – 0 + 0 = 0
δ( e ,e ) = δ’( e , e ) – h( e ) + h( e )
= 2 – 0 + (-4) = -2
δ’( e , a ) 4 h ( a ) 0
δ’( e , b ) 2 h ( b ) -1
δ’( e , c ) 2 h ( c ) -5
δ’( e , d ) 2 h ( d ) 0
δ’( e , e ) 0 h ( e ) -4
• Shortest path from vertex E to every other vertex
Time Complexity:
• The main steps in 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).
• But for sparse graphs, the algorithm performs much better than Floyd Warshell.

More Related Content

What's hot

Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest pathArafat Hossan
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 

What's hot (20)

Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Time complexity
Time complexityTime complexity
Time complexity
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Quick sort
Quick sortQuick sort
Quick sort
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 

Similar to All pair shortest path by Sania Nisar

Bellman ford
Bellman fordBellman ford
Bellman fordKiran K
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
(Www.entrance exam.net)-sail placement sample paper 5
(Www.entrance exam.net)-sail placement sample paper 5(Www.entrance exam.net)-sail placement sample paper 5
(Www.entrance exam.net)-sail placement sample paper 5SAMEER NAIK
 
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxericbrooks84875
 
"Mesh of Periodic Minimal Surfaces in CGAL."
"Mesh of Periodic Minimal Surfaces in CGAL.""Mesh of Periodic Minimal Surfaces in CGAL."
"Mesh of Periodic Minimal Surfaces in CGAL."Vissarion Fisikopoulos
 
Crib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsCrib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsA Jorge Garcia
 
Banco de preguntas para el ap
Banco de preguntas para el apBanco de preguntas para el ap
Banco de preguntas para el apMARCELOCHAVEZ23
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4Roziq Bahtiar
 
10.1 Distance and Midpoint Formulas
10.1 Distance and Midpoint Formulas10.1 Distance and Midpoint Formulas
10.1 Distance and Midpoint Formulasswartzje
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDeepakGowda357858
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
mathematics question bank for engineering students
mathematics question bank for engineering studentsmathematics question bank for engineering students
mathematics question bank for engineering studentsMrMRubanVelsUniversi
 

Similar to All pair shortest path by Sania Nisar (20)

Bellman ford
Bellman fordBellman ford
Bellman ford
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
(Www.entrance exam.net)-sail placement sample paper 5
(Www.entrance exam.net)-sail placement sample paper 5(Www.entrance exam.net)-sail placement sample paper 5
(Www.entrance exam.net)-sail placement sample paper 5
 
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
 
Temporal graph
Temporal graphTemporal graph
Temporal graph
 
Presentacion unidad 4
Presentacion unidad 4Presentacion unidad 4
Presentacion unidad 4
 
"Mesh of Periodic Minimal Surfaces in CGAL."
"Mesh of Periodic Minimal Surfaces in CGAL.""Mesh of Periodic Minimal Surfaces in CGAL."
"Mesh of Periodic Minimal Surfaces in CGAL."
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Crib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsCrib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC exams
 
Banco de preguntas para el ap
Banco de preguntas para el apBanco de preguntas para el ap
Banco de preguntas para el ap
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4
 
10.1 Distance and Midpoint Formulas
10.1 Distance and Midpoint Formulas10.1 Distance and Midpoint Formulas
10.1 Distance and Midpoint Formulas
 
Ch4
Ch4Ch4
Ch4
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Circle
CircleCircle
Circle
 
Math 3-H6
Math 3-H6Math 3-H6
Math 3-H6
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
mathematics question bank for engineering students
mathematics question bank for engineering studentsmathematics question bank for engineering students
mathematics question bank for engineering students
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

All pair shortest path by Sania Nisar

  • 1. z MADE BY SANIA NISAR ALL PAIR SHORTEST PATH
  • 2. Shortest Path Problem In data structures, • Shortest path problem is a problem of finding the shortest path(s) between vertices of a given graph. • Shortest path between two vertices is a path that has the least cost as compared to all other existing paths. Shortest Path Algorithms • Shortest path algorithms are a family of algorithms used for solving the shortest path problem.
  • 3. Types of Shortest Path Problem Various types of shortest path problem are S 1. Single-pair shortest path problem. 2. Single-source shortest path problem. 3. Single-destination shortest path problem. 4. All pairs shortest path problem.
  • 4. Single-Pair Shortest Path Problem • It is a shortest path problem where the shortest path between a given pair of vertices is computed. Single-Source Shortest Path Problem • It is a shortest path problem where the shortest path from a given source vertex to all other remaining vertices is computed. • Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for solving single-source shortest path problem.
  • 5. Single-Destination Shortest Path Problem • It is a shortest path problem where the shortest path from all the vertices to a single destination vertex is computed. • By reversing the direction of each edge in the graph, this problem reduces to single-source shortest path problem. • Dijkstra’s Algorithm is a famous algorithm adapted for solving single- destination shortest path problem. All Pairs Shortest Path Problem • It is a shortest path problem where the shortest path between every pair of vertices is computed. • Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for solving All pairs shortest path problem.
  • 6. Some Algorithms When no negative edges • Using Dijkstra’s algorithm: O(V3) • Using Binary heap implementation: O(VE lg V) • Using Fibonacci heap: O(VE + V2 log V) When no negative cycles • Floyd-Warshall : O(V3) time When negative cycles • Using Bellman-Ford algorithm: O(V2 E) = O(V4 ) • Johnson : O(VE + V2 log V) time based on a clever combination of Bellman-Ford and Dijkstra
  • 7. Floyd-Warshall Algorithm • It is used to solve All Pairs Shortest Path Problem. • It computes the shortest path between every pair of vertices of the given graph. • Floyd-Warshall Algorithm is an example of dynamic programming approach.
  • 8. Advantages Floyd-Warshall Algorithm has the following main advantages • It is extremely simple. • It is easy to implement.
  • 9. Floyd-Warshall Psuedocode 1. n = W . rows 2. D = W 3. for k = 1 to n 4. for i = 1 to n 5. for j = 1 to n 6. 7. return D
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. Time Complexity • Floyd-Warshall Algorithm consists of three loops over all the nodes. • The inner most loop consists of only constant complexity operations. • Hence, the asymptotic complexity of Floyd-Warshall algorithm is O(n3). • Here, n is the number of nodes in the given graph.
  • 18. When Floyd-Warshall Algorithm is Used? • Floyd-Warshall Algorithm is best suited for dense graphs. • This is because its complexity depends only on the number of vertices in the given graph. • For sparse graphs, Johnson’s Algorithm is more suitable.
  • 19. Johnson’s Algorithm • Johnson's algorithm is a way to find the shortest paths between all pairs of vertices in an edge-weighted, directed graph. • It allows some of the edge weights to be negative numbers, but no negative-weight cycles may exist.It turns all negative values into non- negative. • It works by using the Bellman–Ford algorithm to compute a transformation of the input graph that removes all negative weights, allowing Dijkstra's algorithm to be used on the transformed graph.
  • 20. Algorithm description Johnson's algorithm consists of the following steps: • First, a new node s is added to the graph, connected by zero-weight edges to each of the other nodes. • Second, the Bellman–Ford algorithm is used, starting from the new vertex s, to find for each vertex v the minimum weight h(v) of a path from s to v. • If this step detects a negative cycle, the algorithm is terminated. • Next the edges of the original graph are reweighted using the values computed by the Bellman–Ford algorithm: an edge from u to v ,having length w(u,v) , is given the new length w(u,v) + h(u) − h(v). • Finally, s is removed, and Dijkstra's algorithm is used to find the shortest paths from each node s to every other vertex in the reweighted graph.
  • 21. Psuedocode Input : Graph G Output : List of all pair shortest paths • for G Johnson(G){ • G'.V = G.V + {n} • G'.E = G.E + ((s,u) for u in G.V) • weight(n,u) = 0 in G.V • Dist = BellmanFord(G’.V , G’.E) • for edge(u,v) in G'.E do • weight(u,v) += h[u] - h[v] • End • L = [] /*for storing result*/ • for vertex v in G.V do • L += Dijkstra(G, G.V) • End • return L • }
  • 22. Example | V | = 5 | E | = 9
  • 23. Step : 1 Add source vertex to the given graph | V’ | = 6 | E’ | = 14
  • 24. Step : 2 Find shortest path from “ S ” to all vertices. h ( a ) = δ ( s , a ) = 0 h ( b ) = δ ( s , b ) = s  d  c  b = 0 – 5 + 4 = -1 h ( c ) = δ ( s , c ) = s  d  c = 0 – 5 = -5 h ( d ) = δ ( s , d ) = s  d = 0 h ( e ) = δ ( s , e ) = s a  e = 0 – 4 = -4 h ( a ) 0 h ( b ) -1 h ( c ) -5 h ( d ) 0 h ( e ) -4
  • 25. Step : 3 • Find W’ • Formula for re-weighting edges • W’ ( u , v ) = W ( u , v ) + h ( u ) – h ( v ) • Re-weight all vertices using this formula. • After re-weighting we got non-negative values.
  • 26. • W’( a , b )=W( a , b ) + h( a ) – h( b ) = 3 + 0 – (-1)= 4 • W’( a , c )=W( a , c ) + h( a ) – h( c ) = 8 + 0 – (-5)= 13 • W’( a , e )=W( a , e ) + h( a ) – h( e ) = -4 + 0 – (-4)= 0 • W’( b , d )=W( b , d ) + h( b ) – h( d ) = 1 + (-1) – 0= 0 • W’( b , e )=W( b , e ) + h( b ) – h( e ) = 7 + (-1) – (-4)= 10 • W’( c , b )=W( c , b ) + h( c ) – h( b ) = 4 + (-5) – (-1)= 0 • W’( d , c )=W( d , c ) + h( d ) – h( c ) = -5 + 0 – (-5)= 0 h ( a ) 0 h ( b ) -1 h ( c ) -5 h ( d ) 0 h ( e ) -4
  • 27. • W’( d , a )=W( d , a ) + h( d ) – h( a ) = 2 + 0 – 0= 2 • W’( e , d )=W( e , d ) + h( e ) – h( d ) = 6 + (-4) – 0= 2 • W’( s , a )=W( s , a ) + h( s ) – h( a ) = 0 + 0 – 0= 0 • W’( s , b )=W( s , b ) + h( s ) – h( b ) = 0 + 0 – (-1)= 1 • W’( s , c )=W( s , c ) + h( s ) – h( c ) = 0 + 0 – (-5)= 5 • W’( s , d )=W( s , d ) + h( s ) – h( d ) = 0 + 0 – 0= 0 • W’( s , e )=W( s , e ) + h( s ) – h( e ) = 0 + 0 – (-4)= 4 h ( a ) 0 h ( b ) -1 h ( c ) -5 h ( d ) 0 h ( e ) -4
  • 28. Step : 4 • Put new values of edges in the graph.
  • 29. Step : 5 • Remove source
  • 30. Step : 6 • Now find shortest distance from each vertex to every other vertex in the graph. Applying Dijkstra Algorithm. For Vertex A : δ’( a , a )=0 δ’( a , b )=0+2+0+0= 2 δ’( a , c )=0+2+0= 2 δ’( a , d )=0+2= 2 δ’( a , e )=0
  • 31. To find “ δ ” δ( u , v ) = δ’( u, v ) – h( u ) + h( v ) For Vertex A : δ( a ,a ) = δ’( a , a ) – h( a ) + h( a ) = 0 – 0 + 0 = 0 δ( a ,b ) = δ’( a , b ) – h( a ) + h( b ) = 2 – 0 + (-1) = 1 δ( a ,c ) = δ’( a , c ) – h( a ) + h( c ) = 2 – 0 + (-5) = -3 δ( a ,d ) = δ’( a , d ) – h( a ) + h( d ) = 2 – 0 + 0 = 2 δ( a ,e ) = δ’( a , e ) – h( a ) + h( e ) = 0 – 0 + (-4) = -4 δ’( a , a ) 0 h ( a ) 0 δ’( a , b ) 2 h ( b ) -1 δ’( a , c ) 2 h ( c ) -5 δ’( a , d ) 2 h ( d ) 0 δ’( a , e ) 0 h ( e ) -4
  • 32. • Shortest path from vertex A to every other vertex
  • 33. For Vertex B : δ’( b , a )= 0+2 = 2 δ’( b , b )= 0 δ’( b , c )= 0+0 = 0 δ’( b , d )= 0 δ’( b , e )= 0+2+0= 2
  • 34. To find “ δ ” δ( u , v ) = δ’( u, v ) – h( u ) + h( v ) For Vertex B : δ( b ,a ) = δ’( b , a ) – h( b ) + h( a ) = 2 – (-1) + 0 = 3 δ( b ,b ) = δ’( b , b ) – h( b ) + h( b ) = 0 – (-1) + (-1) = 0 δ( b ,c ) = δ’( b , c ) – h( b ) + h( c ) = 0 – (-1) + (-5) = -4 δ( b ,d ) = δ’( b , d ) – h( b ) + h( d ) = 0 – (-1) + 0 = 1 δ( b ,e ) = δ’( b , e ) – h( b ) + h( e ) = 2 – (-1) + (-4) = -1 δ’( b , a ) 2 h ( a ) 0 δ’( b , b ) 0 h ( b ) -1 δ’( b , c ) 0 h ( c ) -5 δ’( b , d ) 0 h ( d ) 0 δ’( b , e ) 2 h ( e ) -4
  • 35. • Shortest path from vertex B to every other vertex
  • 36. For Vertex C : δ’( c , a )= 0+0+2 = 2 δ’( c , b )= 0 δ’( c , c )= 0 δ’( c , d )= 0+0= 0 δ’( c , e )= 0+0+2+0= 2
  • 37. To find “ δ ” δ( u , v ) = δ’( u, v ) – h( u ) + h( v ) For Vertex C : δ( c ,a ) = δ’( c , a ) – h( c ) + h( a ) = 2 – (-5) + 0 = 7 δ( c ,b ) = δ’( c , b ) – h( c ) + h( b ) = 0 – (-5) + (-1) = 4 δ( c ,c ) = δ’( c , c ) – h( c ) + h( c ) = 0 – (-5) + (-5) = 0 δ( c ,d ) = δ’( c , d ) – h( c ) + h( d ) = 0 – (-5) + 0 = 5 δ( c ,e ) = δ’( c , e ) – h( c ) + h( e ) = 2 – (-5) + (-4) = 3 δ’( c , a ) 2 h ( a ) 0 δ’( c , b ) 0 h ( b ) -1 δ’( c , c ) 0 h ( c ) -5 δ’( c , d ) 0 h ( d ) 0 δ’( c , e ) 2 h ( e ) -4
  • 38. • Shortest path from vertex C to every other vertex
  • 39. For Vertex D : δ’( d , a )= 2 δ’( d , b )= 0+0= 0 δ’( d , c )= 0 δ’( d , d )= 0 δ’( d , e )= 2+0= 2
  • 40. To find “ δ ” δ( u , v ) = δ’( u, v ) – h( u ) + h( v ) For Vertex D : δ( d ,a ) = δ’( d , a ) – h( d ) + h( a ) = 2 – 0 + 0 = 2 δ( d ,b ) = δ’( d , b ) – h( d ) + h( b ) = 0 – 0 + (-1) = -1 δ( d ,c ) = δ’( d , c ) – h( d ) + h( c ) = 0 – 0 + (-5) = -5 δ( d ,d ) = δ’( d , d ) – h( d ) + h( d ) = 0 – 0 + 0 = 0 δ( d ,e ) = δ’( d , e ) – h( d ) + h( e ) = 2 – 0 + (-4) = -2 δ’( d , a ) 2 h ( a ) 0 δ’( d , b ) 0 h ( b ) -1 δ’( d , c ) 0 h ( c ) -5 δ’( d , d ) 0 h ( d ) 0 δ’( d , e ) 2 h ( e ) -4
  • 41. • Shortest path from vertex D to every other vertex
  • 42. For Vertex E : δ’( e , a )= 2+2= 4 δ’( e , b )= 2+0+0= 2 δ’( e , c )= 2+0= 2 δ’( e , d )= 2 δ’( e , e )= 0
  • 43. To find “ δ ” δ( u , v ) = δ’( u, v ) – h( u ) + h( v ) For Vertex E : δ( e ,a ) = δ’( e , a ) – h( e ) + h( a ) = 2 – 0 + 0 = 2 δ( e ,b ) = δ’( e , b ) – h( e ) + h( b ) = 0 – 0 + (-1) = -1 δ( e ,c ) = δ’( e , c ) – h( e ) + h( c ) = 0 – 0 + (-5) = -5 δ( e ,d ) = δ’( e , d ) – h( e ) + h( d ) = 0 – 0 + 0 = 0 δ( e ,e ) = δ’( e , e ) – h( e ) + h( e ) = 2 – 0 + (-4) = -2 δ’( e , a ) 4 h ( a ) 0 δ’( e , b ) 2 h ( b ) -1 δ’( e , c ) 2 h ( c ) -5 δ’( e , d ) 2 h ( d ) 0 δ’( e , e ) 0 h ( e ) -4
  • 44. • Shortest path from vertex E to every other vertex
  • 45. Time Complexity: • The main steps in 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). • But for sparse graphs, the algorithm performs much better than Floyd Warshell.