알고리즘/프로그래머스

[깊이/너비 우선 탐색(DFS/BFS)] 네트워크

래울 2021. 11. 11. 20:43

https://programmers.co.kr/learn/courses/30/lessons/43162

 

코딩테스트 연습 - 네트워크

네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있

programmers.co.kr

 

def solution(n, computers):
    visited = [0 for i in range(n)]
    answer = 0
    
    def DFS(arg):
        visited[arg] = 1
        for i in range(n):
            if computers[arg][i] and not visited[i]:
                DFS(i)
                
    for c in range(n):
        if visited[c] == 1:
            continue
        answer += 1
        DFS(c)
    return answer