visited = []
def dfs(v, n, computers):
global visited
visited[v] = True
for nei in range(n): # 인접노드 탐색
# unvisited & 인접 또는 노드 자신(1)
if not visited[nei] and computers[v][nei]:
dfs(nei, n, computers)
def solution(n, computers):
# computers = n*n
# 네트워크 개수
global visited
# 덩어리 세기 -> DFS
# 외딴 노드도 count
count = 0
visited = [False] * (n)
for node_idx in range(n):
if not visited[node_idx]:
dfs(node_idx, n, computers)
count += 1 # dfs 끝나면 count 해주기
return count
'언어' 카테고리의 다른 글
티켓으로 여행 경로 만들기: BFS (0) | 2024.02.16 |
---|---|
단어 변환 최소 스텝: BFS (0) | 2024.02.16 |
target으로 계산되는 경우의 수 구하기: DFS (0) | 2024.02.16 |
BFS: 게임 맵 최단거리 구하기 (1) | 2024.02.16 |
비트연산자: XOR 연산 (0) | 2024.02.15 |