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