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
Introduction to Graph Theory
PPT
dijkstra algo.ppt
PPTX
Kruskal’s Algorithm
PPT
Graph theory concepts complex networks presents-rouhollah nabati
PDF
Shortest Path in Graph
PDF
Euler and hamilton paths
PDF
Quick sort
PPTX
Graph theory
Introduction to Graph Theory
dijkstra algo.ppt
Kruskal’s Algorithm
Graph theory concepts complex networks presents-rouhollah nabati
Shortest Path in Graph
Euler and hamilton paths
Quick sort
Graph theory

What's hot (20)

PDF
Shortest path algorithms
PPTX
Dijkstra's algorithm presentation
PPTX
Dijkstra s algorithm
PPTX
Graph Theory
PPT
Lattice lecture Final DM Updated2.ppt
PPTX
Interesting applications of graph theory
PPT
Graph theory presentation
PPTX
Dijkstra’S Algorithm
PPTX
Kruskal's algorithm
PPTX
Slides Chapter10.1 10.2
PDF
Graph theory and its applications
PPTX
Graph theory and life
PDF
Discrete Mathematics in Real Life ppt.pdf
PPTX
Graph theory introduction - Samy
PPTX
Dijkstra’s algorithm
PPTX
Graph Theory
PPTX
Graph Theory
PDF
Introduction to Graph Theory
PPT
Graphs bfs dfs
PPTX
Dijkstra's Algorithm
Shortest path algorithms
Dijkstra's algorithm presentation
Dijkstra s algorithm
Graph Theory
Lattice lecture Final DM Updated2.ppt
Interesting applications of graph theory
Graph theory presentation
Dijkstra’S Algorithm
Kruskal's algorithm
Slides Chapter10.1 10.2
Graph theory and its applications
Graph theory and life
Discrete Mathematics in Real Life ppt.pdf
Graph theory introduction - Samy
Dijkstra’s algorithm
Graph Theory
Graph Theory
Introduction to Graph Theory
Graphs bfs dfs
Dijkstra's Algorithm
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
PPTX
Discrete Mathematics Presentation
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
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
Discrete Mathematics Presentation
Shortest Path Problem
Intel Core i7 Processors
Intel I3,I5,I7 Processor
Unix command-line tools
Network Problem CPM & PERT
Ad

Similar to Unit26 shortest pathalgorithm (20)

PPTX
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
PDF
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
PDF
Lecture 16 - Dijkstra's Algorithm.pdf
PPTX
dms slide discrete mathematics sem 2 engineering
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
PPTX
Dijkstras-Algorithm-Finding-the-Shortest-Path.pptx
PDF
Dijkstra algorithm
PPT
2.6 all pairsshortestpath
PPTX
Algo labpresentation a_group
PPT
Dijkstra algorithm ds 57612334t4t44.ppt
PPT
Dijkstra Shortest Path Algorithm in Network.ppt
PDF
Djikstra’s Algorithm. Approach to shortest path algorithm with greedy method
PPT
Dijkstra.ppt
PPTX
Dijkstra Algorithm Presentation -the shortest path finding algorithm.pptx
PPT
lecture2faafffffffffffffffaaaaaaaaaaa4.ppt
PPTX
15-bellmanFord.pptx...........................................
PPTX
Shortest-Path Problems - Graph Theory in Computer Applications
PPT
Dijkstra's algorithm for computer science
PPTX
Shortest Path.pptx
PPT
(148065320) dijistra algo
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
dms slide discrete mathematics sem 2 engineering
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
Dijkstras-Algorithm-Finding-the-Shortest-Path.pptx
Dijkstra algorithm
2.6 all pairsshortestpath
Algo labpresentation a_group
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
Djikstra’s Algorithm. Approach to shortest path algorithm with greedy method
Dijkstra.ppt
Dijkstra Algorithm Presentation -the shortest path finding algorithm.pptx
lecture2faafffffffffffffffaaaaaaaaaaa4.ppt
15-bellmanFord.pptx...........................................
Shortest-Path Problems - Graph Theory in Computer Applications
Dijkstra's algorithm for computer science
Shortest Path.pptx
(148065320) dijistra algo

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
PDF
Electronic commerce courselecture one. Pdf
PDF
REPORT: Heating appliances market in Poland 2024
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced IT Governance
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
DevOps & Developer Experience Summer BBQ
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
CroxyProxy Instagram Access id login.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
madgavkar20181017ppt McKinsey Presentation.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development
Electronic commerce courselecture one. Pdf
REPORT: Heating appliances market in Poland 2024
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Event Presentation Google Cloud Next Extended 2025
NewMind AI Monthly Chronicles - July 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
GamePlan Trading System Review: Professional Trader's Honest Take
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced IT Governance
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
DevOps & Developer Experience Summer BBQ
Sensors and Actuators in IoT Systems using pdf
CroxyProxy Instagram Access id login.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.