-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
266 lines (230 loc) · 9.86 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
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
from Vertex import Vertex
from Edge import Edge
class Graph():
def __init__(self):
self.__vertecies = {}
@property
def vertecies(self):
return self.__vertecies
def addEdge(self, from_v = None, to_v = None, weight = 1):
if from_v and to_v:
if from_v not in self.__vertecies:
self.__vertecies[from_v] = Vertex(name = from_v)
if to_v not in self.__vertecies:
self.__vertecies[to_v] = Vertex(name = to_v)
edge = Edge(weight, self.__vertecies[to_v])
self.__vertecies[from_v].add_adjecent_edge(edge)
else:
raise Exception("Illegal edge definition")
def __str__(self):
retval = ''
for v in self.vertecies:
vertex = self.vertecies[v]
retval += v + '(' + str(vertex.distance) + ')'
for e in vertex.adjecent:
retval += ' --> [' + e.vertex.name + ': ' + str(e.weight) + ']: ' + str(e.vertex.distance)
retval += '\n'
return retval
def readFile(self, filename = None):
import pandas as pd
from collections import namedtuple
columnnames = ['vertex_from', 'vertex_to', 'weight']
df = pd.read_csv(filename, error_bad_lines=False,
encoding='latin-1', warn_bad_lines=False,
names=columnnames, header = None, sep = ';')
edge = namedtuple('edge', ['vertex_from','vertex_to','weight'])
def maketuple(vertex_from ='', vertex_to ='', weight = 0):
return edge(vertex_from = vertex_from,
vertex_to = vertex_to,
weight = weight)
# Convert pandas representation into namedtuples
edges = [maketuple(vertex_from, vertex_to, weight)
for vertex_from, vertex_to, weight
in zip(df['vertex_from'], df['vertex_to'], df['weight'])]
for e in edges:
self.addEdge(e.vertex_from, e.vertex_to, e.weight)
def unweightedPathDistance(self, startVertexName):
self.resetGraph()
# Give an exception if startnode does not exist
if startVertexName not in self.vertecies:
raise KeyError("Startnode not present in graph")
# ... and initialize
for v in self.vertecies:
vertex = self.vertecies[v]
vertex.distance = None
# Create a FIFO-queue and define enqueue / dequeue
from queue import SimpleQueue
queue = SimpleQueue()
def enqueue(data):
queue.put(data)
def dequeue():
return queue.get()
self.vertecies[startVertexName].distance = distance = 0
enqueue(self.vertecies[startVertexName])
previous_node = None
while not queue.empty():
eyeball = dequeue()
# print("Visiting .... " + eyeball.name)
if eyeball.distance is None:
eyeball.distance = distance
eyeball.previous = previous_node
for edge in eyeball.adjecent:
if edge.vertex.distance is None:
edge.vertex.distance = eyeball.distance + 1
edge.vertex.previous = eyeball
enqueue(edge.vertex)
previous_node = eyeball
def getPath(self, fromVertex, toVertex):
# To verify if a path exists, the path has to be updated ...
#self.unweightedPathDistance(fromVertex)
# Need to verify that the destination vertex exists within the graph
if toVertex not in self.vertecies:
raise KeyError("Destination node not present in graph")
from queue import LifoQueue
stack = LifoQueue()
def push(data):
stack.put(data)
def pop():
return stack.get()
vertex = self.vertecies[toVertex]
push(vertex)
while True:
if vertex.previous is not None:
push(vertex.previous)
vertex = vertex.previous
else:
break
retval = []
while not stack.empty():
retval.append(pop())
return retval
def getPathAsString(self, fromVertex, toVertex):
vertecies = self.getPath(fromVertex, toVertex)
retval = ''
for i in range(0,len(vertecies)):
retval += vertecies[i].name + ' '
if i < len(vertecies) - 1:
retval += '--> '
return retval
def resetGraph(self):
for v in self.vertecies:
vertex = self.vertecies[v]
vertex.previous = None
vertex.distance = None
vertex.visited = False
vertex.indegree = 0
vertex.known = False
def Dijkstra(self, startVertexName):
# Check to see that startvertex is in Graph
if startVertexName not in self.vertecies:
raise KeyError("Start node not present in graph")
# Reset visited and previous pointer before running algorithm
self.resetGraph()
vertex = self.vertecies[startVertexName]
vertex.distance = distance = weight = 0
previous_node = None
#
# Create priority queue, priority = current weight on edge ...
# No duplicate edges in queue allowed
#
edge = Edge(0, vertex)
from queue import PriorityQueue
priqueue = PriorityQueue()
def enqueue(data):
priqueue.put(data)
def dequeue():
return priqueue.get()
enqueue(edge)
while not priqueue.empty():
# Get the element with lowest priority (i.e. weight on edge)
edge = dequeue()
eyeball = edge.vertex
# If not visited previously, we need to define the distance
if not eyeball.known:
eyeball.distance = distance
eyeball.previous = previous_node
eyeball.known = True
# If the vertex pointed to by the edge has an adjecency list, we need to iterate on it
for adjecentedge in eyeball.adjecent:
if not adjecentedge.vertex.known:
adjecentedge.vertex.distance = eyeball.distance + adjecentedge.weight
adjecentedge.vertex.previous = eyeball
adjecentedge.vertex.known = True
enqueue(adjecentedge)
else:
if adjecentedge.vertex.distance > eyeball.distance + adjecentedge.weight:
adjecentedge.vertex.distance = eyeball.distance + adjecentedge.weight
adjecentedge.vertex.previous = eyeball
enqueue(adjecentedge)
def topologicalSort(self):
self.resetGraph()
# Inspecting all vertecies for verticies that have no incoming edges ...
for v in self.vertecies:
vertex = self.vertecies[v]
for edge in vertex.adjecent:
self.vertecies[edge.vertex.name].indegree += 1
# Create a FIFO-queue and define enqueue / dequeue
from queue import SimpleQueue
queue = SimpleQueue()
def enqueue(data):
queue.put(data)
def dequeue():
return queue.get()
# Putting vertecies with indegree == 0 onto queue
for v in self.vertecies:
if self.vertecies[v].indegree == 0:
enqueue(self.vertecies[v])
# Logically remove incoming edges ...
# and preserve sequence ...
topologicalSequence = []
while True:
if queue.empty():
break
vertex = dequeue()
topologicalSequence.append(vertex)
for edge in vertex.adjecent:
edge.vertex.indegree -= 1
if edge.vertex.indegree == 0:
enqueue(edge.vertex)
if len(self.vertecies) != len(topologicalSequence):
raise Exception("Graph is loopy ...")
else:
return topologicalSequence
def DAGShortWeightedPath(self):
self.resetGraph()
# Inspecting all vertecies for verticies that have no incoming edges ...
for v in self.vertecies:
vertex = self.vertecies[v]
for edge in vertex.adjecent:
self.vertecies[edge.vertex.name].indegree += 1
# Create a FIFO-queue and define enqueue / dequeue
from queue import SimpleQueue
queue = SimpleQueue()
def enqueue(data):
queue.put(data)
def dequeue():
return queue.get()
# Putting vertecies with indegree == 0 onto queue
for v in self.vertecies:
if self.vertecies[v].indegree == 0:
enqueue(self.vertecies[v])
self.vertecies[v].distance = 0
# Logically remove incoming edges ...
# and update weight
while not queue.empty():
eyeball = dequeue()
eyeball.known = True
# If an adjecency list is present, we need to iterate on it
for edge in eyeball.adjecent:
edge.vertex.indegree -= 1
# Updating info based on edges
if not edge.vertex.known:
edge.vertex.distance = eyeball.distance + edge.weight
edge.vertex.previous = eyeball
edge.vertex.known = True
else:
if edge.vertex.distance > eyeball.distance + edge.weight:
edge.vertex.distance = eyeball.distance + edge.weight
edge.vertex.previous = eyeball
if edge.vertex.indegree == 0:
enqueue(edge.vertex)