BFS로 풀이가 불가능하고 DFS로만 해결해야 하는 경우는 다음과 같습니다.
🔹 예제 키워드
"모든 경우를 탐색해서 조건을 만족하는 경우를 찾으시오."
"백트래킹을 사용해 가능한 모든 경로를 탐색하시오."
✔ 예제 문제
N × N체스판에서 N개의 퀸을 서로 공격하지 않게 배치하는 경우의 수를 구하시오. (N-Queen 문제)
📌 🔥 DFS만 가능
def n_queen(n):
def is_safe(row, col, board):
for r in range(row):
if board[r] == col or abs(board[r] - col) == abs(r - row):
return False
return True
def dfs(row, board):
if row == n:
solutions.append(board[:])
return
for col in range(n):
if is_safe(row, col, board):
board[row] = col
dfs(row + 1, board)
solutions = []
dfs(0, [-1] * n)
return len(solutions)
print(n_queen(8)) # 8-Queen 문제 해결
🔹 예제 키워드
"한 번만 방문할 수 있는 경로를 찾으시오."
"모든 가능한 경로를 조사하시오."
✔ 예제 문제
그래프에서 출발점과 도착점을 정하고, 중복 없이 한 번씩만 방문하여 도착하는 경로의 개수를 구하시오.
📌 🔥 DFS만 가능
def count_paths(graph, start, end, visited):
if start == end:
return 1
visited.add(start)
count = 0
for neighbor in graph[start]:
if neighbor not in visited:
count += count_paths(graph, neighbor, end, visited.copy())
return count
graph = {1: [2, 3], 2: [4], 3: [4], 4: []}
print(count_paths(graph, 1, 4, set())) # 가능한 경로 개수 출력
🔹 예제 키워드
"가장 깊이 들어갈 수 있는 경로를 찾으시오."
"최대 깊이를 구하시오."
✔ 예제 문제
주어진 트리에서 가장 깊은 노드까지의 거리(깊이)를 구하시오.
📌 🔥 DFS만 가능
def max_depth(tree, node):
if not tree.get(node):
return 0
return 1 + max(max_depth(tree, child) for child in tree[node])
tree = {1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}
print(max_depth(tree, 1)) # 트리의 최대 깊이
🔹 예제 키워드
"사이클이 존재하는지 확인하시오."
"그래프에서 순환 구조를 찾으시오."
✔ 예제 문제
방향 그래프에서 사이클이 존재하는지 확인하시오.
📌 🔥 DFS만 가능
def has_cycle(graph, node, visited, stack):
visited.add(node)
stack.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
if has_cycle(graph, neighbor, visited, stack):
return True
elif neighbor in stack:
return True # 사이클 발견
stack.remove(node)
return False
graph = {1: [2], 2: [3], 3: [4], 4: [2]} # 사이클 존재
print(has_cycle(graph, 1, set(), set())) # True 출력
✔ 예제 문제
"출발점에서 도착점까지의 모든 가능한 경로를 찾으시오."
📌 🔥 DFS가 더 효율적
✔ 예제 문제
"트리에서 특정 노드의 부모를 찾으시오."
📌 🔥 DFS가 더 효율적
| 유형 | DFS가 필요한 이유 |
|---|---|
| 백트래킹 (모든 경우 탐색) | DFS는 가지치기가 가능해 최적화 가능 |
| 특정 경로를 완전 탐색 | BFS는 불필요한 탐색이 많아 비효율적 |
| 깊이 우선 탐색이 필요한 경우 | BFS는 너비 우선이라 깊이 탐색이 어려움 |
| 사이클 탐색 | DFS는 방문 경로 추적이 가능 |
🚀 결론: "모든 경우를 탐색"하거나 "깊이 기반 문제"라면, BFS가 아니라 DFS를 사용해야 합니다. 😊