SlideShare a Scribd company logo
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem. Implementation Dijkstra's Algorithm
What is the shortest path problem? In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge. For example, in a graph representing a network of airports, the weights could represent: distance, cost or time. Such a graph could be used to answer any of the following: What is the fastest way to get from A to B? Which route from A to B is the least expensive? What is the shortest possible distance from A to B? Each of these questions is an instance of the same problem: The shortest path problem!
Is the shortest path problem well defined? If all the edges in a graph have non-negative weights, then it is possible to find the shortest path from any two vertices. For example, in the figure below, the shortest path from B to F is { B, A, C, E, F } with a total cost of nine. Thus, the problem is well defined for a graph that contains non-negative weights.
Is the shortest path problem well defined? - Cont'd Things get difficult for a graph with negative weights. For example, the path D, A, C, E, F costs 4 even though the edge (D, A) costs 5 -- the longer the less costly. The problem gets even worse if the graph has a negative cost cycle.  e.g. {D, A, C, D} A solution can be found even for negative-weight graphs but not for graphs involving negative cost cycles. {D, A, C, D, A, C, E, F} = 2 {D, A, C, D, A, C, D, A, C, E, F} = 0
The Dijkstra's Algorithm Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights graph. It finds the shortest path from an initial vertex, say s, to all the other vertices.
The Dijkstra's Algorithm  Cont'd // Let V be the set of all vertices in G, and s the start vertex. for(each vertex v){ currentDistance(s-v) = ∞; predecessor(v) = undefined; } currentDistance(s-s) = 0; T = V; while(T      ){ v = a vertex in T with minimal currentDistance from s; T= T – {v}; for(each vertex u adjacent to v and in T){ if(currentDistance(s-u) > currentDistance(s-v) + weight(edge(vu)){ currentDistance(s-u) = currentDistance(s-v) + weight(edge(vu)); predecessor(u) = v; } } } For each vertex, the algorithm keeps track of its current distance from the starting vertex and the predecessor on the current path
Example Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is:
Data structures required The implementation of Dijkstra's algorithm uses the Entry structure, which contains the following three fields:  know : a boolean variable indicating whether the shortest path to v is known, initially false for all vertices. distance  : the shortest known distance from s to v, initially infinity for all vertices except that of s which is 0. predecessor   : the predecessor of v on the path from s to v, initially unknown for all vertices. public class Algorithms{ static final class Entry{ boolean known; int distance; Vertex predecessor; Entry(){ known = false; distance = Integer.MAX_VALUE; predecessor = null; } }
Implementation of Dijkstra's Algorithm The dijkstrasAlgorithm method shown below takes two arguments, a directed graph and the starting vertex. The method returns a vertex-weighted Digraph from which the shortest path from s to any vertex can be found. Since in each pass, the vertex with the smallest known distance is chosen, a minimum priority queue is used to store the vertices. public static Graph dijkstrasAlgorithm(Graph g, Vertex start){ int n = g.getNumberOfVertices(); Entry table[] = new Entry[n]; for(int v = 0; v < n; v++) table[v] = new Entry(); table[g.getIndex(start)].distance = 0; PriorityQueue queue = new BinaryHeap( g.getNumberOfEdges()); queue.enqueue(new Association(new Integer(0), start));
Implementation of Dijkstra's Algorithm - Cont'd while(!queue.isEmpty()) { Association association = (Association)queue.dequeueMin(); Vertex v1 = (Vertex) association.getValue(); int n1 = g.getIndex(v1); if(!table[n1].known){ table[n1].known = true; Iterator p = v1.getEmanatingEdges(); while (p.hasNext()){ Edge edge = (Edge) p.next(); Vertex v2 = edge.getMate(v1); int n2 = g.getIndex(v2); Integer weight = (Integer) edge.getWeight(); int d = table[n1].distance + weight.intValue(); if(table[n2].distance > d){ table[n2].distance = d; table[n2].predecessor = v1; queue.enqueue(new Association(d, v2)); } } } }
Implementation of Dijkstra's Algorithm Cont'd Graph result = new GraphAsLists(true);//Result is Digraph Iterator it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); result.addVertex(v.getLabel(), new Integer(table[g.getIndex(v)].distance)); } it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); if (v != start){ String from = v.getLabel(); String to = table[g.getIndex(v)].predecessor.getLabel(); result.addEdge(from, to); } } return result; }
Review Questions Use the graph Gc shown above to trace the execution of Dijkstra's algorithm as it solves the shortest path problem starting from vertex a. Dijkstra's algorithm works as long as there are no negative edge weights. Given a graph that contains negative edge weights, we might be tempted to eliminate the negative weights by adding a constant weight to all of the edges. Explain why this does not work. Dijkstra's algorithm can be modified to deal with negative edge weights (but not negative cost cycles) by eliminating the known flag  and by inserting a vertex back into the queue every time its tentative distance decreases.  Implement this modified algorithm.
Ad

