SlideShare a Scribd company logo
1 of 11
DATA STRUCTURES & ALGORITHMS
SHORTEST PATH
BY
D SEETHALAKSHMI
ASSISTANT PROFESSOR OF COMPUTER APPLICATIONS
BON SECOURS COLLEGE FOR WOMEN
THANJAVUR
Shortest Path Problem-
In data structures,
 Shortest path problem is a problem of finding the shortest path(s) between vertices of a
given graph.
 Shortest path between two vertices is a path that has the least cost as compared to all other
existing paths.
Shortest Path Algorithms-
Shortest path algorithms are a family of algorithms used for solving the shortest path
problem.
Applications-
Shortest path algorithms have a wide range of applications such as in-
 Google Maps
 Road Networks
 Logistics Research
Types of Shortest Path Problem-
Various types of shortest path problem are-
1. Single-pair shortest path problem
2. Single-source shortest path problem
3. Single-destination shortest path problem
4. All pairs shortest path problem
Single-Pair Shortest Path Problem-
 It is a shortest path problem where the shortest path between a given pair of vertices is
computed.
 A* Search Algorithm is a famous algorithm used for solving single-pair shortest path
problem.
Single-Source Shortest Path Problem-
 It is a shortest path problem where the shortest path from a given source vertex to all other
remaining vertices is computed.
 Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for
solving single-source shortest path problem.
Single-Destination Shortest Path Problem-
 It is a shortest path problem where the shortest path from all the vertices to a single
destination vertex is computed.
 By reversing the direction of each edge in the graph, this problem reduces to single-source
shortest path problem.
 Dijkstra’s Algorithm is a famous algorithm adapted for solving single-destination shortest
path problem.
All Pairs Shortest Path Problem-
 It is a shortest path problem where the shortest path between every pair of vertices is
computed.
 Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for
solving All pairs shortest path problem.
Dijkstra Algorithm | Example | Time Complexity
Dijkstra Algorithm-
 Dijkstra Algorithm is a very famous greedy algorithm.
 It is used for solving the single source shortest path problem.
 It computes the shortest path from one particular source node to all other remaining nodes
of the graph.
Conditions-
It is important to note the following points regarding Dijkstra Algorithm-
 Dijkstra algorithm works only for connected graphs.
 Dijkstra algorithm works only for those graphs that do not contain any negative weight
edge.
 The actual Dijkstra algorithm does not output the shortest paths.
 It only provides the value or cost of the shortest paths.
 By making minor modifications in the actual algorithm, the shortest paths can be easily
obtained.
 Dijkstra algorithm works for directed as well as undirected graphs.
Dijkstra Algorithm-
dist[S] ← 0 // The distance to source vertex is set to 0
Π[S] ← NIL // The predecessor of source vertex is set as NIL
for all v ∈ V - {S} // For all other vertices
do dist[v] ← ∞ // All other distances are set to ∞
Π[v] ← NIL // The predecessor of all other vertices is set as NIL
S ← ∅ // The set of vertices that have been visited 'S' is initially empty
Q ← V // The queue 'Q' initially contains all the vertices
while Q ≠ ∅ // While loop executes till the queue is not empty
do u ← mindistance (Q, dist) // A vertex from Q with the least distance is selected
S ← S ∪ {u} // Vertex 'u' is added to 'S' list of vertices that have been visited
for all v ∈ neighbors[u] // For all the neighboring vertices of vertex 'u'
do if dist[v] > dist[u] + w(u,v) // if any new shortest path is discovered
then dist[v] ← dist[u] + w(u,v) // The new value of the shortest path is selected
return dist
Implementation-
The implementation of above Dijkstra Algorithm is explained in the following steps-
Step-01:
In the first step two sets are defined-
 One set contains all those vertices which have been included in the shortest path tree.
 In the beginning, this set is empty.
 Other set contains all those vertices which are still left to be included in the shortest path
tree.
 In the beginning, this set contains all the vertices of the given graph.
Step-02:
For each vertex of the given graph, two variables are defined as-
 Π[v] which denotes the predecessor of vertex ‘v’
 d[v] which denotes the shortest path estimate of vertex ‘v’ from the source vertex.
Initially, the value of these variables is set as-
 The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL
 The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0
 The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] = ∞
Step-03:
The following procedure is repeated until all the vertices of the graph are processed-
 Among unprocessed vertices, a vertex with minimum value of variable‘d’ is chosen.
 Its outgoing edges are relaxed.
 After relaxing the edges for that vertex, the sets created in step-01 are updated.
