[Algorithm]DFS์™€ BFS

๐Ÿฆฅ ๋ณธ๋ฌธ

https://www.acmicpc.net/problem/1260 Image

์Šคํƒ์œผ๋กœ๋กœ ํ‘ผ DFS


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package backjoon_1260;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main_withStack {
    static StringBuilder sb = new StringBuilder();
    static boolean[] check; // ๋ฐฉ๋ฌธ ์ฒดํฌ
    static ArrayList<Integer>[] adjList; // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ
    static int node, line, start; // ๋…ธ๋“œ ์ˆ˜, ๊ฐ„์„  ์ˆ˜, ์‹œ์ž‘ ๋…ธ๋“œ

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        node = Integer.parseInt(st.nextToken());
        line = Integer.parseInt(st.nextToken());
        start = Integer.parseInt(st.nextToken());

        // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™”
        adjList = new ArrayList[node + 1];
        for (int i = 1; i <= node; i++) {
            adjList[i] = new ArrayList<>();
        }

        // ๊ทธ๋ž˜ํ”„ ์ž…๋ ฅ
        for (int i = 0; i < line; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            adjList[a].add(b);
            adjList[b].add(a);
        }

        // ๋ฐฉ๋ฌธ ์ˆœ์„œ๋ฅผ ์œ„ํ•ด ์ •๋ ฌ
        for (int i = 1; i <= node; i++) {
            Collections.sort(adjList[i]);
        }

        // DFS (์Šคํƒ ์ด์šฉ)
        check = new boolean[node + 1];
        dfsWithStack(start);

        // BFS
        sb.append("\n");
        check = new boolean[node + 1];
        bfs(start);

        // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        System.out.println(sb);
    }

    // ์Šคํƒ์„ ์ด์šฉํ•œ DFS
    private static void dfsWithStack(int start) {
        Stack<Integer> stack = new Stack<>();
        stack.push(start); // ์‹œ์ž‘ ๋…ธ๋“œ ์‚ฝ์ž…

        while (!stack.isEmpty()) {
            int cur = stack.pop(); // ์Šคํƒ์—์„œ ๋…ธ๋“œ ๊บผ๋ƒ„

            if (!check[cur]) { // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
                check[cur] = true;
                sb.append(cur).append(" ");

                // ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์ถ”๊ฐ€ (์—ญ์ˆœ์œผ๋กœ ์ถ”๊ฐ€ํ•ด ์ž‘์€ ๋ฒˆํ˜ธ ์šฐ์„  ํƒ์ƒ‰)
                for (int i = adjList[cur].size() - 1; i >= 0; i--) {
                    int next = adjList[cur].get(i);
                    if (!check[next]) {
                        stack.push(next);
                    }
                }
            }
        }
    }

    // ํ๋ฅผ ์ด์šฉํ•œ BFS
    private static void bfs(int start) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        check[start] = true;

        while (!queue.isEmpty()) {
            int cur = queue.poll();
            sb.append(cur).append(" ");

            for (int next : adjList[cur]) {
                if (!check[next]) {
                    queue.add(next);
                    check[next] = true;
                }
            }
        }
    }
}

์žฌ๊ท€๋กœ ํ‘ผ DFS


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static StringBuilder sb = new StringBuilder();
    static boolean[] check;
    static int[][] arr;

    static int node,line,start;

    static Queue<Integer> q= new LinkedList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        node = Integer.parseInt(st.nextToken());
        line = Integer.parseInt(st.nextToken());
        start = Integer.parseInt(st.nextToken());

        arr = new int[node+1][node+1];
        check = new boolean[node+1];

        for(int i = 0 ; i < line ; i++ ){
            StringTokenizer str = new StringTokenizer(br.readLine());

            int a = Integer.parseInt(str.nextToken());
            int b = Integer.parseInt(str.nextToken());

            arr[a][b] = arr[b][a] = 1;

        }
        dfs(start);
        sb.append("\n");
        check = new boolean[node+1];

        bfs(start);

        System.out.println(sb);



    }



    private static void dfs(int start) {
        check[start] = true;
        sb.append(start + " ");
        for(int i = 1 ; i <= node ; i++){
            if(arr[start][i] == 1 && check[i] == false){
                dfs(i);
            }
        }

    }


    private static void bfs(int start) {
        q.add(start);
        check[start] = true;

        while(!q.isEmpty()){
            start= q.poll();
            sb.append(start + " ");

            for(int i = 1 ; i <= node ; i++){
                if(arr[start][i] == 1 && check[i] == false){
                    q.add(i);
                    check[i] = true;
                }
            }

        }
    }
}

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS,Breadth-First Search)


  • ์‹œ์ž‘ ์ •์  ๋ฐฉ๋ฌธ ํ›„ ๊ฐ€๊นŒ์šด ์ •์  ์šฐ์„  ๋ฐฉ๋ฌธ
  • ๋„“๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๋‘ ๋…ธ๋“œ ์‚ฌ์ด ์ตœ๋‹จ๊ฑฐ๋ฆฌ , ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ตฌํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ
  • ์žฅ์ 
    • ์ตœ์ ํ•ด ์ฐพ์Œ ๋ณด์žฅ
  • ๋‹จ์ 
    • ์ตœ์†Œ ์‹คํ–‰๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ๊ตฌํ˜„


  1. ์ •์  v์— ๋ฐฉ๋ฌธํ•œ๋‹ค.
    2.์ •์  v์— ์ธ์ ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ •์ ์„ ์ฐจ๋ก€๋กœ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ํ์— ๋„ฃ๋Š”๋‹ค.
    3.์ธ์ ‘ํ•ฉ ์ •์  ๋ชจ๋‘ ๋ฐฉ๋ถ„ํ–ˆ๋‹ค๋ฉด ํ์—์„œ dequeueํ•˜์—ฌ ๋ฐ›์€ ๊ฐ’ ์ •์  v๋กœ ์„ค์ •ํ•˜๊ณ  2๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    4.ํ๊ฐ€ ๊ณต๋ฐฑ์ด๋ผ๋ฉด ํƒ์ƒ‰ ์™„๋ฃŒํ•œ ๊ฒƒ์ด๋‹ค.