More Related Content

What's hot (20)

Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
Nv Thejaswini
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
Ruchika Sinha
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Kruskal’s Algorithm
Kruskal’s AlgorithmKruskal’s Algorithm
Kruskal’s Algorithm
Syed Maniruzzaman Pabel
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
InteX Research Lab
 
Algorithms
AlgorithmsAlgorithms
Algorithms
WaqarzadAa
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
Subid Biswas
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
ami_01
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
MdSajjadulislamBappi
 
Multistage graph unit 4 of algorithm.ppt
Multistage graph unit 4 of algorithm.pptMultistage graph unit 4 of algorithm.ppt
Multistage graph unit 4 of algorithm.ppt
VidulaVinothkumar
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims Algorithm
Sriram Raj
 
Slr parser
Slr parserSlr parser
Slr parser
Akila Krishnamoorthy
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)
Akshay soni
 

Viewers also liked (20)

Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
Graphs
GraphsGraphs
Graphs
Ghaffar Khan
 
Analysis of Algorithms II - PS3
Analysis of Algorithms II - PS3Analysis of Algorithms II - PS3
Analysis of Algorithms II - PS3
AtakanAral
 
Shortest path search for real road networks and dynamic costs with pgRouting
Shortest path search for real road networks and dynamic costs with pgRoutingShortest path search for real road networks and dynamic costs with pgRouting
Shortest path search for real road networks and dynamic costs with pgRouting
antonpa
 
2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s
Krish_ver2
 
Top-k shortest path
Top-k shortest pathTop-k shortest path
Top-k shortest path
redhatdb
 
Multi-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architectureMulti-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architecture
Umair Amjad
 
Solving The Shortest Path Tour Problem
Solving The Shortest Path Tour ProblemSolving The Shortest Path Tour Problem
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
Shortest path algorithm
Shortest  path algorithmShortest  path algorithm
Shortest path algorithm
Subrata Kumer Paul
 
Intel core i3, i5, i7 , core2 duo and atom processors
Intel core i3, i5, i7 , core2 duo and atom processorsIntel core i3, i5, i7 , core2 duo and atom processors
Intel core i3, i5, i7 , core2 duo and atom processors
FadyMorris
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
taimurkhan803
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 
Discrete Mathematics Presentation
Discrete Mathematics PresentationDiscrete Mathematics Presentation
Discrete Mathematics Presentation
Salman Elahi
 
Shortest Path Problem
Shortest Path ProblemShortest Path Problem
Shortest Path Problem
Guillaume Guérard
 
Intel Core i7 Processors
Intel Core i7 ProcessorsIntel Core i7 Processors
Intel Core i7 Processors
Anagh Vijayvargia
 
Intel I3,I5,I7 Processor
Intel I3,I5,I7 ProcessorIntel I3,I5,I7 Processor
Intel I3,I5,I7 Processor
sagar solanky
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
Eric Wilson
 
Network Problem CPM & PERT
Network Problem CPM &  PERTNetwork Problem CPM &  PERT
Network Problem CPM & PERT
Pulchowk Campus
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
Analysis of Algorithms II - PS3
Analysis of Algorithms II - PS3Analysis of Algorithms II - PS3
Analysis of Algorithms II - PS3
AtakanAral
 