What is Edge Relaxation?
Consider the edge (a,b) in the following graph-
Here, d[a] and d[b] denotes the shortest path estimate for vertices a and b respectively from
the source vertex ‘S’.
Now,
If d[a] + w < d[b]
then d[b] = d[a] + w and Π[b] = a
This is called as edge relaxation.
PRACTICE PROBLEM BASED ON DIJKSTRA ALGORITHM-
Problem-
Using Dijkstra’s Algorithm, find the shortest distance from source vertex ‘S’ to remaining
vertices in the following graph-
Also, write the order in which the vertices are visited.
Solution-
Step-01:
The following two sets are created-
 Unvisited set : {S , a , b , c , d , e}
 Visited set : { }
Step-02:
The two variables Π and d are created for each vertex and initialized as-
 Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL
 d[S] = 0
 d[a] = d[b] = d[c] = d[d] = d[e] = ∞
Step-03:
 Vertex ‘S’ is chosen.
 This is because shortest path estimate for vertex ‘S’ is least.
 The outgoing edges of vertex ‘S’ are relaxed.
Before Edge Relaxation-
Now,
 d[S] + 1 = 0 + 1 = 1 < ∞
∴ d[a] = 1 and Π[a] = S
 d[S] + 5 = 0 + 5 = 5 < ∞
∴ d[b] = 5 and Π[b] = S
After edge relaxation, our shortest path tree is-
Now, the sets are updated as-
 Unvisited set : {a , b , c , d , e}
 Visited set : {S}
Step-04:
 Vertex ‘a’ is chosen.
 This is because shortest path estimate for vertex ‘a’ is least.
 The outgoing edges of vertex ‘a’ are relaxed.
Before Edge Relaxation-
Now,
 d[a] + 2 = 1 + 2 = 3 < ∞
∴ d[c] = 3 and Π[c] = a
 d[a] + 1 = 1 + 1 = 2 < ∞
∴ d[d] = 2 and Π[d] = a
 d[b] + 2 = 1 + 2 = 3 < 5
∴ d[b] = 3 and Π[b] = a
After edge relaxation, our shortest path tree is-
Now, the sets are updated as-
 Unvisited set : {b , c , d , e}
 Visited set : {S , a}
Step-05:
 Vertex ‘d’ is chosen.
 This is because shortest path estimate for vertex ‘d’ is least.
 The outgoing edges of vertex ‘d’ are relaxed.
Before Edge Relaxation-
Now,
 d[d] + 2 = 2 + 2 = 4 < ∞
∴ d[e] = 4 and Π[e] = d
After edge relaxation, our shortest path tree is-
Now, the sets are updated as-
 Unvisited set : {b , c , e}
 Visited set : {S , a , d}
Step-06:
 Vertex ‘b’ is chosen.
 This is because shortest path estimate for vertex ‘b’ is least.
 Vertex ‘c’ may also be chosen since for both the vertices, shortest path estimate is least.
 The outgoing edges of vertex ‘b’ are relaxed.
Before Edge Relaxation-
Now,
d[b] + 2 = 3 + 2 = 5 > 2
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Now, the sets are updated as-
 Unvisited set : {c , e}
 Visited set : {S , a , d , b}
Step-07:
 Vertex ‘c’ is chosen.
 This is because shortest path estimate for vertex ‘c’ is least.
 The outgoing edges of vertex ‘c’ are relaxed.
Before Edge Relaxation-
Now,
 d[c] + 1 = 3 + 1 = 4 = 4
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Now, the sets are updated as-
 Unvisited set : {e}
 Visited set : {S , a , d , b , c}
Step-08:
 Vertex ‘e’ is chosen.
 This is because shortest path estimate for vertex ‘e’ is least.
 The outgoing edges of vertex ‘e’ are relaxed.
 There are no outgoing edges for vertex ‘e’.
 So, our shortest path tree remains the same as in Step-05.
Now, the sets are updated as-
 Unvisited set : { }
 Visited set : {S , a , d , b , c , e}
Now,
 All vertices of the graph are processed.
 Our final shortest path tree is as shown below.
 It represents the shortest path from source vertex ‘S’ to all other remaining vertices.
The order in which all the vertices are processed is :
S , a , d , b , c , e.

More Related Content

What's hot

Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Presentation systemc
Presentation systemcPresentation systemc
Presentation systemcSUBRAHMANYA S
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 
Ch03 Mining Massive Data Sets stanford
Ch03 Mining Massive Data Sets  stanfordCh03 Mining Massive Data Sets  stanford
Ch03 Mining Massive Data Sets stanfordSakthivel C R
 
