DFS/BFS

๐Ÿ” ๊ฐœ์š”

DFS (Depth-First Search, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

  • ํŠน์ง• : ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋๊นŒ์ง€ ํŒŒ๊ณ ๋“  ํ›„, ๋” ์ด์ƒ ๊ฐˆ ๊ณณ์ด ์—†์œผ๋ฉด ๋’ค๋กœ ๋Œ์•„์™€ ๋‹ค๋ฅธ ๊ฒฝ๋กœ ํƒ์ƒ‰
  • ๊ตฌํ˜„ : ์žฌ๊ท€ ํ•จ์ˆ˜ or ์Šคํƒ(Stack)
  • ์žฅ์  : ๊ตฌํ˜„ ๊ฐ„๋‹จ, ๊ฒฝ๋กœ ํƒ์ƒ‰(๋ฐฑํŠธ๋ž˜ํ‚น)์— ์œ ๋ฆฌ.
  • ๋‹จ์  : ๊ฒฝ๋กœ๊ฐ€ ๊นŠ์œผ๋ฉด ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๊ฐ€๋Šฅ

BFS (Breadth-First Search, ๋„“์ด ์šฐ์„  ํƒ์ƒ‰)

  • ํŠน์ง• : ์‹œ์ž‘์ ์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰
  • ๊ตฌํ˜„ : ํ(Queue)
  • ์žฅ์  : ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์— ์ž์ฃผ ์‚ฌ์šฉ
  • ๋‹จ์  : ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ โ†‘

โœ… DFS/BFS ๊ณตํ†ต ํŒจํ„ด

  1. ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„

    • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ (List<List<Integer>>)
    • ์ธ์ ‘ ํ–‰๋ ฌ (int[][]) โ†’ ๋ณดํ†ต N์ด ์ž‘์„ ๋•Œ
    • 2D ๋ฐฐ์—ด (๊ฒฉ์ž ๋ฌธ์ œ)
  2. ๋ฐฉ๋ฌธ ์ฒดํฌ

    • boolean[] visited ๋˜๋Š” boolean[][] visited
    • ์ค‘๋ณต ๋ฐฉ๋ฌธ ๋ฐฉ์ง€
  3. ํƒ์ƒ‰ ์‹œ์ž‘์ 

    • ์—ฐ๊ฒฐ ์š”์†Œ ๋ฌธ์ œ โ†’ ๋ชจ๋“  ์ •์  ์ˆœํšŒํ•˜๋ฉฐ ๋ฐฉ๋ฌธ ์•ˆ ๋œ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘
    • ํŠน์ • ์กฐ๊ฑด โ†’ ๋ฌธ์ œ์— ๋”ฐ๋ผ ์‹œ์ž‘์ /์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹œ์ž‘์  ๊ฐ€๋Šฅ
  4. 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๋กœ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ (์•ˆ์ „ ์˜์—ญ, ์ „์—ผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋“ฑ)