[알고리즘] DFS
깊이 우선 탐색이란? (DFS, Depth First Search)
- 미로 찾이과 같이, 한 우물만 끝까지 파고 아니면 돌아가는 방식
- 핵심 자료구조 : 스택 (Stack)
구현
1
2
3
4
5
6
7
8
9
10
def dfs(current_node, visited):
# 1. 방문 처리
visited.add(current_node)
print(current_node)
# 2. 인접한 노드들 확인
for neighbor in get_neighbors(current_node):
if neighbor not in visited:
# 3. 재귀 호출 (더 깊이 들어감)
dfs(neighbor, visited)
This post is licensed under CC BY 4.0 by the author.