SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Shortest path problem
Shortest path problemShortest path problem
Shortest path problem
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
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
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
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
 
Backtracking
BacktrackingBacktracking
Backtracking
 

Andere mochten auch

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: 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
 

Andere mochten auch (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
 
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: 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
 

Ähnlich wie 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
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batchJaimin Jani
 

Ähnlich wie 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
 
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
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batch
 

Mehr von 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
 

Kürzlich hochgeladen

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
 
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...Pooja Nehwal
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Douxkojalkojal131
 
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
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
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
 
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...Call Girls in Nagpur High Profile
 
Government polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcdGovernment polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcdshivubhavv
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...BarusRa
 
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...Call Girls in Nagpur High Profile
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
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...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
 
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
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funneljen_giacalone
 

Kürzlich hochgeladen (20)

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...
 
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...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
 
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...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
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 🧵
 
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
 
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...
 
Government polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcdGovernment polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcd
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
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...
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
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...
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
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...
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
 

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