-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestComponent.py
82 lines (68 loc) · 1.51 KB
/
largestComponent.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
'''
Write a function, largest_component, that takes in the adjacency list of an undirected graph.
The function should return the size of the largest connected component in the graph.
'''
def largest_component(graph):
'''
Time: O(edges)
Space: O(nodes)
'''
largest = 0
visited = set()
for node in graph:
largest = max(largest, explore(graph, node, visited))
return largest
def explore(graph, current, visited):
if current in visited: return 0
visited.add(current)
component_size = 1
for neighbor in graph[current]:
component_size += explore(graph, neighbor, visited)
return component_size
def main():
assert largest_component({
0: [8, 1, 5],
1: [0],
5: [0, 8],
8: [0, 5],
2: [3, 4],
3: [2, 4],
4: [3, 2]
}) == 4
print("Test case 1 - passed")
assert largest_component({
1: [2],
2: [1,8],
6: [7],
9: [8],
7: [6, 8],
8: [9, 7, 2]
}) == 6
print("Test case 2 - passed")
assert largest_component({
3: [],
4: [6],
6: [4, 5, 7, 8],
8: [6],
7: [6],
5: [6],
1: [2],
2: [1]
}) == 5
print("Test case 3 - passed")
assert largest_component({}) == 0
print("Test case 4 - passed")
assert largest_component({
0: [4,7],
1: [],
2: [],
3: [6],
4: [0],
6: [3],
7: [0],
8: []
}) == 3
print("Test case 5 - passed")
print ("All test cases passed!!")
if __name__ == '__main__':
main()