-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
executable file
·295 lines (257 loc) · 8.04 KB
/
Graph.py
File metadata and controls
executable file
·295 lines (257 loc) · 8.04 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
import heapq
import sys
from collections import deque
class Graph(object):
class AdjNode(object):
def __init__(self, src, dst, cst=1):
self.source = src
self.destination = dst
self.cost = cst
self.next = None
class AdjList(object):
def __init__(self):
self.head = None
def __init__(self, cnt):
self.count = cnt
self.array = [None] * cnt
i = 0
while i < cnt:
self.array[i] = self.AdjList()
self.array[i].head = None
i += 1
def AddEdge(self, source, destination, cost=1):
node = self.AdjNode(source, destination, cost)
node.next = self.array[source].head
self.array[source].head = node
def AddBiEdge(self, source, destination, cost=1):
self.AddEdge(source, destination, cost)
self.AddEdge(destination, source, cost)
def Print(self):
ad = self.AdjNode()
i = 0
while i < self.count:
ad = self.array[i].head
if ad != None:
print "Vertex " , i , " is connected to : ",
while ad != None:
print ad.destination,
ad = ad.next
print ""
i += 1
def Dijkstra(gph, source):
previous = [-1] * gph.count
dist = [sys.maxint] * gph.count
dist[source] = 0
previous[source] = -1
pqarray = []
node = Graph.AdjNode(source, source, 0)
heapq.heappush(pqarray, (0, node))
while len(pqarray) != 0:
val = heapq.heappop(pqarray)
node = val[1]
adl = gph.array[node.destination]
adn = adl.head
while adn != None:
alt = adn.cost + dist[adn.source]
if alt < dist[adn.destination]:
dist[adn.destination] = alt
previous[adn.destination] = adn.source
node = Graph.AdjNode(adn.source, adn.destination, alt)
heapq.heappush(pqarray, (alt, node))
adn = adn.next
count = gph.count
i = 0
while i < count:
if dist[i] == sys.maxint:
print "node id" , i , "prev" , previous[i] , "distance : Unreachable"
else:
print "node id" , i , "prev" , previous[i] , "distance :" , dist[i]
i += 1
def Prims(gph):
previous = [-1] * gph.count
dist = [sys.maxint] * gph.count
source = 1
dist[source] = 0
previous[source] = -1
pqarray = []
node = Graph.AdjNode(source, source, 0)
heapq.heappush(pqarray, (0, node))
while len(pqarray) != 0:
val = heapq.heappop(pqarray)
node = val[1]
if dist[node.destination] < node.cost:
continue
dist[node.destination] = node.cost;
previous[node.destination] = node.source;
adl = gph.array[node.destination]
adn = adl.head
while adn != None:
if previous[adn.destination]==-1:
node = Graph.AdjNode(adn.source, adn.destination, adn.cost)
heapq.heappush(pqarray, (adn.cost, node))
adn = adn.next
count = gph.count
i = 0
while i < count:
if dist[i] == sys.maxint:
print "node id" , i , "prev" , previous[i] , "distance : Unreachable"
else:
print "node id" , i , "prev" , previous[i] , "distance :" , dist[i]
i += 1
def TopologicalSortDFS(gph, index, visited, stk):
head = gph.array[index].head
while head != None:
if visited[head.destination] == 0:
visited[head.destination] = 1
TopologicalSortDFS(gph, head.destination, visited, stk)
head = head.next
stk.append(index)
def TopologicalSort(gph):
stk = []
count = gph.count
visited = [0] * count
i = 0
while i < count:
if visited[i] == 0:
visited[i] = 1
TopologicalSortDFS(gph, i, visited, stk)
i += 1
while len(stk) != 0:
print stk.pop(),
def PathExist(gph, source, destination):
count = gph.count
visited = [False] * count
visited[source] = True
DFSRec(gph, source, visited)
return visited[destination]
def DFSRec(gph, index, visited):
head = gph.array[index].head
while head != None:
if visited[head.destination] == False:
visited[head.destination] = True
DFSRec(gph, head.destination, visited)
head = head.next
def isConnected(gph):
count = gph.count
visited = [False] * count
visited[0] = True
DFSRec(gph, 0, visited)
i = 0
while i < count:
if visited[i] == False:
return False
i += 1
return True
def DFSStack(gph):
count = gph.count
visited = [0] * count
stk = []
visited[0] = 1
stk.append(object)
while len(stk) != 0:
curr = stk.pop()
head = gph.array[curr].head
while head != None:
if visited[head.destination] == 0:
visited[head.destination] = 1
append(head.destination)
head = head.next
def DFS(gph):
count = gph.count
visited = [0] * count
i = 0
while i < count:
if visited[i] == 0:
visited[i] = 1
DFSRec(gph, i, visited)
i += 1
def BFSQueue(gph, index, visited):
que = deque([])
visited[index] = 1
que.append(index)
while len(que) != 0:
curr = que.popleft()
head = gph.array[curr].head
while head != None:
if visited[head.destination] == 0:
visited[head.destination] = 1
que.append(head.destination)
head = head.next
def BFS(gph):
count = gph.count
visited = [0] * count
i = 0
while i < count:
if visited[i] == 0:
BFSQueue(gph, i, visited)
i += 1
def ShortestPath(gph, source):
count = gph.count
distance = [-1] * count
path = [-1] * count
que = deque([])
que.append(source)
distance[source] = 0
while len(que) != 0:
curr = que.popleft()
head = gph.array[curr].head
while head != None:
if distance[head.destination] == -1:
distance[head.destination] = distance[curr] + 1
path[head.destination] = curr
que.append(head.destination)
head = head.next
i = 0
while i < count:
print path[i] , "to" , i , "weight" , distance[i]
i += 1
def BellmanFordShortestPath(gph, source):
count = gph.count
distance = [sys.maxint] * count
path = [-1] * count
distance[source] = 0
i = 0
while i < count - 1:
j = 0
while j < count:
head = gph.array[j].head
while head != None:
newDistance = distance[j] + head.cost
if distance[head.destination] > newDistance:
distance[head.destination] = newDistance
path[head.destination] = j
head = head.next
j += 1
i += 1
i = 0
while i < count:
print path[i] , "to" , i , "weight" , distance[i]
i += 1
gph = Graph(9)
gph.AddBiEdge(0, 2, 1)
gph.AddBiEdge(1, 2, 5)
gph.AddBiEdge(1, 3, 7)
gph.AddBiEdge(1, 4, 9)
gph.AddBiEdge(3, 2, 2)
gph.AddBiEdge(3, 5, 4)
gph.AddBiEdge(4, 5, 6)
gph.AddBiEdge(4, 6, 3)
gph.AddBiEdge(5, 7, 1)
gph.AddBiEdge(6, 7, 7)
gph.AddBiEdge(7, 8, 17)
Dijkstra(gph, 1)
# Prims(gph)
"""print PathExist(gph, 1, 5)
print isConnected(gph)
ShortestPath(gph, 1)
BellmanFordShortestPath(gph, 1)
g = Graph(6);
g.AddEdge(5, 2);
g.AddEdge(5, 0);
g.AddEdge(4, 0);
g.AddEdge(4, 1);
g.AddEdge(2, 3);
g.AddEdge(3, 1);
print "Topological Sort::",
Graph.TopologicalSort(g);"""