SlideShare une entreprise Scribd logo
Shortest Path Algorithm
CS 250 Data Structures and Algorithms
Prof. Dr. Faisal Shafait
1 / 16
Shortest path
Learning Objectives
In this lecture you will learn
The need to calculate the shortest path and its various
applications
Implementation of Dijkstra’s Algorithm
2 / 16
Shortest path
Shortest path
Goal. find the shortest path and its length
Assumption. weights on all edges is a positive number.
negative vertices may end up in a cycle; each pass decreasing the
total length
3 / 16
Shortest path
Shortest path: applications
Circuit design. time taken for a change in input to affect an output
Internet. Packets are passed from the source, through routers, to
their destination
considers low latency ( to minimize time), or
minimum hop count
Maps. finding shortest route between to points on a map
rush hour
scheduled events (e.g., road construction)
unscheduled events (e.g., accidents)
Courier company. to minimize the cost of different factors:
salary of the truck driver
tolls costs
bonuses for being early
penalties for being late
cost of fuel
4 / 16
Shortest path
Dijkstra’s Algorithm
Input.
a directed weighted graph
Basic idea.
FIND an unvisited vertex vcur with shortest distance to it
MARK it as visited
FOR EACH adjacent unvisited vertex vadj to the vertex vcur:
ADD weight of the connecting edge to distance of vertex vcur, gives
dcur
CONDITION if dcur < dadj , update dadj and set vadj parent to vcurrent
Halting point.
when all vertices are visited
Output.
a shortest path from source vertex to destination vertex
5 / 16
Shortest path
Dijkstra’s Algorithm
Initialize the table.
visited to false.
cost to ∞.
parent to 0.
Vertex Visited Distance Parent
1 F ∞ 0
2 F ∞ 0
3 F ∞ 0
4 F ∞ 0
5 F ∞ 0
6 F ∞ 0
7 F ∞ 0
8 F ∞ 0
9 F ∞ 0
6 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 1.
cost to 0.
three paths: (1,2), (1,4) and (1,5) with costs 4, 1 and 8.
Vertex Visited Distance Parent
1 T 0 0
2 F 4 1
3 F ∞ 0
4 F 1 1
5 F 8 1
6 F ∞ 0
7 F ∞ 0
8 F ∞ 0
9 F ∞ 0
7 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 4, the minimum-cost unvisited vertex.
three paths: (1,4,5), (1,4,7) and (1,4,8) with costs 12, 10 and 9.
ignore path (1,4,5); there is a shorter path to vertex 5.
Vertex Visited Distance Parent
1 T 0 0
2 F 4 1
3 F ∞ 0
4 T 1 1
5 F 8 1
6 F ∞ 0
7 F 10 4
8 F 9 4
9 F ∞ 0
8 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 2, the minimum-cost unvisited vertex.
three paths: (1,2,3), (1,2,5) and (1,2,6) with costs 5, 10 and 5.
ignore path (1,2,5); there is a shorter path to vertex 5.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 F 5 2
4 T 1 1
5 F 8 1
6 F 5 2
7 F 10 4
8 F 9 4
9 F ∞ 0
9 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 3, the minimum-cost unvisited vertex.
three paths: (1,2,3,5) and (1,2,3,6) with costs 7 and 12.
ignore path (1,2,3,6); there is a shorter path to vertex 6.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 F 7 3
6 F 5 2
7 F 10 4
8 F 9 4
9 F ∞ 0
10 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 6, the minimum-cost unvisited vertex.
paths: (1,2,6,8) and (1,2,6,9) with costs 12 and 13.
ignore path (1,2,3,8); there is a shorter path to vertex 8.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 F 7 3
6 T 5 2
7 F 10 4
8 F 9 4
9 F 13 6
11 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 5, the minimum-cost unvisited vertex.
paths: (1,2,3,5,7), (1,2,3,5,8) and (1,2,3,5,9) with costs 8, 8 and 15.
ignore path (1,2,3,5,9); there is a shorter path to vertex 9.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 T 7 3
6 T 5 2
7 F 8 5
8 F 8 5
9 F 13 6
12 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 7, the minimum-cost unvisited vertex.
one paths: (1,2,3,5,7,8) with cost 10.
ignore the path; there is a shorter path to vertex 8.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 T 7 3
6 T 5 2
7 T 8 5
8 F 8 5
9 F 13 6
13 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 8, the minimum-cost unvisited vertex.
one path: (1,2,3,5,8,9) with cost 11.
update the cost to reach 11.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 T 7 3
6 T 5 2
7 T 8 5
8 T 8 5
9 F 11 8
14 / 16
Shortest path
Dijkstra’s Algorithm
Visit vertex 9, the minimum-cost unvisited vertex.
one path: (1,2,3,5,8,9) with cost 11.
this is the shortest path from vertex 1 to vertex 9.
Vertex Visited Distance Parent
1 T 0 0
2 T 4 1
3 T 5 2
4 T 1 1
5 T 7 3
6 T 5 2
7 T 8 5
8 T 8 5
9 T 11 8
15 / 16
Shortest path
Reading Material
For further reading, please refer to
Data Structures by Weiss: Chapter 15, Sections 15.3
https://www.programiz.com/dsa/dijkstra-algorithm
https://www.cs.usfca.edu/ galles/visualization/Dijkstra.html
16 / 16

