[์ฝํ ] DFS vs BFS
์ธ์ ๋ญ ์ ํํด์ผ ํ ๊น?
![[์ฝํ
] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
์ ํ์ง
DFS or BFS ์ด๋ค๊ฒ์ด๋ ์ฌ์ฉํด๋ ๋ฌด๊ดํ ๊ฒฝ์ฐ
DFS๋ฅผ ์ฌ์ฉํ๋๊ฒ์ด ์๋์ ์ผ๋ก ํธํ๊ฒฝ์ฐ
์์ด & ์กฐํฉ ๊ตฌํ (์ซ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ง ๊ฒฝ์ฐ +, - ๊ฒฐ๊ณผ๊ฐ์ด ํ๊ฒ๊ณผ ์ผ์นํ๋ ๊ฒฝ์ฐ์ ์)
https://school.programmers.co.kr/learn/courses/30/lessons/43165
# ํ๋ก๊ทธ๋๋จธ์ค ํ๊ฒ๋๋ฒ def solution(numbers, target): global count count = 0 n = len(numbers) def dfs(i, total): global count if i == len(numbers): # ๋ชจ๋ ์ซ์๋ฅผ ๋ค ํ์ฉํ๋์ง if target == total: # ํ๊ฒ ์ซ์์ ๋์ผํ์ง count += 1 return # ํ์ฉํ์ง๋ง ํ๊ฒ๊ณผ ๋์ผํ์ง ์๋ ๊ฒฝ์ฐ ๊ทธ๋ฅ ๋ฆฌํด dfs(i+1, total+numbers[i]) dfs(i+1, total-numbers[i]) dfs(0, 0) return count์ฐ๊ฒฐ๋ ๊ทธ๋ํ๋ฅผ ์์ ํ ํ์ํด์ผ ํ ๋ (์ฃผ์ด์ง ๋ชจ๋ ๋นํ๊ธฐ ํฐ์ผ์ ์ฌ์ฉํด์ผํ๋ ๊ฒ์ด ๋ฌธ์ ์ ์กฐ๊ฑด)
BFS๋ฅผ ์ฌ์ฉํ๋ฉด ์ฒซ ๊ฒฝ๋ก์์ ๊ฐ ๊ณณ์ด ์๋ ๊ฒฝ์ฐ ์์ธ ์ผ์ด์ค๋ฅผ ์ฒ๋ฆฌํด์ฃผ๊ธฐ ๋ฒ๊ฑฐ๋กญ๋ค
https://school.programmers.co.kr/learn/courses/30/lessons/43164
from collections import defaultdict def solution(tickets): graph = defaultdict(list) for (start, end) in tickets: graph[start].append(end) for airport in graph: graph[airport].sort(reverse=True) path = [] stack = ["ICN"] while stack: route = stack[-1] if not graph[route]: path.append(stack.pop()) else: next_route = graph[route].pop() stack.append(next_route) return path[::-1]
BFS๋ฅผ ์ฌ์ฉํ๋๊ฒ์ด ์๋์ ์ผ๋ก ํธํ๊ฒฝ์ฐ
์ต๋จ ๊ฒฝ๋ก ํ์ (2์ฐจ์ ๋ฐฐ์ด์์ ๋ฒฝ์ ํผํด ๋ชฉ์ ์ง์ ๊ฐ ์ ์๋ ๊ฑฐ๋ฆฌ์ ์ต๋จ)
https://school.programmers.co.kr/learn/courses/30/lessons/1844
# ํ๋ก๊ทธ๋๋จธ์ค ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ from collections import deque def solution(maps): # ์ํ์ข์ฐ dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y): queue = deque() queue.append((x, y)) while queue: x, y = queue.popleft() for i in range(4): # ์ํ์ข์ฐ ์์ง์ด๊ธฐ nx = x + dx[i] ny = y + dy[i] if nx < 0 or ny < 0 or nx >= n or ny >= m: # ์ขํ ๋ฒ์ ๋ฒ์ด๋ ๊ฒฝ์ฐ continue if maps[nx][ny] == 0: # ๋ฒฝ์ธ ๊ฒฝ์ฐ continue if maps[nx][ny] == 1: # ์ฎ๊ฒจ๊ฐ ์ ์๋ ๊ฒฝ์ฐ queue.append((nx, ny)) maps[nx][ny] = maps[x][y] + 1 min_count = int(1e9) n, m = len(maps), len(maps[0]) bfs(0, 0) answer = maps[n-1][m-1] if maps[n-1][m-1] != 1 else -1 return answerBFS๋ ์์ ์ง์ ์์ ๊ฐ๊น์ด ์ ์ ๋ถํฐ ์ฐจ๋ก๋๋ก ํ์ํด ๋๊ฐ๊ธฐ ๋๋ฌธ์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๋จผ์ ์ฐพ์ ์ ์๋ค. BFS๋ ํ์ ๊ณผ์ ์์ ๋ฐฉ๋ฌธํ ์ ์ ์ ํ์ ์ ์ฅํ์ฌ FIFO ๋ฐฉ์์ผ๋ก ํ์ํ๋ฏ๋ก, ๋ชฉ์ ์ง์ ๋จผ์ ๋์ฐฉํ ๊ฒฝ์ฐ์๋ ๊ทธ ๋์ ๊ฑฐ๋ฆฌ๊ฐ ์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ๋๋ค.
๋ฐ๋ฉด์ DFS๋ ์คํ ๋๋ ์ฌ๊ท๋ฅผ ์ฌ์ฉํ์ฌ ๊น์ด๋ฅผ ์ฐ์ ์ ์ผ๋ก ํ์ํ๋ฏ๋ก, ๊ฒฝ๋ก๋ฅผ ๋ฐ๋ผ ์ต์ข ๋ชฉ์ ์ง๊น์ง ๋๋ฌํ ๋๊น์ง ํ์์ ์งํํ๋ค. ๋ฐ๋ผ์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๋ณด์ฅํ์ง ์์ผ๋ฉฐ, ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ํ์ํ ํ์์ผ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์ ์ ์๋ค. ๋ฐ๋ผ์, ์ต๋จ ๊ฑฐ๋ฆฌ ๋ฌธ์ ์๋ BFS๊ฐ ํจ์ฌ ํจ์จ์ ์ด๋ค.
![[์ฝํ
] ๊ทธ๋ฆฌ๋ ๋ฌธ์ - ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[์ฝํ
] Bfs ํ ๋งํ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[์ฝํ
] Dfs ๋ฌธ์ ์ ํ - ๊ทธ๋ํ ๋ด์์ ๊ตฌ๋ถํ์ฌ ์นด์ดํธ ํ๊ธฐ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[์ฝํ
] ์ฌํ๊ฒฝ๋ก](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)