-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_graph.py
170 lines (136 loc) · 5.29 KB
/
test_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
import numpy as np
from unittest import TestCase
from tree_isomorphism import Graph
class TestGraphInit(TestCase):
def test_empty_matrix__empty_adj_list(self):
matrix = np.array([[]])
graph = Graph(matrix)
self.assertTrue(not graph.adj_list)
def test_1d_matrix__empty_adj_list(self):
matrix = np.array([1, 0])
graph = Graph(matrix)
self.assertTrue(not graph.adj_list)
def test_3d_matrix__empty_adj_list(self):
matrix = np.array([[[0, 1]], [[1, 0], [1, 0]]])
graph = Graph(matrix)
self.assertTrue(not graph.adj_list)
def test_2x3_matrix__empty_adj_list(self):
matrix = np.array([[0, 1, 0],
[1, 0, 0]])
graph = Graph(matrix)
self.assertTrue(not graph.adj_list)
def test_not_sym_matrix__empty_adj_list(self):
matrix = np.array([[0, 1, 0],
[1, 0, 0],
[1, 0, 1]])
graph = Graph(matrix)
self.assertTrue(not graph.adj_list)
def test_valid_1x1_matrix__valid_adj_list(self):
matrix = np.array([[0]])
graph = Graph(matrix)
self.assertEqual(len(graph.adj_list), 1)
def test_valid_3x3_matrix__valid_adj_list(self):
matrix = np.array([[0, 1, 0],
[1, 0, 1],
[0, 1, 0]])
graph = Graph(matrix)
self.assertEqual(set(graph.adj_list[0]), {1})
self.assertEqual(set(graph.adj_list[1]), {0, 2})
self.assertEqual(set(graph.adj_list[2]), {1})
class TestGraph(TestCase):
def setUp(self):
self.graph_tree = Graph(np.array([[]]))
self.graph_unlinked = Graph(np.array([[]]))
self.graph_loop = Graph(np.array([[]]))
self.graph_one_node = Graph(np.array([[]]))
# adjacency matrix of a tree
self.graph_tree.adj_list = [
[1, 2], # 0
[3, 4, 0], # 1
[0, 5], # 2
[1, 6], # 3
[1], # 4
[2], # 5
[3] # 6
]
# adjacency matrix of a graph with loop
self.graph_unlinked.adj_list = [
[1, 2], # 0
[0], # 1
[0, 5], # 2
[4, 6], # 3
[3], # 4
[2], # 5
[3] # 6
]
# adjacency matrix of a graph with loop
self.graph_loop.adj_list = [
[1, 2], # 0
[0, 3, 4], # 1
[0, 3, 5], # 2
[1, 4, 6], # 3
[1, 3], # 4
[2], # 5
[3], # 6
]
# adjacency matrix of a graph with one vertex
self.graph_one_node.adj_list = [[0]]
class TestGraphDFS(TestGraph):
def test_graph_tree_from0__tree_furthest6(self):
is_tree, furthest = self.graph_tree.dfs(0)
self.assertTrue(is_tree)
self.assertEqual(furthest, 6)
def test_graph_tree_from6__tree_furthest5(self):
is_tree, furthest = self.graph_tree.dfs(6)
self.assertTrue(is_tree)
self.assertEqual(furthest, 5)
def test_graph_one_node__not_tree_furthest0(self):
is_tree, furthest = self.graph_one_node.dfs(0)
self.assertFalse(is_tree)
self.assertEqual(furthest, 0)
def test_graph_unlinked_from6__not_tree_furthest4(self):
is_tree, furthest = self.graph_unlinked.dfs(6)
self.assertFalse(is_tree)
self.assertEqual(furthest, 4)
def test_graph_unlinked_from0__not_tree_furthest5(self):
is_tree, furthest = self.graph_unlinked.dfs(0)
self.assertFalse(is_tree)
self.assertEqual(furthest, 5)
def test_graph_loop_from0__not_tree(self):
is_tree, _ = self.graph_loop.dfs(0)
self.assertFalse(is_tree)
def test_graph_loop_from3__not_tree(self):
is_tree, _ = self.graph_loop.dfs(3)
self.assertFalse(is_tree)
class TestGraphFindPath(TestGraph):
def test_graph_tree_0to0__valid_path(self):
path = self.graph_tree.find_path(0, 0)
self.assertEqual(path, [0])
def test_graph_tree_0to6___valid_path(self):
path = self.graph_tree.find_path(0, 6)
self.assertEqual(path, [0, 1, 3, 6])
def test_graph_tree_2to3___valid_path(self):
path = self.graph_tree.find_path(2, 3)
self.assertEqual(path, [2, 0, 1, 3])
def test_graph_tree_0to_invalid__no_path(self):
invalid_node = 5112
path = self.graph_tree.find_path(0, invalid_node)
self.assertFalse(path)
def test_graph_one_node_0to0__valid_path(self):
path = self.graph_one_node.find_path(0, 0)
self.assertEqual(path, [0])
def test_graph_unlinked_0to6__no_path(self):
path = self.graph_unlinked.find_path(0, 6)
self.assertFalse(path)
def test_graph_unlinked_0to5__valid_path(self):
path = self.graph_unlinked.find_path(0, 5)
self.assertEqual(path, [0, 2, 5])
def test_graph_unlinked_3to4__valid_path(self):
path = self.graph_unlinked.find_path(3, 4)
self.assertEqual(path, [3, 4])
def test_graph_loop_0to4__path(self):
path = self.graph_loop.find_path(0, 6)
self.assertTrue(path)
def test_graph_loop_3to4__path(self):
path = self.graph_loop.find_path(3, 4)
self.assertTrue(path)