Contenu connexe

Similaire à 24. Shortest Path Algorithm.pdf

Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path Algorithm
Anish Ansari
 
The shortest not necessarily the best. other path on the basis of the optimal...
The shortest not necessarily the best. other path on the basis of the optimal...The shortest not necessarily the best. other path on the basis of the optimal...
The shortest not necessarily the best. other path on the basis of the optimal...
eSAT Journals
 
The shortest not necessarily the best other path on the basis of the optimal ...
The shortest not necessarily the best other path on the basis of the optimal ...The shortest not necessarily the best other path on the basis of the optimal ...
The shortest not necessarily the best other path on the basis of the optimal ...
eSAT Publishing House
 
Flight-schedule using Dijkstra's algorithm with comparison of routes findings
Flight-schedule using Dijkstra's algorithm with comparison of  routes findingsFlight-schedule using Dijkstra's algorithm with comparison of  routes findings
Flight-schedule using Dijkstra's algorithm with comparison of routes findings
IJECEIAES
 
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
 
Algo labpresentation a_group
Algo labpresentation a_groupAlgo labpresentation a_group
Algo labpresentation a_group
Umme habiba
 
Week13 lec2
Week13 lec2Week13 lec2
Week13 lec2
syedhaiderraza
 
Module 3- transport_layer .pptx
Module 3- transport_layer           .pptxModule 3- transport_layer           .pptx
Module 3- transport_layer .pptx
hariprasad279825
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
dsa.pptx
dsa.pptxdsa.pptx
Lecture set 5
Lecture set 5Lecture set 5
Lecture set 5
Gopi Saiteja
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
Kàŕtheek Jåvvàjí
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
Tamzida_Azad
 
Lec32
Lec32Lec32
12 routing
12 routing12 routing
12 routing
Sanjay Thakare
 
A* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdfA* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdf
CS With Logic
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
sakthibalabalamuruga
 
Shortest path algorithm
Shortest  path algorithmShortest  path algorithm
Shortest path algorithm
Subrata Kumer Paul
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
AndrewJohnson866415
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problem
pooja saini
 

Similaire à 24. Shortest Path Algorithm.pdf (20)

Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path Algorithm
 
The shortest not necessarily the best. other path on the basis of the optimal...
The shortest not necessarily the best. other path on the basis of the optimal...The shortest not necessarily the best. other path on the basis of the optimal...
The shortest not necessarily the best. other path on the basis of the optimal...
 
The shortest not necessarily the best other path on the basis of the optimal ...
The shortest not necessarily the best other path on the basis of the optimal ...The shortest not necessarily the best other path on the basis of the optimal ...
The shortest not necessarily the best other path on the basis of the optimal ...
 
Flight-schedule using Dijkstra's algorithm with comparison of routes findings
Flight-schedule using Dijkstra's algorithm with comparison of  routes findingsFlight-schedule using Dijkstra's algorithm with comparison of  routes findings
Flight-schedule using Dijkstra's algorithm with comparison of routes findings
 
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...
 
Algo labpresentation a_group
Algo labpresentation a_groupAlgo labpresentation a_group
Algo labpresentation a_group
 
Week13 lec2
Week13 lec2Week13 lec2
Week13 lec2
 
Module 3- transport_layer .pptx
Module 3- transport_layer           .pptxModule 3- transport_layer           .pptx
Module 3- transport_layer .pptx
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
Lecture set 5
Lecture set 5Lecture set 5
Lecture set 5
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Lec32
Lec32Lec32
Lec32
 
12 routing
12 routing12 routing
12 routing
 
A* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdfA* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdf
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Shortest path algorithm
Shortest  path algorithmShortest  path algorithm
Shortest path algorithm
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problem
 

Dernier

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 

Dernier (20)

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 

