-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSAGraph.py
More file actions
249 lines (196 loc) · 6.84 KB
/
DSAGraph.py
File metadata and controls
249 lines (196 loc) · 6.84 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
#
# - Graph
#
from iterlinkedList import *
import numpy as np
class DSAGraph:
'''
This graph class implements adjacency list method
'''
def __init__(self):
self.vertices = DSALinkedList()
def addVertex(self,labels):
for label in labels:
sameLabelFound = False
for vertices in self.vertices:
if vertices._value.getLabel() == label:
sameLabelFound = True
if not sameLabelFound:
print("Creating New Graph Node:",label)
vertex = DSAGraphVertex(label)
node = DSAListNode(vertex)
self.vertices.insertLast(node)
def addEdge(self,label1,label2):
self.addVertex((label1,label2))
for vertices in self.vertices:
if vertices._value.getLabel() == label1:
vertices._value.addEdge(DSAGraphVertex(label2))
if vertices._value.getLabel() == label2: # Bidirectional
vertices._value.addEdge(DSAGraphVertex(label1))
def hasVertex(self,label):
retval = False
for vertices in self.vertices:
if vertices._value.getLabel() == label:
retval = True
return retval
def getVertexCount(self):
retval = 0
for vertices in self.vertices:
retval+=1
return retval
def getEdgeCount(self):
retval = 0
for vertices in self.vertices:
retval += vertices._value.getAdjacentCount()
return int(retval/2)
def isAdjacent(self):
pass
def getAdjacent(self,label):
retval = []
for vertices in self.vertices:
if vertices._value.getLabel() == label:
for eachlinks in vertices._value.getAdjacent():
retVal.append(eachlinks)
return retval
def displayAsList(self):
print()
print("***********************************************************")
print("Printing Graph As Adjacent List")
print()
for vertices in self.vertices:
print(vertices._value)
print("***********************************************************")
def displayAsMatrix(self):
pass
def getAllVertices(self):
setOfVertices = set()
for vertices in self.vertices:
setOfVertices = setOfVertices | {vertices._value}
return setOfVertices
def depthFirstSearch(self):
newt = self.getAllVertices()
S = DSASTACKLinked(self.getVertexCount()+1) # Stack of traversed Vertices
T = set() # Traversal Edges
V = newt.pop() # Current Vertex
print("Starting from Node:",V.label)
V.setVisited() # Mark Vertices as old
S.push(V) # Push Vertices onto Stack
while not S.isEmpty():
for w in newt:
for links in V.links:
if w.label == links.label and not w.getVisited():
newEdge = {(V,w)}
w.setVisited()
T = T | newEdge
S.push(w)
V = S.pop()
return T
def breadthFirstSearch(self):
newt = self.getAllVertices()
Q = DSAQueueLinked(self.getVertexCount()+1) # Queue of Traversed Vertices
T = set()
V = newt.pop()
print("Starting from Node:",V.label)
V.setVisited()
Q.queue(V)
while not Q.isEmpty:
V = Q.dequeue()
for w in newt:
for links in V.links:
if w.label == links.label and not w.getVisited():
newEdge = {(V,w)}
w.setVisited()
T = T | newEdge
Q.queue(w)
return T
class DSAGraphVertex:
def __init__(self,inLabel):
self.label = inLabel
self.new = True
#self.value = inValue
self.links = []
self.visited = False
def getLabel(self):
return self.label
# def getValue(self):
# return self.value
def getAdjacent(self):
return self.links
def getAdjacentCount(self):
i = 0
for link in self.links:
i+=1
return i
def addEdge(self,vertex):
linkExists = False
for vertices in self.links:
if vertex.label == vertices.label:
linkExists = True
# print("Link Already Exists")
if not linkExists:
self.links.append(vertex)
def setVisited(self):
self.visited = True
def clearVisited(self):
self.visited = False
def getVisited(self):
return self.visited
def __str__(self):
retstring = "Graph Node: " + str(self.label) + "\t" + "Adjacent Nodes "
for adjacents in self.getAdjacent():
if adjacents:
retstring += str(adjacents.label) + " "
return retstring
#testVertex = DSAGraphVertex("A")
#print(testVertex)
#testVertex.addEdge(DSAGraphVertex("B"))
#testVertex.addEdge(DSAGraphVertex("C"))
#testVertex.addEdge(DSAGraphVertex("D"))
#print(testVertex.getAdjacentCount())
#print(testVertex)
#myGraph.addEdge("A","B")
#myGraph.displayAsList()
#print(myGraph.getEdgeCount())
# myGraph.addEdge("B","C")
# myGraph.displayAsList()
# print(myGraph.getEdgeCount())
# myGraph.addEdge("A","C")
# myGraph.displayAsList()
# print(myGraph.getEdgeCount())
# myGraph.addEdge("C","A")
# myGraph.displayAsList()
# print(myGraph.getEdgeCount())
# filename1 = "prac6_1.al"
# filename2 = "prac6_2.al"
# def graphFromfile(filename):
# Edge_Vertex1 = []
# Edge_Vertex1 = []
# graphFromFile1 = DSAGraph()
# def processline(line):
# tokens = line.split(" ")
# try:
# inEdge_Vertex1 = tokens[0].strip()
# inEdge_Vertex2 = tokens[1].strip()
# except TypeError:
# raise TypeError("File has invalid Format")
# return inEdge_Vertex1,inEdge_Vertex2
# try:
# with open(filename,"r") as f:
# for line in f.readlines():
# inEdge_Vertex1,inEdge_Vertex2 = processline(line)
# graphFromFile1.addEdge(inEdge_Vertex1,inEdge_Vertex2)
# print("File Read Successful")
# except IOError as e:
# print("Error in File Processing:" + str(e))
# return graphFromFile1
# graph1 = graphFromfile(filename1)
# graph1.displayAsList()
# graphFromfile(filename2).displayAsList()
# TraversalSet = graph1.depthFirstSearch()
# #print(TraversalSet)
# for paths in TraversalSet:
# vertex1,vertex2 = paths
# print("(",vertex1.label,",",vertex2.label,")",end=",")
# print()
# # for vertices in graph1.getAllVertices():
# # print(vertices)