-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
79 lines (57 loc) · 2.28 KB
/
graph.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#+======================================================================================+
# |Authors : Kolli Hrudhay,Narendrababu S,Akshat Gupta,Potturi Mourya Chandra
# |Package : Hospital Emergency
# |Module : graph.py
# |Language : Python 3.7
# |Description : This is the module (which is called by the Assignment_code.py
# module) in which the graph is traversed, the shortest distance
# and path is calculated using Dijkstra’s algorithm.
#
#
#+======================================================================================+
import sys
class Graph:
def __init__(self,nodes):
# Constructer for the Graph class
self.nodes=nodes
self.graph=[[0 for i in range(nodes)] for j in range(nodes)]
def minDist(self,dist,spSet):
# This method will find the node with the minimum distance from a node
# that is not yet included in the shortest path graph
# and return the former node to the calling method(spAlgo).
mindist=sys.maxsize
for i in range(self.nodes):
if dist[i] < mindist and spSet[i]==False:
mindist=dist[i]
min_index=i
return min_index
def spAlgo(self,src):
# This method will find/identify the shortest distance and the path from a node to the other
# by implemeting Dijkstra's single source shortest path algorithm
# for a graph that is represented using adjacency matrix representation.
maxnum=sys.maxsize
dist=[maxnum]*self.nodes
dist[src]=0
spSet=[False]*self.nodes
parent=[-1]*self.nodes
for _ in range(self.nodes):
ind1=self.minDist(dist,spSet)
spSet[ind1]=True
for ind2 in range(self.nodes):
if self.graph[ind1][ind2] > 0 and spSet[ind2] == False and dist[ind2] > dist[ind1] + self.graph[ind1][ind2]:
dist[ind2]=dist[ind1] + self.graph[ind1][ind2]
parent[ind2]=ind1
dict={}
for i in range(1,self.nodes):
temp=[]
temp=self.path(parent,i,temp)
dict[i]=[dist[i],temp]
return dict
def path(self,parent,i,temp):
# This method will create the min distance path for each node from the source node.
if parent[i]==-1:
temp.append(0)
return
self.path(parent,parent[i],temp)
temp.append(i)
return temp