24. Shortest Path Algorithm.pdf

  • 1. Shortest Path Algorithm CS 250 Data Structures and Algorithms Prof. Dr. Faisal Shafait 1 / 16
  • 2. Shortest path Learning Objectives In this lecture you will learn The need to calculate the shortest path and its various applications Implementation of Dijkstra’s Algorithm 2 / 16
  • 3. Shortest path Shortest path Goal. find the shortest path and its length Assumption. weights on all edges is a positive number. negative vertices may end up in a cycle; each pass decreasing the total length 3 / 16
  • 4. Shortest path Shortest path: applications Circuit design. time taken for a change in input to affect an output Internet. Packets are passed from the source, through routers, to their destination considers low latency ( to minimize time), or minimum hop count Maps. finding shortest route between to points on a map rush hour scheduled events (e.g., road construction) unscheduled events (e.g., accidents) Courier company. to minimize the cost of different factors: salary of the truck driver tolls costs bonuses for being early penalties for being late cost of fuel 4 / 16
  • 5. Shortest path Dijkstra’s Algorithm Input. a directed weighted graph Basic idea. FIND an unvisited vertex vcur with shortest distance to it MARK it as visited FOR EACH adjacent unvisited vertex vadj to the vertex vcur: ADD weight of the connecting edge to distance of vertex vcur, gives dcur CONDITION if dcur < dadj , update dadj and set vadj parent to vcurrent Halting point. when all vertices are visited Output. a shortest path from source vertex to destination vertex 5 / 16
  • 6. Shortest path Dijkstra’s Algorithm Initialize the table. visited to false. cost to ∞. parent to 0. Vertex Visited Distance Parent 1 F ∞ 0 2 F ∞ 0 3 F ∞ 0 4 F ∞ 0 5 F ∞ 0 6 F ∞ 0 7 F ∞ 0 8 F ∞ 0 9 F ∞ 0 6 / 16
  • 7. Shortest path Dijkstra’s Algorithm Visit vertex 1. cost to 0. three paths: (1,2), (1,4) and (1,5) with costs 4, 1 and 8. Vertex Visited Distance Parent 1 T 0 0 2 F 4 1 3 F ∞ 0 4 F 1 1 5 F 8 1 6 F ∞ 0 7 F ∞ 0 8 F ∞ 0 9 F ∞ 0 7 / 16
  • 8. Shortest path Dijkstra’s Algorithm Visit vertex 4, the minimum-cost unvisited vertex. three paths: (1,4,5), (1,4,7) and (1,4,8) with costs 12, 10 and 9. ignore path (1,4,5); there is a shorter path to vertex 5. Vertex Visited Distance Parent 1 T 0 0 2 F 4 1 3 F ∞ 0 4 T 1 1 5 F 8 1 6 F ∞ 0 7 F 10 4 8 F 9 4 9 F ∞ 0 8 / 16
  • 9. Shortest path Dijkstra’s Algorithm Visit vertex 2, the minimum-cost unvisited vertex. three paths: (1,2,3), (1,2,5) and (1,2,6) with costs 5, 10 and 5. ignore path (1,2,5); there is a shorter path to vertex 5. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 F 5 2 4 T 1 1 5 F 8 1 6 F 5 2 7 F 10 4 8 F 9 4 9 F ∞ 0 9 / 16
  • 10. Shortest path Dijkstra’s Algorithm Visit vertex 3, the minimum-cost unvisited vertex. three paths: (1,2,3,5) and (1,2,3,6) with costs 7 and 12. ignore path (1,2,3,6); there is a shorter path to vertex 6. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 F 7 3 6 F 5 2 7 F 10 4 8 F 9 4 9 F ∞ 0 10 / 16
  • 11. Shortest path Dijkstra’s Algorithm Visit vertex 6, the minimum-cost unvisited vertex. paths: (1,2,6,8) and (1,2,6,9) with costs 12 and 13. ignore path (1,2,3,8); there is a shorter path to vertex 8. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 F 7 3 6 T 5 2 7 F 10 4 8 F 9 4 9 F 13 6 11 / 16
  • 12. Shortest path Dijkstra’s Algorithm Visit vertex 5, the minimum-cost unvisited vertex. paths: (1,2,3,5,7), (1,2,3,5,8) and (1,2,3,5,9) with costs 8, 8 and 15. ignore path (1,2,3,5,9); there is a shorter path to vertex 9. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 T 7 3 6 T 5 2 7 F 8 5 8 F 8 5 9 F 13 6 12 / 16
  • 13. Shortest path Dijkstra’s Algorithm Visit vertex 7, the minimum-cost unvisited vertex. one paths: (1,2,3,5,7,8) with cost 10. ignore the path; there is a shorter path to vertex 8. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 T 7 3 6 T 5 2 7 T 8 5 8 F 8 5 9 F 13 6 13 / 16
  • 14. Shortest path Dijkstra’s Algorithm Visit vertex 8, the minimum-cost unvisited vertex. one path: (1,2,3,5,8,9) with cost 11. update the cost to reach 11. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 T 7 3 6 T 5 2 7 T 8 5 8 T 8 5 9 F 11 8 14 / 16
  • 15. Shortest path Dijkstra’s Algorithm Visit vertex 9, the minimum-cost unvisited vertex. one path: (1,2,3,5,8,9) with cost 11. this is the shortest path from vertex 1 to vertex 9. Vertex Visited Distance Parent 1 T 0 0 2 T 4 1 3 T 5 2 4 T 1 1 5 T 7 3 6 T 5 2 7 T 8 5 8 T 8 5 9 T 11 8 15 / 16
  • 16. Shortest path Reading Material For further reading, please refer to Data Structures by Weiss: Chapter 15, Sections 15.3 https://www.programiz.com/dsa/dijkstra-algorithm https://www.cs.usfca.edu/ galles/visualization/Dijkstra.html 16 / 16