Hello, this is #day_29, I’m going to talk about Dijkstra’s shortest path algorithm
Definition of Dijkstra’s shortest path algorithm
dijkstra’s shortest path is an algorithm created by Dr. Edsger Dijkstra for calculating the shortest path between vertices in a weighted graph (types of graph)
Example of a weighted graph
Dijkstra’s shortest path applications
- Road networks
- Electricity lines
- Google maps
- Social networking apps
- Flighting agenda
- Ip routing
more details
Dijkstra’s shortest path implementation in python
If you are not familiar with Python check this series by @aatmaj
def dijkstraShortestPathAlgo(nodes, edges, source_indedx = 0):
"""
code from
=> [https://www.youtube.com/watch?v=VnTlW572Sc4]
"""
path_lengths = {v:float('inf') for v in nodes}
path_lengths[source_index] = 0
adjacent_nodes = {v:{} for v in nodes}
for (u, v), w_uv in edges.items():
adjacent_nodes[u][v] = w_uv
adjacent_nodes[v][u] = w_uv
temporary_nodes = [v for v in nodes]
while len(temporary_nodes) > 0:
upper_bounds = {v: path_lengths[v] for v in temporary_nodes}
u = min(upper_bounds, key=upper_bounds.get)
temporary_nodes.remove(u)
for v, w_uv in adjacent_nodes[u].items():
path_lengths[v] = min(path_lengths[v], path_lengths[u] + w_uv)
return path_lengths
References & useful resources
- https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
- https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
- https://www.freecodecamp.org/news/dijkstras-shortest-path-algorithm-visual-introduction/
- https://www.youtube.com/watch?v=smHnz2RHJBY
Happy coding!!
Print
Share
Comment
Cite
Upload
Translate
APA
Aya Bouchiha | Sciencx (2024-03-29T14:37:33+00:00) » Dijkstra’s shortest path algorithm. Retrieved from https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/.
MLA" » Dijkstra’s shortest path algorithm." Aya Bouchiha | Sciencx - Sunday July 11, 2021, https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/
HARVARDAya Bouchiha | Sciencx Sunday July 11, 2021 » Dijkstra’s shortest path algorithm., viewed 2024-03-29T14:37:33+00:00,<https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/>
VANCOUVERAya Bouchiha | Sciencx - » Dijkstra’s shortest path algorithm. [Internet]. [Accessed 2024-03-29T14:37:33+00:00]. Available from: https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/
CHICAGO" » Dijkstra’s shortest path algorithm." Aya Bouchiha | Sciencx - Accessed 2024-03-29T14:37:33+00:00. https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/
IEEE" » Dijkstra’s shortest path algorithm." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/. [Accessed: 2024-03-29T14:37:33+00:00]
rf:citation » Dijkstra’s shortest path algorithm | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/07/11/dijkstras-shortest-path-algorithm/ | 2024-03-29T14:37:33+00:00
https://github.com/addpipe/simple-recorderjs-demo