SlideShare una empresa de Scribd logo
1 de 26
S.SRIKRISHNAN
II year
CSE Department
SSNCE
1
The shortest distance between two points is under construction. – Noelie Altito
FLOYD’ ALGORITHM DESIGN
 Introduction
 Problem statement
 Solution
◦ Greedy Method (Dijkstra’s Algorithm)
◦ Dynamic Programming Method
 Applications
2
 A non-linear data structure
 Set of vertices and edges
 Classification
◦ Undirected graph
◦ Directed graph (digraph)
 Basic terminologies
◦ Path
◦ Cycle
◦ Degree
3
 Representation
◦ Incidence Matrix
◦ Adjacency Matrix
◦ Adjacency List
◦ Path Matrix
 Traversals
◦ Depth first (Stack ADT)
◦ Breadth first (Queue ADT)
4
 Standard Problems
◦ Travelling salesman problem
◦ Minimum spanning tree problem
◦ Shortest path problem
◦ Chinese postman problem
5
 To find the shortest path between source and
other vertices
 Greedy method
 Assumptions
◦ A directed acyclic graph
◦ No negative edges
 Unweighted or weighted Graph
6
7
Graph
Source, S
G (V,E) (S,V1)
(S,V2)
(S,V3)
.
.
(S,Vn)
Algorithm
Data
Structure
Program
Dijkstra’s
Algorithm
.
.
.
.
.
8
Step 1
•Assign to every node a distance value. Set it to zero for our initial
node and to infinity for all other nodes.
Step 2
•Mark all nodes as unvisited. Set initial node as current.
Step 3
•For current node, consider all its unvisited neighbours and calculate
their distance (from the initial node).
Step 4
•If this distance is less than the previously recorded distance (infinity
in the beginning, zero for the initial node), overwrite the distance.
9
Step 5
•When we are done considering all neighbours of the current node,
mark it as visited.
Step 6
•A visited node will not be checked ever again; its distance recorded
now is final and minimal.
Step 7
•Set the unvisited node with the smallest distance (from the initial
node) as the next "current node" and continue from step 3
Step 8
•Stop
Pseudo code
1. function Dijkstra (Graph, source):
2. for each vertex v in Graph: // Initializations
3. dist[v] := infinity // Unknown distance function from source to v
4. previous[v] := undefined // Previous node in optimal path from source
5. dist[source] := 0 // Distance from source to
source
6. Q := the set of all nodes in Graph // All nodes in the graph are unoptimized -
thus are in Q
7. while Q is not empty: // The main loop
8. u := vertex in Q with smallest dist[]
9. if dist[u] = infinity:
10. break // all remaining vertices are inaccessible from source
11. remove u from Q
12. for each neighbor v of u: // where v has not yet been removed from Q
13. alt := dist[u] + dist_between(u, v)
14. if alt < dist[v]: // Relax (u,v,a)
15. dist[v] := alt
16. previous[v] := u
17. return dist[]
Running time: O((n+|E|)log n)
10
A sample graph:
2
4
103
2
2
4 1
1
85
0
2
3
1
3
6
5
Sample Ip/Op
11
Result and Scope of DA
 The shortest path between a source vertex to
all other vertices have been found
 Problem statement could me modified to:
◦ To find shortest path between all vertices to a
particular vertex (destination)
◦ How do I change the algorithm ?
12
Problem Extensions
 The SINGLE-SOURCE SHORTEST PATH PROBLEM, in which
we have to find shortest paths from a source vertex v to
all other vertices in the graph.
 The SINGLE-DESTINATION SHORTEST PATH PROBLEM, in
which we have to find shortest paths from all vertices in
the graph to a single destination vertex v. This can be
reduced to the single-source shortest path problem by
reversing the edges in the graph.
 The ALL-PAIRS SHORTEST PATH PROBLEM, in which we
have to find shortest paths between every pair of
vertices v, v' in the graph.
13
Graph
Source, S
G (V,E)
Dijkstra’s
Algorithm
.
.
.
.
.
(S,V1)
(S,V2)
(S,V3)
.
.
(S,Vn)
(-)
Graph
Destination, D
G (V,E)
Dijkstra’s
Algorithm
.
.
.
.
.
(D,V1)
(D,V2)
(D,V3)
.
.
(D,Vn)
SINGLE-SOURCE SHORTEST PATH PROBLEM
SINGLE-DESTINATION SHORTEST PATH PROBLEM
Graph
G (V,E)
Dijkstra’s
Algorithm
.
.
.
.
.
(V1,V1)
(V1,V2)
(V1,V3)
.
.
(Vn,Vn)
ALL - PAIRS SHORTEST PATH PROBLEM
14
All Pairs Shortest Path problem
 The all-pairs shortest path algorithm is to determine
