forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.py
55 lines (45 loc) · 1.98 KB
/
dijkstra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# i checked your other py files and decided that i would go for default dict
# i used same structure as needed for this repository.
from collections import defaultdict
import sys
r = range
# min weight goes for 0 in this case
max_weight = sys.maxsize
class Graph:
# Class initializer
def __init__(self, vertices):
# num of vertices and our starting graph
self.vertices = vertices
# Values will be [[]] two deminsial array with
# columns for start going to rows.
self.graph = [[0] * self.vertices for _ in r(self.vertices)]
self.visited = [0] * self.vertices # to control visited vertices
# for our distances to minimaze them.
self.distances = [max_weight] * self.vertices
# Add edge to graph start --> end point with specific weight!
def add_edge(self, start, end, weight):
self.graph[start][end] = weight
def print_dist(self, dist):
for _ in r(self.vertices):
print("vert ", _, "\tdist ", self.distances[_])
def dijkstra(self, end_point):
self.distances[end_point] = 0
for vert in r(self.vertices):
# we need to check for minimum but not visited!!
my_min, min_index = max_weight, 0
for _ in r(self.vertices):
if not self.visited[_]:
if my_min > self.distances[_]:
my_min = self.distances[_]
min_index = _
# check the flag for visited
self.visited[min_index] = 1
# # iterate and update if needed
for adj in r(self.vertices):
# check for edge and visited
val = self.graph[min_index][adj]
if not self.visited[adj] and val != 0:
# check if needed update
if self.distances[adj] > self.distances[min_index] + val:
self.distances[adj] = self.distances[min_index] + val
self.print_dist(self.distances)