-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphcomponents.py
More file actions
68 lines (54 loc) · 2.16 KB
/
graphcomponents.py
File metadata and controls
68 lines (54 loc) · 2.16 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
import unittest
def findComponents(V, E):
def dfs(vertex, graph, visited1: set):
visited.add(vertex)
for neighbour in graph[vertex]:
if neighbour not in visited1:
dfs(neighbour, graph, visited1)
# process the data from list to dict
graph1 = {v: set() for v in V}
for (edge1, edge2) in E:
graph1[edge1].add(edge2)
graph1[edge2].add(edge1)
components = []
remaining_vertices = set(V)
while remaining_vertices:
visited = set()
dfs(next(iter(remaining_vertices)), graph1, visited)
components.append(sorted(list(visited)))
remaining_vertices.difference_update(visited)
return components
class TestGraphComponents(unittest.TestCase):
def test_testcase1(self):
V = [1, 2, 3, 4, 5, 6, 7, 8]
E = [(1, 2), (2, 3), (1, 3), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8)]
result = findComponents(V, E)
expected = [[1, 2, 3], [4, 5, 6, 7, 8]]
self.assertEqual(result, expected)
def test_testcase2(self):
V = [1, 2, 3, 4, 5]
E = [(1, 2), (1, 3), (1, 4), (1, 5)]
result = findComponents(V, E)
expected = [[1, 2, 3, 4, 5]]
self.assertEqual(result, expected)
def test_testcase3(self):
V = [1, 2, 3, 4, 5, 6, 7, 8, 9]
E = [(1, 2), (1, 3), (1, 8), (3, 7), (4, 5), (4, 6), (4, 9), (5, 6), (5, 9), (7, 8)]
result = findComponents(V, E)
expected = [[1, 2, 3, 7, 8], [4, 5, 6, 9]]
self.assertEqual(result, expected)
def test_testcase4(self):
V = [1, 2, 3, 4, 5]
E = []
result = findComponents(V, E)
expected = [[1], [2], [3], [4], [5]]
self.assertEqual(result, expected)
def test_testcase5(self):
V = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
E = [(1, 2), (2, 3), (4, 5), (7, 8), (7, 9), (9, 10), (9, 11), (9, 12)]
result = findComponents(V, E)
expected = [[1, 2, 3], [4, 5], [6], [7, 8, 9, 10, 11, 12]]
self.assertEqual(result, expected)
if __name__ == '__main__':
print("Testing examples")
unittest.main()