a matrix A such that A(i, j) is the length of the
shortest path between i and j.
 Input given as a matrix form
 Output is an nXn matrix D = [dij] where dij is the
shortest path from vertex i to j.
Wij =
0, if i=j
W(i, j), if (i,j) ε E
∞, if (i,j) ε E
15
Solution 1
 If there are no negative cost edges apply
Dijkstra’s algorithm to each vertex (as the
source) of the digraph.
 Disadvantage
 Running time increases to O(n(n+|E|)log n)
 Therefore we go for Dynamic Programming
16
17
Dynamic Programming
 An algorithm design method that can be used
when the solution to the problem can be viewed
as a result of a sequence of decisions.
 Best examples:
 Ordering matrix multiplication
 Optimal binary search trees
 All pairs-shortest path
18
Solution 2
 To find the shortest path from i to j (i!=j)
 Assume some intermediate vertex k (or no
vertices also)
 The shortest path from i to j is the shortest
path from [(i,k) + (k,j) or i to j ] which ever is
shorter.
 We use associated matrices and its powers to
calculate the shortest path from i to k and also
k to j.
 Matrix obtained in O(n.n.n)
19
Associated Matrices
1
6
3
4
2
A0 1 2 3
1 0 4 11
2 6 0 2
3 3 ∞ 0
A1 1 2 3
1 0 4 11
2 6 0 2
3 3 7 0
A2 1 2 3
1 0 4 6
2 6 0 2
3 3 7 0
A3 1 2 3
1 0 4 6
2 5 0 2
3 3 7 0
Ak(i,j) = min {Ak-1(i,j), Ak-1(i,k) + Ak-1(k,j)}, k>=1
* Not true for negative edges
20
Pseudo Code
1. algorithm allpairs (cost, A, n):
2. //cost[1:n, 1:n] is the cost adjacency matrix of a graph with n
vertices
3. //A[i,j] is the cost of a shortest path from vertex i toj .
4. //cost[i,i] = 0 for 1<=i<=n.
5. {
6. for(i=0;i<n;i++)
7. for(j=0;j<n;j++)
8. A[i][j]=cost[i][j] //copy cost into A
9. for(k=0;k<n;k++)
10. for(i=0;i<n;i++)
11. for(j=0;j<n;j++)
12. A[i,j] = min { A[i,j] , A[i,k] + A[k,j] };
13. }
21
Applications
 To automatically find directions between
physical locations
 Vehicle Routing and scheduling
 In a networking or telecommunication
applications, Dijkstra’s algorithm has been used
for solving the min-delay path problem (which
is the shortest path problem). For example in
data network routing, the goal is to find
the path for data packets to go through a
switching network with minimal delay.
22
New York To Los Angels 
23
A Real Life Problem
 Whole pineapples are served in a restaurant in London. To
ensure freshness, the pineapples are purchased in Hawaii and air
freighted from Honolulu to Heathrow in London. The following
network diagram outlines the different routes that the
pineapples could take.
105
68
57
76
65
88
105
4875
44
63
56
71
24
References
 Data Structures and Algorithm Analysis in C,
Second Edition, M.A. Weiss
 Fundamentals of Computer Algorithms, Second
Edition, Ellis Horowitz, Sartaj Sahni,
Sanguthevar Rajasekaran
 Introduction to Design and Analysis of
Algorithms, Fifth Edition, Anany Levitin
25
All pairs shortest path algorithm

Más contenido relacionado

La actualidad más candente

Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 

La actualidad más candente (20)

Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Traveling salesman problem
Traveling salesman problemTraveling salesman problem
Traveling salesman problem
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptx
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 

Destacado

Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and AnalysisAll Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
Inderjeet Singh
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
guest862df4e
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
faisal2204
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
Carlostheran
 
All pair shortest path--SDN
All pair shortest path--SDNAll pair shortest path--SDN
All pair shortest path--SDN
Sarat Prasad
 

Destacado (20)

Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithm
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - Malinga
 
Shortest path problem
Shortest path problemShortest path problem
Shortest path problem
 
Shortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraShortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma Dijkstra
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and AnalysisAll Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest path
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
 
21 All Pairs Shortest Path
21 All Pairs Shortest Path21 All Pairs Shortest Path
21 All Pairs Shortest Path
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Dijkstra's Algorithm - Colleen Young
Dijkstra's Algorithm  - Colleen YoungDijkstra's Algorithm  - Colleen Young
Dijkstra's Algorithm - Colleen Young
 
