01-10 04:27
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 1260 :: DFS์™€ BFS ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 1260 :: DFS์™€ BFS

miinsun 2022. 3. 28. 22:51

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ ,
๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

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

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„์— DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ, ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” BFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. V๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ๋œ ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

4 5 1
1 2
1 3
1 4
2 4
3 4

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

1 2 4 3
1 2 3 4

 

์˜ˆ์ œ ์ž…๋ ฅ 2)

5 5 3
5 4
5 2
1 2
3 4
3 1

 

์˜ˆ์ œ ์ถœ๋ ฅ 2)

3 1 2 5 4
3 1 4 2 5

โ€‹

์˜ˆ์ œ ์ž…๋ ฅ 3)

1000 1 1000
999 1000

 

์˜ˆ์ œ ์ถœ๋ ฅ 3)

1000 999
1000 999

 

โ€‹

๐Ÿ’ป  Main.java

  • ์ค‘๋ณต ๋ฐฉ๋ฌธ์„ ๋ง‰๊ธฐ ์œ„ํ•ด check[n] ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • DFS๋Š” ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ, BFS๋Š” Queue๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด์ค€๋‹ค.
  • ์ž…๋ ฅ ์ˆœ์ด ์ •๋ ฌ๋˜์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๊ธฐ๋•Œ๋ฌธ์—, (x,y), (y,x) ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•ด์ค€๋‹ค.
/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ - 1260 :: DFS์™€ BFS */
import java.util.*;
import java.io.*;

public class Main {
	public void DFS(int n, int s, boolean[][] arr, boolean[] check) {
		// DFS๋Š” ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ด
		// ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” check[i] true๋กœ ํ‘œ์‹œ
		check[s] = true;
		System.out.print(s + " ");

		for(int i = 1; i <= n; i++) {
			if (!check[i]) {
				if(arr[s][i]) {
					// ์Šคํƒ์— ์Œ“์•„๊ฐ€๋“ฏ์ด ์žฌ๊ท€๋ฅผ ์Œ“์•„์ค€๋‹ค.
					DFS(n, i, arr, check);
				}
			}
		}
	}
	
	public void BFS(int n, int s, boolean[][] arr, boolean[] check) {
		// BFS๋Š” queue๋ฅผ ์ด์šฉํ•ด์„œ ํ’€
		Queue<Integer> q = new LinkedList<>();
		
		// ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ๋…ธ๋“œ v๋ถ€ํ„ฐ
		// ๋ฐฉ๋ฌธํ•  ์ •์ ์„ queue์— ๋‹ด๋Š”๋‹ค.
		for(int i = 1; i <= n; i++) {
			if(arr[s][i]) {
				check[i] = true;
				q.offer(i);
			}
		}
		
		// ๋ฐฉ๋ฌธ์„ ์™„๋ฃŒํ•˜๋ฉด check ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๊ณ 
		// ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ ์ถœ
		check[s] = true;
		System.out.print(s + " ");
		
		while(!q.isEmpty()) {
			// ๋ฐฉ๋ฌธํ•œ ์ •์ ์—์„œ ๊ฐ™์€ ๋ ˆ๋ฒจ๋กœ ๋ป—์–ด๋‚˜๊ฐ„๋‹ค.
			int tmp = q.poll();
			System.out.print(tmp + " ");
			
			for(int i = 1; i <= n; i++) {
				if(!check[i]) {
					if(arr[tmp][i]) {
						q.offer(i);
						check[i] = true;
					}
				}
			}
		}
	}
	
	public static void main(String[] args) throws IOException{
		Main main = new Main();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int v = Integer.parseInt(st.nextToken());

		boolean[][] arr = new boolean [n + 1][n + 1];
		boolean[] checkDFS = new boolean [n + 1];
		boolean[] checkBFS = new boolean [n + 1];

		for(int i = 0; i < m; i++) {
			st = new StringTokenizer(br.readLine());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			
			// 2,5์™€ 5,2๋Š” ๊ฐ™์€ ์ •์ ์ด๊ธฐ ๋•Œ๋ฌธ์—
			// (x,y) (y,x) ์žํ‘œ๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•ด์ค€๋‹ค.
			arr[x][y] = true;
			arr[y][x] = true;
		}
		
		
		// DFS
		main.DFS(n, v, arr, checkDFS);
		System.out.println();
    
		// BFS
		main.BFS(n, v, arr, checkBFS);
		
        br.close();
	}
}
Comments