-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectedComponentsCount.py
More file actions
82 lines (68 loc) · 1.55 KB
/
Copy pathconnectedComponentsCount.py
File metadata and controls
82 lines (68 loc) · 1.55 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
'''
Write a function, connected_components_count, that takes in the adjacency list of an undirected graph.
The function should return the number of connected components within the graph.
'''
def explore(graph, current, visited):
if current in visited: return False
visited.add(current)
for neighbor in graph[current]:
explore(graph, neighbor, visited)
return True
def connected_components_count(graph):
'''
Time: O(edges)
Space: O(nodes)
'''
count = 0
visited = set()
for node in graph:
if explore(graph, node, visited):
count += 1
return count
def main():
assert connected_components_count({
0: [8, 1, 5],
1: [0],
5: [0, 8],
8: [0, 5],
2: [3, 4],
3: [2, 4],
4: [3, 2]
}) == 2
print("Test case 1 - passed")
assert connected_components_count({
1: [2],
2: [1,8],
6: [7],
9: [8],
7: [6, 8],
8: [9, 7, 2]
}) == 1
print("Test case 2 - passed")
assert connected_components_count({
3: [],
4: [6],
6: [4, 5, 7, 8],
8: [6],
7: [6],
5: [6],
1: [2],
2: [1]
}) == 3
print("Test case 3 - passed")
assert connected_components_count({}) == 0
print("Test case 4 - passed")
assert connected_components_count({
0: [4,7],
1: [],
2: [],
3: [6],
4: [0],
6: [3],
7: [0],
8: []
}) == 5
print("Test case 5 - passed")
print ("All test cases passed!!")
if __name__ == '__main__':
main()