dfs

def dfs(graph, v, visited):

    # ํ˜„์žฌ ๋…ธ๋“œ์ˆ˜ํ–‰, ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    print(v, end=' ')
    visited[v] = True

    # ์ธ์ ‘๋…ธ๋“œ ํƒ์ƒ‰
    for i in graph[v]:
        # ๋ฐฉ๋ฌธ ์•ˆ๋˜์–ด์žˆ์œผ๋ฉด
        if not visited[i]:
            # ์žฌ๊ท€
            dfs(graph,i,visited)

graph = [
    [],
    [2,3,8],
    [1,7],
    [1,4,5],
    [3,5],
    [3,4],
    [7],
    [2,6,8],
    [1,7]
]

visited = [False]*9

# 1 ์€ ํ˜„์žฌ๋…ธ๋“œ
dfs(graph,1,visited)

Last updated