forked from bloominstituteoftechnology/CS-Build-Week-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_utils.py
More file actions
190 lines (166 loc) · 5.34 KB
/
basic_utils.py
File metadata and controls
190 lines (166 loc) · 5.34 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
class Queue():
def __init__(self):
self.queue = []
def __repr__(self):
return f'{self.queue}'
def enqueue(self, value):
self.queue.append(value)
def dequeue(self):
if self.size() > 0:
return self.queue.pop(0)
else:
return None
def size(self):
return len(self.queue)
class Stack():
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
if self.size() > 0:
return self.stack.pop()
else:
return None
def size(self):
return len(self.stack)
class Graph:
"""Represent a graph as a dictionary of vertices mapping labels to edges."""
def __init__(self):
self.vertices = {}
def add_vertex(self, vertex):
"""
Add a vertex to the graph.
"""
if vertex not in self.vertices.keys():
self.vertices[vertex] = set()
else:
pass
def add_edge(self, v1, v2):
"""
Add a directed edge to the graph.
"""
if v1 and v2 in self.vertices.keys():
# self.vertices[v2].add(v1)
self.vertices[v1].add(v2)
def bft(self, starting_vertex):
"""
Print each vertex in breadth-first order
beginning from starting_vertex.
"""
ret_list = []
if starting_vertex is None:
return None
my_q = Queue()
visited = [starting_vertex]
my_q.enqueue(starting_vertex)
while len(my_q.queue) > 0:
point = my_q.queue[0]
joins = self.vertices[point]
for j in joins:
if j not in visited:
my_q.enqueue(j)
visited.append(j)
# print(my_q.dequeue())
ret = my_q.dequeue()
ret_list.append(ret)
return ret_list
def dft(self, starting_vertex, chooser=None):
print(f"\n---Starting DFT---")
"""
Print each vertex in depth-first order
beginning from starting_vertex.
"""
ret_list = []
if starting_vertex is None:
return None
my_s = Stack()
visited = [starting_vertex]
my_s.push(starting_vertex)
while len(my_s.stack) > 0:
point = my_s.stack[-1]
joins = self.vertices[point]
r = my_s.pop() # new code
ret_list.append(r) # new code
# print(r) ##changed to r from pop
if chooser is None:
pass
elif chooser == 'random':
joins = random.sample(joins, len(joins))
elif chooser == 'shortest':
joins = find_longest_clique(point, self, visited)
for j in joins:
if j not in visited:
my_s.push(j)
visited.append(j)
return ret_list
def dft_recursive(self, starting_vertex, visited=[]):
print(f"---Starting DFT RECURSIVE---")
"""
Print each vertex in depth-first order
beginning from starting_vertex.
This should be done using recursion.
"""
print(starting_vertex)
visited.append(starting_vertex)
joins = self.vertices[starting_vertex]
if joins is None:
return None
for j in joins:
if j in visited:
pass
else:
self.dft_recursive(j, visited)
def bfs(self, starting_vertex, destination_vertex):
print(f"\n---Starting BFS---")
"""
Return a list containing the shortest path from
starting_vertex to destination_vertex in
breath-first order.
"""
print('Starting BFS')
q = Queue()
visited = set()
q.enqueue([starting_vertex])
print(f'Starting vertex: {starting_vertex}')
print(f'End Room: {destination_vertex}')
print(f"q.queue[0]: {q.queue[0]}")
while destination_vertex not in q.queue[0]:
# while q.queue[0][-1] != destination_vertex:
# print(q)
current_point = q.queue[0][-1]
# print(f'current point: {current_point}')
joins = self.vertices[current_point].values()
# print(joins)
for j in joins:
# print(f'J: {j}')
if j != '?' and j not in visited:
visited.add(j)
_ = [x for x in q.queue[0]]
_.append(j)
q.enqueue(_)
q.dequeue()
return q.queue[0]
def dfs(self, starting_vertex, destination_vertex):
"""
Return a list containing a path from
starting_vertex to destination_vertex in
depth-first order.
"""
s = Stack()
s.push([starting_vertex])
while destination_vertex not in s.stack[-1]:
current_point = s.stack[-1][-1]
joins = self.vertices[current_point]
if joins is None:
s.pop()
else:
temp_list = []
for j in joins:
_ = [x for x in s.stack[-1]]
_.append(j)
temp_list.append(_)
for tl in temp_list:
s.push(tl)
# s.pop()
return s.stack[-1]