static boolean[] visit;
static LinkedList<Integer>[] graph;
static int[][] graph;

//์‹œ์ž‘ ์ •์  v
Public static void bfs(int v){
    Queue<Integer> queue = new LinkedList<>();
    queue.add(v);
    visit[v] = true;

    while(!queue.isEmpty()){
        int temp = queue.poll();
        System.out.println(temp);

        for(int nextV: graph[temp]){
            if(!visit[nextV]){
                queue.add(nextV);
                visit[nextV] = true;
            }
        }
    }
}


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


  • ์‹œ์ž‘ ์ •์  ๋ฐฉ๋ฌธ ํ›„ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „ ํ•ด๋‹น ๋ถ„๊ธฐ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰
  • ํŠธ๋ฆฌ์—์„œ ๋ณด๋ฉด ๋…ธ๋“œ ๋ฐฉ๋ฌธ ํ›„ ์ž์‹ ๋…ธ๋“œ ์šฐ์„  ๋ฐฉ๋ฌธ
  • ๊นŠ์ด ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์žฅ์ 
    • ์ตœ์„ ์˜ ๊ฒฝ์šฐ ๋น ๋ฅด๊ฒŒ ํ•ด๋ฅผ ์ฐพ์Œ
  • ๋‹จ์ 
    • ์ฐพ์€ ํ•ด๊ฐ€ ์ตœ์  ํ•ด๊ฐ€ ์•„๋‹ ๊ฐ€๋Šฅ์„ฑ ์žˆ์Œ
    • ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•ด ์ฐพ๋Š”๋ฐ ๊ฐ€์žฅ ์˜ค๋žœ ์‹œ๊ฐ„ ๊ฑธ๋ฆผ

๊ธฐ๋ณธ ๊ตฌํ˜„


1.์ •์  v๋ฅผ ๋ฐฉ๋ถ„ํ•œ๋‹ค.
2.์ •์  v์—์„œ ์ธ์ ‘ํ•œ ์ •์  ์ค‘์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ •์  w๊ฐ€ ์žˆ๋‹ค๋ฉด w๋ฅผ v๋กœ ํ•˜์—ฌ 1๋ถ€ํ„ฐ ๋ฐ˜๋ณตํ•œ๋‹ค(์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ)
3.์ธ์ ‘ํ•œ ์ •์  ๋ชจ๋‘ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ์Šคํƒ์—์„œ ์ •์ ์„ ๊บผ๋‚ด ์œ„๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.



1
2
3
4
5
6
7
8
9
10
11
12
13
//์‹œ์ž‘ ์ •์  v
static boolean[] visit;
//์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ, ํ–‰๋ ฌ ๊ทธ๋ž˜ํ”„ ์ค‘ ์„ ํƒ
static LinkedList<Integer>[] graph;
static int[][]graph;

public static void dfs(int v){
    visit[v] = true;
    System.out.println(v);
    for(int nextV: graph[v]){
        if(!visit[nextV]defs (nextV));
    }
}

์‹œ๊ฐ„ ๋ณต์žก๋„


  • BFS DFS ๋‘˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋™์ผํ•˜๋ฉฐ , ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„์— ๋”ฐ๋ผ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋‹ฌ๋ผ์ง
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ๊ทธ๋ž˜ํ”„:O(n+m)
  • ํ–‰๋ ฌ๋กœ ๊ตฌํ˜„ํ•œ ๊ทธ๋ž˜ํ”„ : O(n^2)

Categories:

Updated:

Leave a comment