SOFTWARE MANUAL TESTING SYLLABUS
SOFTWARE MANUAL TESTING SYLLABUSSOFTWARE MANUAL TESTING SYLLABUS
SOFTWARE MANUAL TESTING SYLLABUSSHPINE TECHNOLOGIES
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QMLICS
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework DesignsSauce Labs
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligencesandeep54552
 
Parallel programming c++ win10 msmpi visual studio
Parallel programming c++ win10 msmpi visual studioParallel programming c++ win10 msmpi visual studio
Parallel programming c++ win10 msmpi visual studiopraveench1888
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design PatternsYnon Perek
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scriptingvceder
 
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdfUNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdfJenishaR1
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015Nir Kaufman
 
Introduction to Selenium grid
Introduction to Selenium gridIntroduction to Selenium grid
Introduction to Selenium gridKnoldus Inc.
 

What's hot (20)

Selenium
SeleniumSelenium
Selenium
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Presentation systemc
Presentation systemcPresentation systemc
Presentation systemc
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Ch03 Mining Massive Data Sets stanford
Ch03 Mining Massive Data Sets  stanfordCh03 Mining Massive Data Sets  stanford
Ch03 Mining Massive Data Sets stanford
 
SOFTWARE MANUAL TESTING SYLLABUS
SOFTWARE MANUAL TESTING SYLLABUSSOFTWARE MANUAL TESTING SYLLABUS
SOFTWARE MANUAL TESTING SYLLABUS
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QML
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
Gatling
Gatling Gatling
Gatling
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
Parallel programming c++ win10 msmpi visual studio
Parallel programming c++ win10 msmpi visual studioParallel programming c++ win10 msmpi visual studio
Parallel programming c++ win10 msmpi visual studio
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design Patterns
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdfUNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
 
C#.NET
C#.NETC#.NET
C#.NET
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
Introduction to Selenium grid
Introduction to Selenium gridIntroduction to Selenium grid
Introduction to Selenium grid
 
Test Coverage
Test CoverageTest Coverage
Test Coverage
 

Similar to Shortest Path Problem.docx

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
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
 
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
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...KUSHDHIRRA2111026030
 
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
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsSujith Jay Nair
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras algdouglaslyon
 
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.pdfzefergaming
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 

Similar to Shortest Path Problem.docx (20)

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
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
 
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...
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
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
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
 
Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
 
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
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
Unit ii-ppt
Unit ii-pptUnit ii-ppt
Unit ii-ppt
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Graph 3
Graph 3Graph 3
Graph 3
 

More from SeethaDinesh

Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptxSeethaDinesh
 
Generations of Computers.ppt
Generations of Computers.pptGenerations of Computers.ppt
Generations of Computers.pptSeethaDinesh
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.pptSeethaDinesh
 
Arrays in JAVA.ppt
Arrays in JAVA.pptArrays in JAVA.ppt
Arrays in JAVA.pptSeethaDinesh
 
PROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxPROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxSeethaDinesh
 
Cloud Computing Basics.pptx
Cloud Computing Basics.pptxCloud Computing Basics.pptx
Cloud Computing Basics.pptxSeethaDinesh
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
Greedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptGreedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptSeethaDinesh
 
Structure and Union.ppt
Structure and Union.pptStructure and Union.ppt
Structure and Union.pptSeethaDinesh
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptSeethaDinesh
 
NME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxNME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxSeethaDinesh
 
NME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfNME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfSeethaDinesh
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdfSeethaDinesh
 

More from SeethaDinesh (20)

Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptx
 
Generations of Computers.ppt
Generations of Computers.pptGenerations of Computers.ppt
Generations of Computers.ppt
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Arrays in JAVA.ppt
Arrays in JAVA.pptArrays in JAVA.ppt
Arrays in JAVA.ppt
 
PROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxPROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptx
 
Cloud Computing Basics.pptx
Cloud Computing Basics.pptxCloud Computing Basics.pptx
Cloud Computing Basics.pptx
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Greedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptGreedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.ppt
 
Structure and Union.ppt
Structure and Union.pptStructure and Union.ppt
Structure and Union.ppt
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
NME UNIT II.ppt
NME UNIT II.pptNME UNIT II.ppt
NME UNIT II.ppt
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
chapter 1.pptx
chapter 1.pptxchapter 1.pptx
chapter 1.pptx
 
DW unit 3.pptx
DW unit 3.pptxDW unit 3.pptx
DW unit 3.pptx
 