All pair shortest path--SDN
All pair shortest path--SDNAll pair shortest path--SDN
All pair shortest path--SDN
 
Dijkastra’s algorithm
Dijkastra’s algorithmDijkastra’s algorithm
Dijkastra’s algorithm
 

Similar a All pairs shortest path algorithm

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
meisamstar
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 

Similar a All pairs shortest path algorithm (20)

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
12_Graph.pptx
12_Graph.pptx12_Graph.pptx
12_Graph.pptx
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
 
Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Metric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsMetric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphs
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
 

Más de Srikrishnan Suresh (10)

Sources of Innovation
Sources of InnovationSources of Innovation
Sources of Innovation
 
Second review presentation
Second review presentationSecond review presentation
Second review presentation
 
First review presentation
First review presentationFirst review presentation
First review presentation
 
Final presentation
Final presentationFinal presentation
Final presentation
 
Zeroth review presentation
Zeroth review presentationZeroth review presentation
Zeroth review presentation
 
Canvas based presentation
Canvas based presentationCanvas based presentation
Canvas based presentation
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Merge sort
Merge sortMerge sort
Merge sort
 
Theory of LaTeX
Theory of LaTeXTheory of LaTeX
Theory of LaTeX
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Último

Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
instagramfab782445
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
TusharBahuguna2
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
amitlee9823
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
nirzagarg
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
amitlee9823
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
kumaririma588
 

Último (20)

UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
 

