-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopoSort.py
More file actions
308 lines (221 loc) · 7.91 KB
/
TopoSort.py
File metadata and controls
308 lines (221 loc) · 7.91 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
296
297
298
299
300
301
302
303
304
305
306
307
308
# File: TopoSort.py
# Description:
# Student Name: Sashi Ayyalasomayajula
# Student UT EID: sa55645
# Partner Name: N/A
# Partner UT EID: N/A
# Course Name: CS 313E
# Unique Number:
# Date Created: 11/27/22
# Date Last Modified: 11/28/22
import sys
class Stack (object):
def __init__ (self):
self.stack = []
# add an item to the top of the stack
def push (self, item):
self.stack.append (item)
# remove an item from the top of the stack
def pop (self):
return self.stack.pop()
# check the item on the top of the stack
def peek (self):
return self.stack[-1]
# check if the stack if empty
def is_empty (self):
return (len (self.stack) == 0)
# return the number of elements in the stack
def size (self):
return (len (self.stack))
class Queue (object):
def __init__ (self):
self.queue = []
# add an item to the end of the queue
def enqueue (self, item):
self.queue.append (item)
# remove an item from the beginning of the queue
def dequeue (self):
return (self.queue.pop(0))
# check if the queue is empty
def is_empty (self):
return (len (self.queue) == 0)
# return the size of the queue
def size (self):
return (len (self.queue))
class Vertex (object):
def __init__ (self, label):
self.label = label
self.visited = False
# determine if a vertex was visited
def was_visited (self):
return self.visited
# determine the label of the vertex
def get_label (self):
return self.label
# string representation of the vertex
def __str__ (self):
return str (self.label)
class Graph (object):
def __init__ (self):
self.Vertices = []
self.adjMat = []
# check if a vertex is already in the graph
def has_vertex (self, label):
nVert = len (self.Vertices)
for i in range (nVert):
if (label == (self.Vertices[i]).get_label()):
return True
return False
# given the label get the index of a vertex
def get_index (self, label):
nVert = len (self.Vertices)
for i in range (nVert):
if (label == (self.Vertices[i]).get_label()):
return i
return -1
# add a Vertex with a given label to the graph
def add_vertex (self, label):
if (self.has_vertex (label)):
return
# add vertex to the list of vertices
self.Vertices.append (Vertex (label))
# add a new column in the adjacency matrix
nVert = len (self.Vertices)
for i in range (nVert - 1):
(self.adjMat[i]).append (0)
# add a new row for the new vertex
new_row = []
for i in range (nVert):
new_row.append (0)
self.adjMat.append (new_row)
# add weighted directed edge to graph
def add_directed_edge (self, start, finish, weight = 1):
self.adjMat[start][finish] = weight
# add weighted undirected edge to graph
def add_undirected_edge (self, start, finish, weight = 1):
self.adjMat[start][finish] = weight
self.adjMat[finish][start] = weight
# return an unvisited vertex adjacent to vertex v (index)
def get_adj_unvisited_vertex (self, v):
nVert = len (self.Vertices)
for i in range (nVert):
if (self.adjMat[v][i] > 0) and (not (self.Vertices[i]).was_visited()):
return i
return -1
# do a depth first search in a graph
def dfs (self, v):
# create the Stack
theStack = Stack ()
# mark the vertex v as visited and push it on the Stack
(self.Vertices[v]).visited = True
print (self.Vertices[v])
theStack.push (v)
# visit all the other vertices according to depth
while (not theStack.is_empty()):
# get an adjacent unvisited vertex
u = self.get_adj_unvisited_vertex (theStack.peek())
if (u == -1):
u = theStack.pop()
else:
(self.Vertices[u]).visited = True
print (self.Vertices[u])
theStack.push (u)
# the stack is empty, let us rest the flags
nVert = len (self.Vertices)
for i in range (nVert):
(self.Vertices[i]).visited = False
def delete_edge (self, fromVertexLabel, toVertexLabel):
from_idx = self.get_index(fromVertexLabel)
to_idx = self.get_index(toVertexLabel)
test3 = self.adjMat.copy()
#could have done self.adjMat or test3. same difference.
if from_idx!=-1 and to_idx!=-1: #checks if vertex exists
if test3[from_idx][to_idx] == test3[to_idx][from_idx]:
test3[from_idx][to_idx] = 0
test3[to_idx][from_idx] = 0
else:
test3[from_idx][to_idx] = 0
return test3
def delete_vertex (self, vertexLabel):
idx = self.get_index(vertexLabel)
duplicate = self.get_vertices()
duplicate.remove(duplicate[idx])
for vertex in duplicate:
print(vertex)
length = len(self.adjMat)
for i in range(length):
value = self.adjMat[i][idx]
self.adjMat[i].pop(idx) #pops the column out for specified vertex
self.adjMat.remove(self.adjMat[idx]) #pops the row out for specified vertex
# do the breadth first search in a graph
def bfs (self, v):
#0. creating Queue
frontierqueue = Queue()
#currentv = self.Vertices[v]
currentv = v
frontierqueue.enqueue(v)
discoveredset = []
(self.Vertices[v]).visited = True
#2. Visit an adjacent unvisited vertex (if there is one) in
#order from the current vertex. Mark it visited and insert
#it into the queue.
while (not frontierqueue.is_empty()):
currentv = frontierqueue.dequeue()
self.Vertices[currentv].visited = True
idx = self.Vertices[currentv].get_label()
lst = self.get_neighbors(idx)
print(idx) #PRINTS the bfs traversal
discoveredset.append(currentv)
for adjVertex in lst:
if adjVertex not in discoveredset:
discoveredset.append(adjVertex)
frontierqueue.enqueue(adjVertex)
# the Queue is empty, let us reset the flags
nVert = len (self.Vertices)
for i in range (nVert):
(self.Vertices[i]).visited = False
return
# determine if a directed graph has a cycle
# this function should return a boolean and not print the result
def has_cycle (self):
return
# return a list of vertices after a topological sort
# this function should not print the list
def toposort (self):
return
def main():
# create the Graph object
cities = Graph()
# read the number of vertices
line = sys.stdin.readline()
line = line.strip()
num_vertices = int (line)
# read the vertices to the list of Vertices
for i in range (num_vertices):
line = sys.stdin.readline()
city = line.strip()
cities.add_vertex (city)
# read the number of edges
line = sys.stdin.readline()
line = line.strip()
num_edges = int (line)
# read each edge and place it in the adjacency matrix
for i in range (num_edges):
line = sys.stdin.readline()
edge = line.strip()
edge = edge.split()
start = int (edge[0])
finish = int (edge[1])
weight = int (edge[2])
cities.add_directed_edge (start, finish, weight)
# read the starting vertex for dfs and bfs
line = sys.stdin.readline()
start_vertex = line.strip()
# get the index of the starting vertex
start_index = cities.get_index (start_vertex)
# do the depth first search
print ("Depth First Search")
cities.dfs (start_index)
print ()
if __name__ == "__main__":
main()