Skip to content

Commit 1fbc579

Browse files
Monal5031aviaryan
authored andcommitted
Added Dijkstra Algorithm [Python] (iiitv#291)
1 parent 4e5629f commit 1fbc579

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
2020
| [Coin Change Problem](http://www.algorithmist.com/index.php/Coin_Change) | [:white_check_mark:](coin_change_problem/coin_change_problem.c) | | [:white_check_mark:](coin_change_problem/CoinChange.java) | [:white_check_mark:](coin_change_problem/coin_change_problem.py) | | |
2121
| [Counting Sort](http://www.geeksforgeeks.org/counting-sort/)| [:white_check_mark:](counting_sort/counting_sort.c) | | [:white_check_mark:](counting_sort/CountingSort.java) | | | |
2222
| [Depth First Traversal](http://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/) | | | [:white_check_mark:](depth_first_traversal/DepthFirstTraversal.java) | | | |
23-
| [Dijkstra Algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm/) | [:white_check_mark:](dijkstra/dijkstra.c) | | [:white_check_mark:](dijkstra/Dijkstra.java) | | | |
23+
| [Dijkstra Algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm/) | [:white_check_mark:](dijkstra/dijkstra.c) | | [:white_check_mark:](dijkstra/Dijkstra.java) | [:white_check_mark:](dijkstra/dijkstra.py) | | |
2424
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.c) | | [:white_check_mark:](euclidean_gcd/EuclideanGCD.java) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.py) | | |
2525
| [Exponentation by Squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring) | [:white_check_mark:](exponentation_by_squaring/exponentation_by_squaring.c) | | | [:white_check_mark:](exponentation_by_squaring/exponentation_by_squaring.py) | | |
2626
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | [:white_check_mark:](heap_sort/heap_sort.c) | | [:white_check_mark:](heap_sort/HeapSort.java) | [:white_check_mark:](heap_sort/heap_sort.py) | | |

dijkstra/dijkstra.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Dijktra's shortest path algorithm. Finds the path and distance from source to target.
3+
To add an edge between vertex a and b with distance dist:
4+
graph[a].append((b, dist)) and graph[b].append((a, dist)).
5+
"""
6+
7+
8+
import heapq
9+
10+
11+
def dijkstra(graph, source, target):
12+
"""
13+
Finds the shortest path and shortest distance from source to target
14+
15+
:param graph: Dictionary linking the vertices using list of tuples
16+
:param source: The vertex to start the path.
17+
:param target: The vertex to end the path.
18+
:return: shortest path and distance from source to target
19+
"""
20+
INF = float('Inf')
21+
predecessors = {x: x for x in graph}
22+
distances = {x: INF for x in graph}
23+
distances[source] = 0
24+
temp = []
25+
heapq.heappush(temp, [source, distances[source]])
26+
27+
while temp:
28+
u = heapq.heappop(temp)
29+
u_dist = u[1]
30+
u_idx = u[0]
31+
if u_dist == distances[u_idx]:
32+
for v in graph[u_idx]:
33+
v_idx = v[0]
34+
u2v = v[1]
35+
if distances[u_idx] + u2v < distances[v_idx]:
36+
distances[v_idx] = distances[u_idx] + u2v
37+
heapq.heappush(temp, [v_idx, distances[v_idx]])
38+
predecessors[v_idx] = u_idx
39+
40+
if distances[target] == INF:
41+
return None, None
42+
else:
43+
path = []
44+
vertex = target
45+
while True:
46+
path.append(str(vertex))
47+
if vertex == predecessors[vertex]:
48+
break
49+
vertex = predecessors[vertex]
50+
return path[::-1], distances[target]
51+
52+
53+
def main():
54+
"""
55+
driver function to test the dijkstra's algorithm
56+
"""
57+
graph = {'s': [('a', 2), ('b', 1)],
58+
'a': [('s', 3), ('b', 4), ('c', 8)],
59+
'b': [('s', 4), ('a', 2), ('d', 2)],
60+
'c': [('a', 2), ('d', 7), ('t', 4)],
61+
'd': [('b', 1), ('c', 11), ('t', 5)],
62+
't': [('c', 3), ('d', 5)]
63+
}
64+
source = 'a'
65+
target = 't'
66+
path, cost = dijkstra(graph, source, target)
67+
print('The path from ' + source + ' to ' + target + ' :')
68+
if path is None or cost is None:
69+
print('does not exist')
70+
else:
71+
print(str(path) + ' with cost: ' + str(cost))
72+
73+
74+
if __name__ == "__main__":
75+
main()

0 commit comments

Comments
 (0)