Shortest path search for real road networks and dynamic costs with pgRouting
Shortest path search for real road networks and dynamic costs with pgRoutingShortest path search for real road networks and dynamic costs with pgRouting
Shortest path search for real road networks and dynamic costs with pgRouting
antonpa
 
2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s
Krish_ver2
 
Top-k shortest path
Top-k shortest pathTop-k shortest path
Top-k shortest path
redhatdb
 
Multi-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architectureMulti-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architecture
Umair Amjad
 
Solving The Shortest Path Tour Problem
Solving The Shortest Path Tour ProblemSolving The Shortest Path Tour Problem
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
Intel core i3, i5, i7 , core2 duo and atom processors
Intel core i3, i5, i7 , core2 duo and atom processorsIntel core i3, i5, i7 , core2 duo and atom processors
Intel core i3, i5, i7 , core2 duo and atom processors
FadyMorris
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
taimurkhan803
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 
Discrete Mathematics Presentation
Discrete Mathematics PresentationDiscrete Mathematics Presentation
Discrete Mathematics Presentation
Salman Elahi
 
Intel I3,I5,I7 Processor
Intel I3,I5,I7 ProcessorIntel I3,I5,I7 Processor
Intel I3,I5,I7 Processor
sagar solanky
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
Eric Wilson
 
Network Problem CPM & PERT
Network Problem CPM &  PERTNetwork Problem CPM &  PERT
Network Problem CPM & PERT
Pulchowk Campus
 
Ad

Similar to Unit26 shortest pathalgorithm (20)

Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
SantoshDood
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
dijkstra algo.ppt
dijkstra algo.pptdijkstra algo.ppt
dijkstra algo.ppt
Santhosh Krishna
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
Core Condor
 
Lec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data StructureLec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data Structure
Anil Yadav
 
graph in Data Structures and Algorithm.ppt
graph in Data Structures and Algorithm.pptgraph in Data Structures and Algorithm.ppt
graph in Data Structures and Algorithm.ppt
RAJASEKARAN G
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
AndrewJohnson866415
 
Hamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's AlgorithmHamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's Algorithm
Mahesh Singh Madai
 
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptxSEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
WahyuAde4
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
zefergaming
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
Mohammad Akbarizadeh
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
DEEPAK948083
 
Dijkstra's algorithm for computer science
Dijkstra's algorithm for computer scienceDijkstra's algorithm for computer science
Dijkstra's algorithm for computer science
ajmalnajath4
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
CSE633
CSE633CSE633
CSE633
Meenakshi Muthuraman
 
Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
SantoshDood
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
Lec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data StructureLec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data Structure
Anil Yadav
 
graph in Data Structures and Algorithm.ppt
graph in Data Structures and Algorithm.pptgraph in Data Structures and Algorithm.ppt
graph in Data Structures and Algorithm.ppt
RAJASEKARAN G
 
Hamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's AlgorithmHamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's Algorithm
Mahesh Singh Madai
 
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptxSEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
WahyuAde4
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
zefergaming
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
Mohammad Akbarizadeh
 
Dijkstra's algorithm for computer science
Dijkstra's algorithm for computer scienceDijkstra's algorithm for computer science
Dijkstra's algorithm for computer science
ajmalnajath4
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
Ad

Recently uploaded (20)

Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 