All pairs shortest path algorithm

  • 1. S.SRIKRISHNAN II year CSE Department SSNCE 1 The shortest distance between two points is under construction. – Noelie Altito FLOYD’ ALGORITHM DESIGN
  • 2.  Introduction  Problem statement  Solution ◦ Greedy Method (Dijkstra’s Algorithm) ◦ Dynamic Programming Method  Applications 2
  • 3.  A non-linear data structure  Set of vertices and edges  Classification ◦ Undirected graph ◦ Directed graph (digraph)  Basic terminologies ◦ Path ◦ Cycle ◦ Degree 3
  • 4.  Representation ◦ Incidence Matrix ◦ Adjacency Matrix ◦ Adjacency List ◦ Path Matrix  Traversals ◦ Depth first (Stack ADT) ◦ Breadth first (Queue ADT) 4
  • 5.  Standard Problems ◦ Travelling salesman problem ◦ Minimum spanning tree problem ◦ Shortest path problem ◦ Chinese postman problem 5
  • 6.  To find the shortest path between source and other vertices  Greedy method  Assumptions ◦ A directed acyclic graph ◦ No negative edges  Unweighted or weighted Graph 6
  • 7. 7 Graph Source, S G (V,E) (S,V1) (S,V2) (S,V3) . . (S,Vn) Algorithm Data Structure Program Dijkstra’s Algorithm . . . . .
  • 8. 8 Step 1 •Assign to every node a distance value. Set it to zero for our initial node and to infinity for all other nodes. Step 2 •Mark all nodes as unvisited. Set initial node as current. Step 3 •For current node, consider all its unvisited neighbours and calculate their distance (from the initial node). Step 4 •If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance.
  • 9. 9 Step 5 •When we are done considering all neighbours of the current node, mark it as visited. Step 6 •A visited node will not be checked ever again; its distance recorded now is final and minimal. Step 7 •Set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3 Step 8 •Stop
  • 10. Pseudo code 1. function Dijkstra (Graph, source): 2. for each vertex v in Graph: // Initializations 3. dist[v] := infinity // Unknown distance function from source to v 4. previous[v] := undefined // Previous node in optimal path from source 5. dist[source] := 0 // Distance from source to source 6. Q := the set of all nodes in Graph // All nodes in the graph are unoptimized - thus are in Q 7. while Q is not empty: // The main loop 8. u := vertex in Q with smallest dist[] 9. if dist[u] = infinity: 10. break // all remaining vertices are inaccessible from source 11. remove u from Q 12. for each neighbor v of u: // where v has not yet been removed from Q 13. alt := dist[u] + dist_between(u, v) 14. if alt < dist[v]: // Relax (u,v,a) 15. dist[v] := alt 16. previous[v] := u 17. return dist[] Running time: O((n+|E|)log n) 10
  • 11. A sample graph: 2 4 103 2 2 4 1 1 85 0 2 3 1 3 6 5 Sample Ip/Op 11
  • 12. Result and Scope of DA  The shortest path between a source vertex to all other vertices have been found  Problem statement could me modified to: ◦ To find shortest path between all vertices to a particular vertex (destination) ◦ How do I change the algorithm ? 12
  • 13. Problem Extensions  The SINGLE-SOURCE SHORTEST PATH PROBLEM, in which we have to find shortest paths from a source vertex v to all other vertices in the graph.  The SINGLE-DESTINATION SHORTEST PATH PROBLEM, in which we have to find shortest paths from all vertices in the graph to a single destination vertex v. This can be reduced to the single-source shortest path problem by reversing the edges in the graph.  The ALL-PAIRS SHORTEST PATH PROBLEM, in which we have to find shortest paths between every pair of vertices v, v' in the graph. 13
  • 14. Graph Source, S G (V,E) Dijkstra’s Algorithm . . . . . (S,V1) (S,V2) (S,V3) . . (S,Vn) (-) Graph Destination, D G (V,E) Dijkstra’s Algorithm . . . . . (D,V1) (D,V2) (D,V3) . . (D,Vn) SINGLE-SOURCE SHORTEST PATH PROBLEM SINGLE-DESTINATION SHORTEST PATH PROBLEM Graph G (V,E) Dijkstra’s Algorithm . . . . . (V1,V1) (V1,V2) (V1,V3) . . (Vn,Vn) ALL - PAIRS SHORTEST PATH PROBLEM 14
  • 15. All Pairs Shortest Path problem  The all-pairs shortest path algorithm is to determine a matrix A such that A(i, j) is the length of the shortest path between i and j.  Input given as a matrix form  Output is an nXn matrix D = [dij] where dij is the shortest path from vertex i to j. Wij = 0, if i=j W(i, j), if (i,j) ε E ∞, if (i,j) ε E 15
  • 16. Solution 1  If there are no negative cost edges apply Dijkstra’s algorithm to each vertex (as the source) of the digraph.  Disadvantage  Running time increases to O(n(n+|E|)log n)  Therefore we go for Dynamic Programming 16
  • 17. 17 Dynamic Programming  An algorithm design method that can be used when the solution to the problem can be viewed as a result of a sequence of decisions.  Best examples:  Ordering matrix multiplication  Optimal binary search trees  All pairs-shortest path
  • 18. 18 Solution 2  To find the shortest path from i to j (i!=j)  Assume some intermediate vertex k (or no vertices also)  The shortest path from i to j is the shortest path from [(i,k) + (k,j) or i to j ] which ever is shorter.  We use associated matrices and its powers to calculate the shortest path from i to k and also k to j.  Matrix obtained in O(n.n.n)
  • 19. 19 Associated Matrices 1 6 3 4 2 A0 1 2 3 1 0 4 11 2 6 0 2 3 3 ∞ 0 A1 1 2 3 1 0 4 11 2 6 0 2 3 3 7 0 A2 1 2 3 1 0 4 6 2 6 0 2 3 3 7 0 A3 1 2 3 1 0 4 6 2 5 0 2 3 3 7 0 Ak(i,j) = min {Ak-1(i,j), Ak-1(i,k) + Ak-1(k,j)}, k>=1 * Not true for negative edges
  • 20. 20 Pseudo Code 1. algorithm allpairs (cost, A, n): 2. //cost[1:n, 1:n] is the cost adjacency matrix of a graph with n vertices 3. //A[i,j] is the cost of a shortest path from vertex i toj . 4. //cost[i,i] = 0 for 1<=i<=n. 5. { 6. for(i=0;i<n;i++) 7. for(j=0;j<n;j++) 8. A[i][j]=cost[i][j] //copy cost into A 9. for(k=0;k<n;k++) 10. for(i=0;i<n;i++) 11. for(j=0;j<n;j++) 12. A[i,j] = min { A[i,j] , A[i,k] + A[k,j] }; 13. }
  • 21. 21 Applications  To automatically find directions between physical locations  Vehicle Routing and scheduling  In a networking or telecommunication applications, Dijkstra’s algorithm has been used for solving the min-delay path problem (which is the shortest path problem). For example in data network routing, the goal is to find the path for data packets to go through a switching network with minimal delay.
  • 22. 22 New York To Los Angels 
  • 23. 23 A Real Life Problem  Whole pineapples are served in a restaurant in London. To ensure freshness, the pineapples are purchased in Hawaii and air freighted from Honolulu to Heathrow in London. The following network diagram outlines the different routes that the pineapples could take. 105 68 57 76 65 88 105 4875 44 63 56 71
  • 24. 24 References  Data Structures and Algorithm Analysis in C, Second Edition, M.A. Weiss  Fundamentals of Computer Algorithms, Second Edition, Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran  Introduction to Design and Analysis of Algorithms, Fifth Edition, Anany Levitin
  • 25. 25