-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_graphs.py
214 lines (175 loc) · 7.37 KB
/
test_graphs.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
import unittest
from datastructs import graphs
from random import randrange, sample
NUM_VERTICES = 15
SAMPLESIZE = 100
MAX_INITCONNECTIONS_PER = 7
class TestAdjListGraph(unittest.TestCase):
def setUp(self):
self.g = graphs.AdjListGraph()
self.generated_nums = []
# generate vertices into a temp list, l
c = 0
while c < NUM_VERTICES:
n = randrange(100)
while n in self.generated_nums:
n = randrange(100)
self.generated_nums.append(n)
v = graphs.Vertex(n)
if v not in self.g.vertices:
self.g.vertices.append(v)
c += 1
# Assert there are NUM_VERTICES unique vertices in the graph
self.assertEqual(NUM_VERTICES, len(self.g.vertices))
self.assertEqual(NUM_VERTICES, len(set(self.g.vertices)))
# generate adjacent vertices for each generated vertex
for v in self.g.vertices:
# randomize amount of adjacents
num_adjacent = randrange(2, MAX_INITCONNECTIONS_PER)
adjacents = sample(self.g.vertices, num_adjacent)
if v in adjacents:
adjacents.remove(v)
v.adjacents = adjacents
self.assertEqual(len(adjacents), len(set(adjacents)))
# connect adjacent vertices the other way around
for v in self.g.vertices:
for u in v.adjacents:
for t in self.g.vertices:
if u is t:
if v not in t.adjacents:
t.adjacents.append(v)
# assert that every vertex is connected to at least 1 other
for v in self.g.vertices:
self.assertGreater(len(v.adjacents), 0)
# make sure every adjacency list is unique, does not contain vertices
# that do not exist, and are connected both ways
for v in self.g.vertices:
self.assertTrue(v not in v.adjacents)
self.assertGreater(len(v.adjacents), 0)
self.assertEqual(len(v.adjacents), len(set(v.adjacents)))
self.assertLessEqual(len(v.adjacents), NUM_VERTICES)
for u in v.adjacents:
for t in self.g.vertices:
if t is u:
self.assertTrue(v in t.adjacents)
def tearDown(self):
self.g = None
self.generated_nums = []
def test_isedge(self):
for i in range(SAMPLESIZE):
if not self.g:
self.setUp()
graph = self.g
a = randrange(100)
b = randrange(100)
while a in self.generated_nums or b in self.generated_nums:
a = randrange(100)
b = randrange(100)
s = graphs.Vertex(a)
t = graphs.Vertex(b)
for v in graph.vertices:
# u is randomly chosen from adjacent vertices of vertex
len('pop={}'.format(v.adjacents))
u = sample(v.adjacents, 1)[0]
self.assertTrue(graph.is_edge(v, u))
self.assertTrue(graph.is_edge(u, v))
self.assertFalse(graph.is_edge(v, s))
self.assertFalse(graph.is_edge(a, v))
self.assertFalse(graph.is_edge(s, t))
self.assertFalse(graph.is_edge(t, s))
self.tearDown()
def test_insert_edge(self):
for i in range(SAMPLESIZE):
if not self.g:
self.setUp()
ag = self.g
n = self.generated_nums
# get two existing connected vertices
v = sample(ag.vertices, 1)[0]
u = sample(v.adjacents, 1)[0]
all_adjacents = set(v.adjacents + u.adjacents)
while len(all_adjacents) == len(ag.vertices):
v = sample(ag.vertices, 1)[0]
u = sample(v.adjacents, 1)[0]
all_adjacents = set(v.adjacents + u.adjacents)
# get existing vertex not connected to v or u
w = sample(ag.vertices, 1)[0]
while w in all_adjacents:
w = sample(ag.vertices, 1)[0]
self.assertTrue(ag.is_edge(v, u))
self.assertFalse(ag.is_edge(v, w))
self.assertFalse(ag.is_edge(u, w))
# make new vertices s and t
i, j, k = randrange(100), randrange(100), randrange(100)
while i in n or j in n or k in n:
i, j, k = randrange(100), randrange(100), randrange(100)
s, t, r = graphs.Vertex(i), graphs.Vertex(j), graphs.Vertex(k)
# v,u,w exist in graph, but w not connected to other two
# s,t do not exist in graph
self.assertFalse(ag.insert_edge(v, u))
self.assertFalse(ag.insert_edge(u, v))
self.assertTrue(ag.insert_edge(v, w))
self.assertTrue(ag.insert_edge(u, w))
self.assertTrue(ag.insert_edge(u, s))
self.assertTrue(ag.insert_edge(t, r))
self.assertFalse(ag.insert_edge(v, w))
self.assertFalse(ag.insert_edge(u, w))
self.assertFalse(ag.insert_edge(u, s))
self.assertFalse(ag.insert_edge(t, r))
self.tearDown()
def test_remove_edge(self):
for i in range(SAMPLESIZE):
if not self.g:
self.setUp()
ag = self.g
n = self.generated_nums
# existing vertices
v = sample(ag.vertices, 1)[0]
u = sample(v.adjacents, 1)[0]
all_adjacents = set(v.adjacents + u.adjacents)
while len(all_adjacents) == len(ag.vertices):
v = sample(ag.vertices, 1)[0]
u = sample(v.adjacents, 1)[0]
all_adjacents = set(v.adjacents + u.adjacents)
# get existing vertex not connected to v or u
w = sample(ag.vertices, 1)[0]
while w in all_adjacents:
w = sample(ag.vertices, 1)[0]
self.assertTrue(ag.is_edge(v, u))
self.assertFalse(ag.is_edge(v, w))
self.assertFalse(ag.is_edge(u, w))
# make new vertices s and t
i, j, k = randrange(100), randrange(100), randrange(100)
while i in n or j in n or k in n:
i, j, k = randrange(100), randrange(100), randrange(100)
s, t, r = graphs.Vertex(i), graphs.Vertex(j), graphs.Vertex(k)
# v,u,w exists
# s,t,r dont
self.assertFalse(ag.remove_edge(v, w))
self.assertFalse(ag.remove_edge(v, s))
self.assertFalse(ag.remove_edge(w, s))
self.assertFalse(ag.remove_edge(s, t))
self.assertTrue(ag.remove_edge(v, u))
self.assertFalse(ag.remove_edge(v, u))
self.assertFalse(ag.remove_edge(u, v))
self.tearDown()
def test_dfs(self):
for i in range(SAMPLESIZE):
if not self.g:
self.setUp()
ag = self.g
n = self.generated_nums
graphs.traverse(ag, graphs.DFS)
for v in ag.vertices:
self.assertTrue(v.visited)
self.tearDown()
def test_bfs(self):
for i in range(SAMPLESIZE):
if not self.g:
self.setUp()
graphs.traverse(self.g, graphs.BFS)
for v in self.g.vertices:
self.assertTrue(v.visited)
self.tearDown()
if __name__ == '__main__':
unittest.main()