DFS/BFS
๐ ๊ฐ์
DFS (Depth-First Search, ๊น์ด ์ฐ์ ํ์)
- ํน์ง : ํ ๊ฒฝ๋ก๋ฅผ ๋๊น์ง ํ๊ณ ๋ ํ, ๋ ์ด์ ๊ฐ ๊ณณ์ด ์์ผ๋ฉด ๋ค๋ก ๋์์ ๋ค๋ฅธ ๊ฒฝ๋ก ํ์
- ๊ตฌํ : ์ฌ๊ท ํจ์ or ์คํ(Stack)
- ์ฅ์ : ๊ตฌํ ๊ฐ๋จ, ๊ฒฝ๋ก ํ์(๋ฐฑํธ๋ํน)์ ์ ๋ฆฌ.
- ๋จ์ : ๊ฒฝ๋ก๊ฐ ๊น์ผ๋ฉด ์คํ ์ค๋ฒํ๋ก์ฐ ๊ฐ๋ฅ
BFS (Breadth-First Search, ๋์ด ์ฐ์ ํ์)
- ํน์ง : ์์์ ์์ ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ์ฐจ๋ก๋๋ก ํ์
- ๊ตฌํ : ํ(Queue)
- ์ฅ์ : ์ต๋จ๊ฑฐ๋ฆฌ ๋ฌธ์ ์ ์์ฃผ ์ฌ์ฉ
- ๋จ์ : ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ โ
โ DFS/BFS ๊ณตํต ํจํด
-
๊ทธ๋ํ ํํ
- ์ธ์ ๋ฆฌ์คํธ (
List<List<Integer>>) - ์ธ์ ํ๋ ฌ (
int[][]) โ ๋ณดํต N์ด ์์ ๋ - 2D ๋ฐฐ์ด (๊ฒฉ์ ๋ฌธ์ )
- ์ธ์ ๋ฆฌ์คํธ (
-
๋ฐฉ๋ฌธ ์ฒดํฌ
boolean[] visited๋๋boolean[][] visited- ์ค๋ณต ๋ฐฉ๋ฌธ ๋ฐฉ์ง
-
ํ์ ์์์
- ์ฐ๊ฒฐ ์์ ๋ฌธ์ โ ๋ชจ๋ ์ ์ ์ํํ๋ฉฐ ๋ฐฉ๋ฌธ ์ ๋ ๋ ธ๋์์ ์์
- ํน์ ์กฐ๊ฑด โ ๋ฌธ์ ์ ๋ฐ๋ผ ์์์ /์ฌ๋ฌ ๊ฐ์ ์์์ ๊ฐ๋ฅ
-
BFS์์์ ๊ฑฐ๋ฆฌ/๋จ๊ณ ๊ธฐ๋ก
dist[]๋ฐฐ์ด ๋๋int[][] dist- ์์์ ๊ฑฐ๋ฆฌ = 0, ์ดํ
dist[next] = dist[cur] + 1โ ์ต๋จ๊ฑฐ๋ฆฌ
๐ค ํต์ฌ ์ ๊ทผ ๋ฐฉ๋ฒ
(A) DFS (์ฌ๊ท, ๊ทธ๋ํ)
class DFS {
static List<List<Integer>> graph = new ArrayList<>();
static boolean[] visited;
public static void dfs(int v) {
visited[v] = true;
System.out.print(v + " "); // ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for(int next : graph.get(v)) {
if(!visited[next]) {
dfs(next);
}
}
}
public static void main(String[] args) {
int n = 5;
visited = new boolean[n+1];
for (int i = 0; i <= n; i++) graph.add(new ArrayList<>());
// ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ ์์
graph.get(1).add(2);
graph.get(2).add(1);
graph.get(1).add(3);
graph.get(3).add(1);
graph.get(2).add(4);
graph.get(4).add(2);
graph.get(3).add(5);
graph.get(5).add(3);
dfs(1); // ์์์ 1
}
}(B) BFS (ํ, ๊ทธ๋ํ)
class BFS {
static List<List<Integer>> graph = new ArrayList<>();
static boolean[] visited;
public static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
visited[start] = true;
q.offer(start);
while (!q.isEmpty()) {
int cur = q.poll();
System.out.print(cur + " ");
for (int next : graph.get(cur)) {
if (!visited[next]) {
visited[next] = true;
q.offer(next);
}
}
}
}
public static void main(String[] args) {
int n = 5;
visited = new boolean[n+1];
for (int i = 0; i <= n; i++) graph.add(new ArrayList<>());
graph.get(1).add(2);
graph.get(1).add(3);
graph.get(2).add(4);
graph.get(3).add(5);
bfs(1);
}
}โ 2์ฐจ์ ๋ฐฐ์ด ํ์ ์์
DFS (์ฌ๊ท)
int n, m;
int[][] map;
boolean[][] visited;
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
void dfs(int x, int y) {
visited[x][y] = true;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx>=0 && ny>=0 && nx<n && ny<m && !visited[nx][ny] && map[nx][ny]==1) {
dfs(nx, ny);
}
}
}BFS(ํ, ์ต๋จ๊ฑฐ๋ฆฌ)
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
void bfs(int sx, int sy) {
Queue<Point> q = new LinkedList<>();
int[][] dist = new int[n][m];
visited[sx][sy] = true;
q.offer(new Point(sx, sy));
dist[sx][sy] = 0;
while(!q.isEmpty()) {
Point cur = q.poll();
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx>=0 && ny>=0 && nx<n && ny<m && !visited[nx][ny] && map[nx][ny]==1) {
visited[nx][ny] = true;
dist[nx][ny] = dist[cur.x][cur.y] + 1;
q.offer(new Point(nx, ny));
}
}
}
}โ ๋ํ ์์ ๋ฌธ์
-
DFS
- ์ฐ๊ฒฐ ์์ ์ธ๊ธฐ (์ฌ์ ๊ฐ์, ์์ญ ๊ฐ์)
- ๋ฐฑํธ๋ํน (N-Queen, ์์ด/์กฐํฉ ์์ฑ)
-
BFS
- ์ต๋จ ๊ฑฐ๋ฆฌ (๋ฏธ๋ก ํ์, ์จ๋ฐ๊ผญ์ง)
- ๋ค์ค ์์์ BFS (ํ ๋งํ , ๋ถ ํผ์ง๊ธฐ)
-
ํผํฉ
- DFS๋ก ์์ญ ํ์ + BFS๋ก ๊ฑฐ๋ฆฌ ๊ณ์ฐ (์์ ์์ญ, ์ ์ผ ์๋ฎฌ๋ ์ด์ ๋ฑ)