-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfsbfs_network.py
More file actions
53 lines (43 loc) · 1.34 KB
/
dfsbfs_network.py
File metadata and controls
53 lines (43 loc) · 1.34 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
def solution(n, computers):
def dfs(i):
visit[i] = 1
for j in range(len(computers)):
if computers[i][j] and not visit[j]:
dfs(j)
visit = [0]*len(computers)
count = 0
for i in range(n):
if not visit[i]:
count += 1
dfs(i)
return count
print("#test case 1")
print(solution(3, [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])) # 3
print("#test case 2")
print(solution(3, [[1, 1, 0],
[1, 1, 0],
[0, 0, 1]])) # 2
print("#test case 3")
print(solution(3, [[1, 1, 0],
[1, 1, 1],
[0, 1, 1]])) # 1
print("#test case 4")
#0, 1, 2, 3, 4, 5
print(solution(6, [[1, 1, 1, 0, 0, 0], # 0
[1, 1, 1, 1, 0, 0], # 1
[1, 1, 1, 1, 0, 0], # 2
[0, 1, 1, 1, 0, 0], # 3
[0, 0, 0, 0, 1, 0], # 4
[0, 0, 0, 0, 0, 1]])) # 5
# 3
print("#test case 5")
#0, 1, 2, 3, 4, 5
print(solution(6, [[1, 1, 1, 1, 0, 0], # 0
[1, 1, 1, 1, 0, 0], # 1
[1, 1, 1, 1, 0, 0], # 2
[1, 1, 1, 1, 0, 0], # 3
[0, 0, 0, 0, 1, 0], # 4
[0, 0, 0, 0, 0, 1]])) # 5
# 3