-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
206 lines (167 loc) · 4.75 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import app
import pandas as pd
import decorganc
dista=pd.read_csv('static/Database/Distances.csv')
hosp=pd.read_csv('static/Database/hospitals.csv')
def shortestsorting(sourcee, organname):
dista=pd.read_csv('static/Database/Distances.csv')
hosp=pd.read_csv('static/Database/hospitals.csv')
match organname:
case 'cornea':
organind = 4
case 'heart':
organind = 5
case 'kidney':
organind = 6
case 'liver':
organind = 7
case 'lung':
organind = 8
case 'pancreas':
organind = 9
distances = []
class Graph():
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
def printSolution(self, dist):
print("Vertex \t Distance from Source")
for node in range(self.V):
print(node, "\t\t", dist[node])
# A utility function to find the vertex with
# minimum distance value, from the set of vertices
# not yet included in shortest path tree
def minDistance(self, dist, sptSet):
# Initialize minimum distance for next node
min = 1e7
# Search not nearest vertex not in the
# shortest path tree
for v in range(self.V):
if dist[v] < min and sptSet[v] == False:
min = dist[v]
min_index = v
return min_index
# Function that implements Dijkstra's single source
# shortest path algorithm for a graph represented
# using adjacency matrix representation
def dijkstra(self, src):
dist = [1e7] * self.V
dist[src] = 0
sptSet = [False] * self.V
for cout in range(self.V):
# Pick the minimum distance vertex from
# the set of vertices not yet processed.
# u is always equal to src in first iteration
u = self.minDistance(dist, sptSet)
# Put the minimum distance vertex in the
# shortest path tree
sptSet[u] = True
# Update dist value of the adjacent vertices
# of the picked vertex only if the current
# distance is greater than new distance and
# the vertex in not in the shortest path tree
for v in range(self.V):
if (self.graph[u][v] > 0 and
sptSet[v] == False and
dist[v] > dist[u] + self.graph[u][v]):
dist[v] = dist[u] + self.graph[u][v]
distances.append(dist)
self.printSolution(dist)
# Driver program
NoOfHospital = dista.shape[0]
SrcHospital = sourcee
g = Graph(NoOfHospital)
mat = [[0 for _ in range(0, NoOfHospital)] for _ in range(0, NoOfHospital)]
x=2
for i in range (0, NoOfHospital):
for j in range (0, NoOfHospital):
mat[i][j]=dista.iloc[i,j+x]
g.graph = mat
g.dijkstra(SrcHospital)
print(distances)
# Merges two subarrays of arr[].
# First subarray is arr[l..m]
# Second subarray is arr[m+1..r]
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
# create temp arrays
L = [0] * (n1)
R = [0] * (n2)
# Copy data to temp arrays L[] and R[]
for i in range(0, n1):
L[i] = arr[l + i]
for j in range(0, n2):
R[j] = arr[m + 1 + j]
# Merge the temp arrays back into arr[l..r]
i = 0 # Initial index of first subarray
j = 0 # Initial index of second subarray
k = l # Initial index of merged subarray
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Copy the remaining elements of L[], if there
# are any
while i < n1:
arr[k] = L[i]
i += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while j < n2:
arr[k] = R[j]
j += 1
k += 1
# l is for left index and r is right index of the
# sub-array of arr to be sorted
def mergeSort(arr, l, r):
if l < r:
# Same as (l+r)//2, but avoids overflow for
# large l and h
m = l+(r-l)//2
# Sort first and second halves
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
SortedDistance = []
for dis in distances[0]:
SortedDistance.append(dis)
mergeSort(SortedDistance, 0, NoOfHospital-1)
for i in range(1,NoOfHospital):
print(SortedDistance[i], end = " ")
HospitalIndex = []
for sd in SortedDistance:
for i in range(NoOfHospital):
if (sd == distances[0][i]):
HospitalIndex.append(i)
newMat=["None" for _ in range(0, NoOfHospital)]
for i in range(0, NoOfHospital):
newMat[i]=[dista.iloc[HospitalIndex[i],1]]
print('\n')
for i in range(0, NoOfHospital-1):
print(HospitalIndex[i], end = " " )
print("\n")
for i in range (1, NoOfHospital):
print(newMat[i][0])
#for SoDis in SortedDistance[0]:
print("\n")
distsor=[]
fmat=[]
for i in range(0, hosp.shape[0]):
for j in range(1, len(newMat)):
if(newMat[j][0]==hosp.iloc[i, 1]):
if(hosp.iloc[i, organind]>0):
fmat.append(newMat[j][0])
distsor.append(SortedDistance[j])
decorganc.decorganc(organname, i)
hosp.iloc[i, organind]=hosp.iloc[i, organind]-1
print(newMat[j][0])
print(fmat)
print(distsor)
return fmat, distsor