Skip to content

Commit aab6d58

Browse files
authored
Merge pull request #134 from malikshubham827/master
Added Dijkstra Algorithm
2 parents 464408e + 4d4b0ff commit aab6d58

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

Diff for: data_structures/Graph/dijkstra_algorithm.py

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Title: Dijkstra's Algorithm for finding single source shortest path from scratch
2+
# Author: Shubham Malik
3+
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
4+
5+
import math
6+
import sys
7+
# For storing the vertex set to retreive node with the lowest distance
8+
9+
10+
class PriorityQueue:
11+
# Based on Min Heap
12+
def __init__(self):
13+
self.cur_size = 0
14+
self.array = []
15+
self.pos = {} # To store the pos of node in array
16+
17+
def isEmpty(self):
18+
return self.cur_size == 0
19+
20+
def min_heapify(self, idx):
21+
lc = self.left(idx)
22+
rc = self.right(idx)
23+
if lc < self.cur_size and self.array(lc)[0] < self.array(idx)[0]:
24+
smallest = lc
25+
else:
26+
smallest = idx
27+
if rc < self.cur_size and self.array(rc)[0] < self.array(smallest)[0]:
28+
smallest = rc
29+
if smallest != idx:
30+
self.swap(idx, smallest)
31+
self.min_heapify(smallest)
32+
33+
def insert(self, tup):
34+
# Inserts a node into the Priority Queue
35+
self.pos[tup[1]] = self.cur_size
36+
self.cur_size += 1
37+
self.array.append((sys.maxsize, tup[1]))
38+
self.decrease_key((sys.maxsize, tup[1]), tup[0])
39+
40+
def extract_min(self):
41+
# Removes and returns the min element at top of priority queue
42+
min_node = self.array[0][1]
43+
self.array[0] = self.array[self.cur_size - 1]
44+
self.cur_size -= 1
45+
self.min_heapify(1)
46+
del self.pos[min_node]
47+
return min_node
48+
49+
def left(self, i):
50+
# returns the index of left child
51+
return 2 * i + 1
52+
53+
def right(self, i):
54+
# returns the index of right child
55+
return 2 * i + 2
56+
57+
def par(self, i):
58+
# returns the index of parent
59+
return math.floor(i / 2)
60+
61+
def swap(self, i, j):
62+
# swaps array elements at indices i and j
63+
# update the pos{}
64+
self.pos[self.array[i][1]] = j
65+
self.pos[self.array[j][1]] = i
66+
temp = self.array[i]
67+
self.array[i] = self.array[j]
68+
self.array[j] = temp
69+
70+
def decrease_key(self, tup, new_d):
71+
idx = self.pos[tup[1]]
72+
# assuming the new_d is atmost old_d
73+
self.array[idx] = (new_d, tup[1])
74+
while idx > 0 and self.array[self.par(idx)][0] > self.array[idx][0]:
75+
self.swap(idx, self.par(idx))
76+
idx = self.par(idx)
77+
78+
79+
class Graph:
80+
def __init__(self, num):
81+
self.adjList = {} # To store graph: u -> (v,w)
82+
self.num_nodes = num # Number of nodes in graph
83+
# To store the distance from source vertex
84+
self.dist = [0] * self.num_nodes
85+
self.par = [-1] * self.num_nodes # To store the path
86+
87+
def add_edge(self, u, v, w):
88+
# Edge going from node u to v and v to u with weight w
89+
# u (w)-> v, v (w) -> u
90+
# Check if u already in graph
91+
if u in self.adjList.keys():
92+
self.adjList[u].append((v, w))
93+
else:
94+
self.adjList[u] = [(v, w)]
95+
96+
# Assuming undirected graph
97+
if v in self.adjList.keys():
98+
self.adjList[v].append((u, w))
99+
else:
100+
self.adjList[v] = [(u, w)]
101+
102+
def show_graph(self):
103+
# u -> v(w)
104+
for u in self.adjList:
105+
print(u, '->', ' -> '.join(str("{}({})".format(v, w))
106+
for v, w in self.adjList[u]))
107+
108+
def dijkstra(self, src):
109+
# Flush old junk values in par[]
110+
self.par = [-1] * self.num_nodes
111+
# src is the source node
112+
self.dist[src] = 0
113+
Q = PriorityQueue()
114+
Q.insert((0, src)) # (dist from src, node)
115+
for u in self.adjList.keys():
116+
if u != src:
117+
self.dist[u] = sys.maxsize # Infinity
118+
self.par[u] = -1
119+
120+
while not Q.isEmpty():
121+
u = Q.extract_min() # Returns node with the min dist from source
122+
# Update the distance of all the neighbours of u and
123+
# if their prev dist was INFINITY then push them in Q
124+
for v, w in self.adjList[u]:
125+
new_dist = self.dist[u] + w
126+
if self.dist[v] > new_dist:
127+
if self.dist[v] == sys.maxsize:
128+
Q.insert((new_dist, v))
129+
else:
130+
Q.decrease_key((self.dist[v], v), new_dist)
131+
self.dist[v] = new_dist
132+
self.par[v] = u
133+
134+
# Show the shortest distances from src
135+
self.show_distances(src)
136+
137+
def show_distances(self, src):
138+
print("Distance from node: {}".format(src))
139+
for u in range(self.num_nodes):
140+
print('Node {} has distance: {}'.format(u, self.dist[u]))
141+
142+
def show_path(self, src, dest):
143+
# To show the shortest path from src to dest
144+
# WARNING: Use it *after* calling dijkstra
145+
path = []
146+
cost = 0
147+
temp = dest
148+
# Backtracking from dest to src
149+
while self.par[temp] != -1:
150+
path.append(temp)
151+
if temp != src:
152+
for v, w in self.adjList[temp]:
153+
if v == self.par[temp]:
154+
cost += w
155+
break
156+
temp = self.par[temp]
157+
path.append(src)
158+
path.reverse()
159+
160+
print('----Path to reach {} from {}----'.format(dest, src))
161+
for u in path:
162+
print('{}'.format(u), end=' ')
163+
if u != dest:
164+
print('-> ', end='')
165+
166+
print('\nTotal cost of path: ', cost)
167+
168+
169+
if __name__ == '__main__':
170+
graph = Graph(9)
171+
graph.add_edge(0, 1, 4)
172+
graph.add_edge(0, 7, 8)
173+
graph.add_edge(1, 2, 8)
174+
graph.add_edge(1, 7, 11)
175+
graph.add_edge(2, 3, 7)
176+
graph.add_edge(2, 8, 2)
177+
graph.add_edge(2, 5, 4)
178+
graph.add_edge(3, 4, 9)
179+
graph.add_edge(3, 5, 14)
180+
graph.add_edge(4, 5, 10)
181+
graph.add_edge(5, 6, 2)
182+
graph.add_edge(6, 7, 1)
183+
graph.add_edge(6, 8, 6)
184+
graph.add_edge(7, 8, 7)
185+
graph.show_graph()
186+
graph.dijkstra(0)
187+
graph.show_path(0, 4)
188+
189+
# OUTPUT
190+
# 0 -> 1(4) -> 7(8)
191+
# 1 -> 0(4) -> 2(8) -> 7(11)
192+
# 7 -> 0(8) -> 1(11) -> 6(1) -> 8(7)
193+
# 2 -> 1(8) -> 3(7) -> 8(2) -> 5(4)
194+
# 3 -> 2(7) -> 4(9) -> 5(14)
195+
# 8 -> 2(2) -> 6(6) -> 7(7)
196+
# 5 -> 2(4) -> 3(14) -> 4(10) -> 6(2)
197+
# 4 -> 3(9) -> 5(10)
198+
# 6 -> 5(2) -> 7(1) -> 8(6)
199+
# Distance from node: 0
200+
# Node 0 has distance: 0
201+
# Node 1 has distance: 4
202+
# Node 2 has distance: 12
203+
# Node 3 has distance: 19
204+
# Node 4 has distance: 21
205+
# Node 5 has distance: 11
206+
# Node 6 has distance: 9
207+
# Node 7 has distance: 8
208+
# Node 8 has distance: 14
209+
# ----Path to reach 4 from 0----
210+
# 0 -> 7 -> 6 -> 5 -> 4
211+
# Total cost of path: 21

0 commit comments

Comments
 (0)