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.

More Related Content

PPTX
Dijkstra’s algorithm
PPTX
Dijkstra s algorithm
PPTX
Introduction to Graph Theory
PPT
SINGLE-SOURCE SHORTEST PATHS
PPT
Dijkstra.ppt
PPTX
graph theory
PPTX
Graph Theory
PPT
Shortest path
Dijkstra’s algorithm
Dijkstra s algorithm
Introduction to Graph Theory
SINGLE-SOURCE SHORTEST PATHS
Dijkstra.ppt
graph theory
Graph Theory
Shortest path

What's hot (20)

PPTX
Applications of graph theory
PPTX
Graph theory
PPTX
Shortest path algorithm
PPTX
Discrete Mathematics Presentation
PPTX
Ppt of graph theory
PPTX
PRIM'S ALGORITHM
PDF
Introduction to Graph Theory
PDF
Shortest Path in Graph
PPT
PPTX
Application of algorithm in real life
PDF
Graph theory and its applications
PPTX
Planar graph( Algorithm and Application )
PDF
Dijkstra's Algorithm
PDF
Algorithm Design and Analysis - Practical File
PPTX
Cyclic group- group theory
PPTX
Kruskal's algorithm
PPTX
Interesting applications of graph theory
PDF
Cs6702 graph theory and applications 2 marks questions and answers
PPT
dijkstra algo.ppt
PPTX
Graph theory
Applications of graph theory
Graph theory
Shortest path algorithm
Discrete Mathematics Presentation
Ppt of graph theory
PRIM'S ALGORITHM
Introduction to Graph Theory
Shortest Path in Graph
Application of algorithm in real life
Graph theory and its applications
Planar graph( Algorithm and Application )
Dijkstra's Algorithm
Algorithm Design and Analysis - Practical File
Cyclic group- group theory
Kruskal's algorithm
Interesting applications of graph theory
Cs6702 graph theory and applications 2 marks questions and answers
dijkstra algo.ppt
Graph theory
Ad

Viewers also liked (20)

PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
PPT
Graphs
PDF
Analysis of Algorithms II - PS3
PDF
Shortest path search for real road networks and dynamic costs with pgRouting
PPT
2.3 shortest path dijkstra’s
PPT
Top-k shortest path
PPTX
Multi-core processor and Multi-channel memory architecture
PDF
Solving The Shortest Path Tour Problem
PPTX
Shortest path algorithm
PPTX
Shortest path problem
PDF
Intel core i3, i5, i7 , core2 duo and atom processors
PPTX
Bellman ford Algorithm
PDF
All pairs shortest path algorithm
PPTX
Dijkstra's algorithm
PDF
Shortest Path Problem
PPT
Intel Core i7 Processors
PPTX
Intel I3,I5,I7 Processor
PPT
Unix command-line tools
PPTX
Network Problem CPM & PERT
PDF
An in-building multi-server cloud system based on shortest Path algorithm dep...
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Graphs
Analysis of Algorithms II - PS3
Shortest path search for real road networks and dynamic costs with pgRouting
2.3 shortest path dijkstra’s
Top-k shortest path
Multi-core processor and Multi-channel memory architecture
Solving The Shortest Path Tour Problem
Shortest path algorithm
Shortest path problem
Intel core i3, i5, i7 , core2 duo and atom processors
Bellman ford Algorithm
All pairs shortest path algorithm
Dijkstra's algorithm
Shortest Path Problem
Intel Core i7 Processors
Intel I3,I5,I7 Processor
Unix command-line tools
Network Problem CPM & PERT
An in-building multi-server cloud system based on shortest Path algorithm dep...
Ad

Similar to Unit26 shortest pathalgorithm (20)

PPT
Dijkstra algorithm ds 57612334t4t44.ppt
PPT
Dijkstra Shortest Path Algorithm in Network.ppt
PPT
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
PPT
2.6 all pairsshortestpath
PPT
Weighted graphs
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPT
graph in Data Structures and Algorithm.ppt
PPTX
DAA_Presentation - Copy.pptx
PPTX
Hamilton Path & Dijkstra's Algorithm
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
PPT
Unit27_MinimumSpanningTree.ppt data structure programming
PPT
Lecture_10_Parallel_Algorithms_Part_II.ppt
PDF
Shortest path by using suitable algorithm.pdf
PPTX
Randomized algorithms all pairs shortest path
PPTX
Dijkstra Algorithm Presentation -the shortest path finding algorithm.pptx
PPTX
Dijkstra's Algorithm
PPTX
Dijkstra’S Algorithm
PDF
Daa chpater14
PPT
Dijesktra 1.ppt
PPT
Dijkstra's algorithm for computer science
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
2.6 all pairsshortestpath
Weighted graphs
Lec-35Graph - Graph - Copy in Data Structure
graph in Data Structures and Algorithm.ppt
DAA_Presentation - Copy.pptx
Hamilton Path & Dijkstra's Algorithm
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
Unit27_MinimumSpanningTree.ppt data structure programming
Lecture_10_Parallel_Algorithms_Part_II.ppt
Shortest path by using suitable algorithm.pdf
Randomized algorithms all pairs shortest path
Dijkstra Algorithm Presentation -the shortest path finding algorithm.pptx
Dijkstra's Algorithm
Dijkstra’S Algorithm
Daa chpater14
Dijesktra 1.ppt
Dijkstra's algorithm for computer science

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx

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.