๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algoritm/Quiz-Solutions

[๋ฐฑ์ค€ 1260] DFS์™€ BFS (Java) - DFS,BFS

by ๐Ÿ’œautumn 2020. 5. 7.

[๋ฌธ์ œ๋งํฌ]

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

 

1260๋ฒˆ: DFS์™€ BFS

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฐ„์„ ์€ ์–‘๋ฐฉํ–ฅ์ด๋‹ค.

www.acmicpc.net

[ํ’€์ด ์†Œ์Šค]

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
96
97
98
99
package algorithm;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class DfsBfs1260 {
    public static int nodeCount;
    public static LinkedList<Integer>[] nearNodeList;
    
    
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        
        nodeCount=Integer.parseInt(st.nextToken());
        int EdgeCount=Integer.parseInt(st.nextToken());
        int startNode=Integer.parseInt(st.nextToken());
        
        // ์ธ์ ‘๋…ธ๋“œ๋ฐฐ์—ด ์„ ์–ธ
        nearNodeList=new LinkedList[nodeCount+1];
        for(int i=0; i<=nodeCount;i++) {
            nearNodeList[i]=new LinkedList<Integer>();
        }
        
        // ์ž…๋ ฅ๋ฐ›์•„ ์ธ์ ‘๋…ธ๋“œ๋ฐฐ์—ด๊ตฌ์„ฑ
        while(EdgeCount-->0) {
            st= new StringTokenizer(br.readLine());
            int node1=Integer.parseInt(st.nextToken());
            int node2=Integer.parseInt(st.nextToken());
            
            // ์ธ์ ‘๋…ธ๋“œ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ •๋ฆฌ
            nearNodeList[node1].add(node2);
            nearNodeList[node2].add(node1);
            Collections.sort(nearNodeList[node1]);
            Collections.sort(nearNodeList[node2]);
        }
        
        // ๋‹ต์„ ์œ„ํ•œ sb์„ ์–ธ
        StringBuilder dfsWay=new StringBuilder();
        StringBuilder bfsWay=new StringBuilder();
        
        // ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” boolean๋ฐฐ์—ด
        boolean[] dfsVisited=new boolean[nodeCount+1];
        boolean[] bfsVisited=new boolean[nodeCount+1];
        
        dfs(startNode, dfsVisited, dfsWay);
        bfs(startNode, bfsVisited, bfsWay);
        
        System.out.println(dfsWay+"\n"+bfsWay);
    }
    
    // DFS : Stack์ด์šฉ or "์žฌ๊ท€ํ•จ์ˆ˜"
    public static void dfs(int startNode, boolean[] visited, StringBuilder way) {
        // ์‹œ์ž‘๋…ธ๋“œ(ํ•ด๋‹น๋…ธ๋“œ)์˜ ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ์ฒดํฌ >> ๋ฐฉ๋ฌธ์‹œ ์ข…๋ฃŒ
        if(visited[startNode]) return;
        
        // ๋ฏธ๋ฐฉ๋ฌธ์‹œ >> ๋ฐฉ๋ฌธ ์ฒดํฌ ํ›„ ์ˆœ์„œ๋กœ ๊ธฐ๋ก
        visited[startNode]=true;
        way.append(startNode+" "); 
        
        // ์ž์‹๋…ธ๋“œ ํƒ์ƒ‰(๋ฐ˜๋ณต) >> ์žฌ๊ท€ํ•จ์ˆ˜
        for(int nextNode:nearNodeList[startNode]) {
            dfs(nextNode, visited, way);
        }
    }
    
    // BFS : Queue์ด์šฉ
    public static void bfs(int startNode, boolean[] visited, StringBuilder way) {
        Queue<Integer> queue=new LinkedList<Integer>();
        
        // ์‹œ์ž‘๋…ธ๋“œ ํ์— ์‚ฝ์ž…
        queue.offer(startNode);
        
        while(!queue.isEmpty()) {
            // ํƒ์ƒ‰ํ•  ๋…ธ๋“œ๋ฅผ pop!
            startNode=queue.poll();
            
            // ํƒ์ƒ‰๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ™•์ธ
            //  => ๋ฐฉ๋ฌธ : ๋„˜์–ด๊ฐ€๊ธฐ
            if(visited[startNode]) continue;
            //  => ๋ฏธ๋ฐฉ๋ฌธ : ๋ฐฉ๋ฌธ ์ฒดํฌ ํ›„ ๋ฐฉ๋ฌธ๊ธฐ๋ก
            visited[startNode]= true;
            way.append(startNode+" ");
            //            : ํ•ด๋‹น ์ž์‹์„ ํ์— ๋ชจ๋‘ ์‚ฝ์ž…
            for (int nextNode:nearNodeList[startNode]) {
                queue.offer(nextNode);
            }
            
        }
        
    }
 
}
 
cs

     

 

[๋Š๋‚€์ ]

    - DFS : ์žฌ๊ท€ํ•จ์ˆ˜ or Stack ์‚ฌ์šฉ

    - BFS : Queue ์‚ฌ์šฉ, ์žฌ๊ท€๋™์ž‘์‚ฌ์šฉX!!

 

๋Œ“๊ธ€