Unit26 shortest pathalgorithm

  • 1. Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem. Implementation Dijkstra's Algorithm
  • 2. What is the shortest path problem? In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge. For example, in a graph representing a network of airports, the weights could represent: distance, cost or time. Such a graph could be used to answer any of the following: What is the fastest way to get from A to B? Which route from A to B is the least expensive? What is the shortest possible distance from A to B? Each of these questions is an instance of the same problem: The shortest path problem!
  • 3. Is the shortest path problem well defined? If all the edges in a graph have non-negative weights, then it is possible to find the shortest path from any two vertices. For example, in the figure below, the shortest path from B to F is { B, A, C, E, F } with a total cost of nine. Thus, the problem is well defined for a graph that contains non-negative weights.
  • 4. Is the shortest path problem well defined? - Cont'd Things get difficult for a graph with negative weights. For example, the path D, A, C, E, F costs 4 even though the edge (D, A) costs 5 -- the longer the less costly. The problem gets even worse if the graph has a negative cost cycle. e.g. {D, A, C, D} A solution can be found even for negative-weight graphs but not for graphs involving negative cost cycles. {D, A, C, D, A, C, E, F} = 2 {D, A, C, D, A, C, D, A, C, E, F} = 0
  • 5. The Dijkstra's Algorithm Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights graph. It finds the shortest path from an initial vertex, say s, to all the other vertices.
  • 6. The Dijkstra's Algorithm Cont'd // Let V be the set of all vertices in G, and s the start vertex. for(each vertex v){ currentDistance(s-v) = ∞; predecessor(v) = undefined; } currentDistance(s-s) = 0; T = V; while(T   ){ v = a vertex in T with minimal currentDistance from s; T= T – {v}; for(each vertex u adjacent to v and in T){ if(currentDistance(s-u) > currentDistance(s-v) + weight(edge(vu)){ currentDistance(s-u) = currentDistance(s-v) + weight(edge(vu)); predecessor(u) = v; } } } For each vertex, the algorithm keeps track of its current distance from the starting vertex and the predecessor on the current path
  • 7. Example Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is:
  • 8. Data structures required The implementation of Dijkstra's algorithm uses the Entry structure, which contains the following three fields: know : a boolean variable indicating whether the shortest path to v is known, initially false for all vertices. distance : the shortest known distance from s to v, initially infinity for all vertices except that of s which is 0. predecessor : the predecessor of v on the path from s to v, initially unknown for all vertices. public class Algorithms{ static final class Entry{ boolean known; int distance; Vertex predecessor; Entry(){ known = false; distance = Integer.MAX_VALUE; predecessor = null; } }
  • 9. Implementation of Dijkstra's Algorithm The dijkstrasAlgorithm method shown below takes two arguments, a directed graph and the starting vertex. The method returns a vertex-weighted Digraph from which the shortest path from s to any vertex can be found. Since in each pass, the vertex with the smallest known distance is chosen, a minimum priority queue is used to store the vertices. public static Graph dijkstrasAlgorithm(Graph g, Vertex start){ int n = g.getNumberOfVertices(); Entry table[] = new Entry[n]; for(int v = 0; v < n; v++) table[v] = new Entry(); table[g.getIndex(start)].distance = 0; PriorityQueue queue = new BinaryHeap( g.getNumberOfEdges()); queue.enqueue(new Association(new Integer(0), start));
  • 10. Implementation of Dijkstra's Algorithm - Cont'd while(!queue.isEmpty()) { Association association = (Association)queue.dequeueMin(); Vertex v1 = (Vertex) association.getValue(); int n1 = g.getIndex(v1); if(!table[n1].known){ table[n1].known = true; Iterator p = v1.getEmanatingEdges(); while (p.hasNext()){ Edge edge = (Edge) p.next(); Vertex v2 = edge.getMate(v1); int n2 = g.getIndex(v2); Integer weight = (Integer) edge.getWeight(); int d = table[n1].distance + weight.intValue(); if(table[n2].distance > d){ table[n2].distance = d; table[n2].predecessor = v1; queue.enqueue(new Association(d, v2)); } } } }
  • 11. Implementation of Dijkstra's Algorithm Cont'd Graph result = new GraphAsLists(true);//Result is Digraph Iterator it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); result.addVertex(v.getLabel(), new Integer(table[g.getIndex(v)].distance)); } it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); if (v != start){ String from = v.getLabel(); String to = table[g.getIndex(v)].predecessor.getLabel(); result.addEdge(from, to); } } return result; }
  • 12. Review Questions Use the graph Gc shown above to trace the execution of Dijkstra's algorithm as it solves the shortest path problem starting from vertex a. Dijkstra's algorithm works as long as there are no negative edge weights. Given a graph that contains negative edge weights, we might be tempted to eliminate the negative weights by adding a constant weight to all of the edges. Explain why this does not work. Dijkstra's algorithm can be modified to deal with negative edge weights (but not negative cost cycles) by eliminating the known flag and by inserting a vertex back into the queue every time its tentative distance decreases. Implement this modified algorithm.