DW unit 2.pdf
DW unit 2.pdfDW unit 2.pdf
DW unit 2.pdf
 
DW Unit 1.pdf
DW Unit 1.pdfDW Unit 1.pdf
DW Unit 1.pdf
 
NME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxNME WPI UNIt 3.pptx
NME WPI UNIt 3.pptx
 
NME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfNME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdf
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdf
 

Recently uploaded

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Shortest Path Problem.docx

  • 1. DATA STRUCTURES & ALGORITHMS SHORTEST PATH BY D SEETHALAKSHMI ASSISTANT PROFESSOR OF COMPUTER APPLICATIONS BON SECOURS COLLEGE FOR WOMEN THANJAVUR
  • 2. Shortest Path Problem- In data structures,  Shortest path problem is a problem of finding the shortest path(s) between vertices of a given graph.  Shortest path between two vertices is a path that has the least cost as compared to all other existing paths. Shortest Path Algorithms- Shortest path algorithms are a family of algorithms used for solving the shortest path problem. Applications- Shortest path algorithms have a wide range of applications such as in-  Google Maps  Road Networks  Logistics Research Types of Shortest Path Problem- Various types of shortest path problem are- 1. Single-pair shortest path problem 2. Single-source shortest path problem 3. Single-destination shortest path problem 4. All pairs shortest path problem
  • 3. Single-Pair Shortest Path Problem-  It is a shortest path problem where the shortest path between a given pair of vertices is computed.  A* Search Algorithm is a famous algorithm used for solving single-pair shortest path problem. Single-Source Shortest Path Problem-  It is a shortest path problem where the shortest path from a given source vertex to all other remaining vertices is computed.  Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for solving single-source shortest path problem. Single-Destination Shortest Path Problem-  It is a shortest path problem where the shortest path from all the vertices to a single destination vertex is computed.  By reversing the direction of each edge in the graph, this problem reduces to single-source shortest path problem.  Dijkstra’s Algorithm is a famous algorithm adapted for solving single-destination shortest path problem. All Pairs Shortest Path Problem-  It is a shortest path problem where the shortest path between every pair of vertices is computed.  Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for solving All pairs shortest path problem. Dijkstra Algorithm | Example | Time Complexity Dijkstra Algorithm-  Dijkstra Algorithm is a very famous greedy algorithm.  It is used for solving the single source shortest path problem.  It computes the shortest path from one particular source node to all other remaining nodes of the graph.
  • 4. Conditions- It is important to note the following points regarding Dijkstra Algorithm-  Dijkstra algorithm works only for connected graphs.  Dijkstra algorithm works only for those graphs that do not contain any negative weight edge.  The actual Dijkstra algorithm does not output the shortest paths.  It only provides the value or cost of the shortest paths.  By making minor modifications in the actual algorithm, the shortest paths can be easily obtained.  Dijkstra algorithm works for directed as well as undirected graphs. Dijkstra Algorithm- dist[S] ← 0 // The distance to source vertex is set to 0 Π[S] ← NIL // The predecessor of source vertex is set as NIL for all v ∈ V - {S} // For all other vertices do dist[v] ← ∞ // All other distances are set to ∞ Π[v] ← NIL // The predecessor of all other vertices is set as NIL S ← ∅ // The set of vertices that have been visited 'S' is initially empty Q ← V // The queue 'Q' initially contains all the vertices while Q ≠ ∅ // While loop executes till the queue is not empty do u ← mindistance (Q, dist) // A vertex from Q with the least distance is selected S ← S ∪ {u} // Vertex 'u' is added to 'S' list of vertices that have been visited for all v ∈ neighbors[u] // For all the neighboring vertices of vertex 'u' do if dist[v] > dist[u] + w(u,v) // if any new shortest path is discovered then dist[v] ← dist[u] + w(u,v) // The new value of the shortest path is selected return dist Implementation- The implementation of above Dijkstra Algorithm is explained in the following steps- Step-01: In the first step two sets are defined-  One set contains all those vertices which have been included in the shortest path tree.  In the beginning, this set is empty.  Other set contains all those vertices which are still left to be included in the shortest path tree.  In the beginning, this set contains all the vertices of the given graph.
  • 5. Step-02: For each vertex of the given graph, two variables are defined as-  Π[v] which denotes the predecessor of vertex ‘v’  d[v] which denotes the shortest path estimate of vertex ‘v’ from the source vertex. Initially, the value of these variables is set as-  The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL  The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0  The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] = ∞ Step-03: The following procedure is repeated until all the vertices of the graph are processed-  Among unprocessed vertices, a vertex with minimum value of variable‘d’ is chosen.  Its outgoing edges are relaxed.  After relaxing the edges for that vertex, the sets created in step-01 are updated. What is Edge Relaxation? Consider the edge (a,b) in the following graph- Here, d[a] and d[b] denotes the shortest path estimate for vertices a and b respectively from the source vertex ‘S’. Now, If d[a] + w < d[b] then d[b] = d[a] + w and Π[b] = a This is called as edge relaxation.
  • 6. PRACTICE PROBLEM BASED ON DIJKSTRA ALGORITHM- Problem- Using Dijkstra’s Algorithm, find the shortest distance from source vertex ‘S’ to remaining vertices in the following graph- Also, write the order in which the vertices are visited. Solution- Step-01: The following two sets are created-  Unvisited set : {S , a , b , c , d , e}  Visited set : { } Step-02: The two variables Π and d are created for each vertex and initialized as-  Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL  d[S] = 0  d[a] = d[b] = d[c] = d[d] = d[e] = ∞ Step-03:  Vertex ‘S’ is chosen.  This is because shortest path estimate for vertex ‘S’ is least.  The outgoing edges of vertex ‘S’ are relaxed. Before Edge Relaxation-
  • 7. Now,  d[S] + 1 = 0 + 1 = 1 < ∞ ∴ d[a] = 1 and Π[a] = S  d[S] + 5 = 0 + 5 = 5 < ∞ ∴ d[b] = 5 and Π[b] = S After edge relaxation, our shortest path tree is- Now, the sets are updated as-  Unvisited set : {a , b , c , d , e}  Visited set : {S} Step-04:  Vertex ‘a’ is chosen.  This is because shortest path estimate for vertex ‘a’ is least.  The outgoing edges of vertex ‘a’ are relaxed.
  • 8. Before Edge Relaxation- Now,  d[a] + 2 = 1 + 2 = 3 < ∞ ∴ d[c] = 3 and Π[c] = a  d[a] + 1 = 1 + 1 = 2 < ∞ ∴ d[d] = 2 and Π[d] = a  d[b] + 2 = 1 + 2 = 3 < 5 ∴ d[b] = 3 and Π[b] = a After edge relaxation, our shortest path tree is- Now, the sets are updated as-
  • 9.  Unvisited set : {b , c , d , e}  Visited set : {S , a} Step-05:  Vertex ‘d’ is chosen.  This is because shortest path estimate for vertex ‘d’ is least.  The outgoing edges of vertex ‘d’ are relaxed. Before Edge Relaxation- Now,  d[d] + 2 = 2 + 2 = 4 < ∞ ∴ d[e] = 4 and Π[e] = d After edge relaxation, our shortest path tree is- Now, the sets are updated as-
  • 10.  Unvisited set : {b , c , e}  Visited set : {S , a , d} Step-06:  Vertex ‘b’ is chosen.  This is because shortest path estimate for vertex ‘b’ is least.  Vertex ‘c’ may also be chosen since for both the vertices, shortest path estimate is least.  The outgoing edges of vertex ‘b’ are relaxed. Before Edge Relaxation- Now, d[b] + 2 = 3 + 2 = 5 > 2 ∴ No change After edge relaxation, our shortest path tree remains the same as in Step-05. Now, the sets are updated as-  Unvisited set : {c , e}  Visited set : {S , a , d , b} Step-07:  Vertex ‘c’ is chosen.  This is because shortest path estimate for vertex ‘c’ is least.  The outgoing edges of vertex ‘c’ are relaxed. Before Edge Relaxation-
  • 11. Now,  d[c] + 1 = 3 + 1 = 4 = 4 ∴ No change After edge relaxation, our shortest path tree remains the same as in Step-05. Now, the sets are updated as-  Unvisited set : {e}  Visited set : {S , a , d , b , c} Step-08:  Vertex ‘e’ is chosen.  This is because shortest path estimate for vertex ‘e’ is least.  The outgoing edges of vertex ‘e’ are relaxed.  There are no outgoing edges for vertex ‘e’.  So, our shortest path tree remains the same as in Step-05. Now, the sets are updated as-  Unvisited set : { }  Visited set : {S , a , d , b , c , e} Now,  All vertices of the graph are processed.  Our final shortest path tree is as shown below.  It represents the shortest path from source vertex ‘S’ to all other remaining vertices. The order in which all the vertices are processed is : S , a , d , b , c , e.