본문 바로가기

알고리즘

[DFS] 프로그래머스 - 네트워크

DFS 

def solution(n, computers):

    answer = 0
    visited = [False] * n

    def dfs (graph, start, visited):
        visited[start] = True
        for node in range(len(graph[start])):
            if visited[node] == False and node != start and graph[start][node] == 1:
                dfs(computers, node, visited)
        
    for i in range(n):
        if visited[i] == False :
            dfs(computers, i, visited)
            answer+=1
    
    return answer