SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances (20)

Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Predicate calculus
Predicate calculusPredicate calculus
Predicate calculus
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Shortest path algorithm
Shortest  path algorithmShortest  path algorithm
Shortest path algorithm
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 

En vedette

Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithmgsp1294
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithmMalinga Perera
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithmami_01
 
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 - MalingaMalinga Perera
 
Shortest path problem
Shortest path problemShortest path problem
Shortest path problemIfra Ilyas
 
Shortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraShortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraOnggo Wiryawan
 
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 AnalysisInderjeet Singh
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithmguest862df4e
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
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 pathMadhumita Tamhane
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpathCarlostheran
 
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 dijkstraRoshan Tailor
 
Dijkstra's Algorithm - Colleen Young
Dijkstra's Algorithm  - Colleen YoungDijkstra's Algorithm  - Colleen Young
Dijkstra's Algorithm - Colleen YoungColleen Young
 
All pair shortest path--SDN
All pair shortest path--SDNAll pair shortest path--SDN
All pair shortest path--SDNSarat Prasad
 
Dijkastra’s algorithm
Dijkastra’s algorithmDijkastra’s algorithm
Dijkastra’s algorithmPulkit Goel
 

En vedette (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
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S 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
 

Similaire à All pairs shortest path algorithm

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest pathMohammad Akbarizadeh
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 
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...Khoa Mac Tu
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Metric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsMetric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsAmna Abunamous
 
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.pdfDKTaxation
 
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 AlgorithmFulvio Corno
 

Similaire à All pairs shortest path algorithm (20)

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
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
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
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
 

Plus 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
 

Dernier

SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵anilsa9823
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdftbatkhuu1
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightDelhi Call girls
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...Suhani Kapoor
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...Amil baba
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girlsmodelanjalisharma4
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...Suhani Kapoor
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️soniya singh
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 

Dernier (20)

SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
 
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 

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