[Java] ๋ฐฑ์ค€ 1260. DFS & BFS

์ •์„ยท2024๋…„ 5์›” 13์ผ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต

๋ชฉ๋ก ๋ณด๊ธฐ
36/67
post-thumbnail

๐Ÿง‘๐Ÿปโ€๐Ÿ’ป ๋ฌธ์ œ

๐Ÿ’ก๋ฌธ์ œ ๋ถ„์„ ์š”์•ฝ

์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ DFS ์™€ BFS๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰์„ ํ•œ ์ˆœ์„œ์— ๋Œ€ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.


๐Ÿ’ก์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„

์ž…๋ ฅ๋ฐ›์€ ๊ทธ๋ž˜ํ”„์™€ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ๋ฐฐ์—ด์€ static ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

โ–ถ๏ธŽ dfs ๋กœ์ง ๊ตฌํ˜„

  1. ์‹œ์ž‘ ๋…ธ๋“œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
  2. for(์‹œ์ž‘ ๋…ธ๋“œ์˜ ์ž์‹๋…ธ๋“œ ๋ฐฉ๋ฌธ (๊ฐ’์ด ์กด์žฌํ•˜๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ))
    2-1. ์žฌ๊ท€ ๊ตฌํ˜„
private static void dfs(int start) {
		check[start] = true;
		sb.append(start + " ");
		
		for (int i = 0; i <= node; i++) {
			if (arr[start][i] == 1 && !check[i]) {
				dfs(i);
			}
		}
		
	}

โ–ถ๏ธŽ bfs ๋กœ์ง ๊ตฌํ˜„

  1. ํ์— ๊ฐ’ ์‚ฝ์ž…
  2. ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  3. while (ํ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋™์•ˆ)
    3-0. start = ํ.poll()
    3-1. for (start์˜ ์ž์‹๋…ธ๋“œ ๋ฐฉ๋ฌธ (๊ฐ’์ด ์กด์žฌํ•˜๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ))
    3-2. q.add(์ž์‹๋…ธ๋“œ)
    3-3. ์ž์‹๋…ธ๋“œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
private static void bfs(int start) {
		q.add(start);
		check[start] = true;
		
		while(!q.isEmpty()) {
			start = q.poll();
			sb.append(start + " ");
			
			for (int i = 0; i <=node; i++) {
				if (arr[start][i] == 1 && !check[i]) {
					q.add(i);
					check[i] = true;
				}
			}
		}
	}

๐Ÿ’ก์ฝ”๋“œ

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++) {
			st = new StringTokenizer(br.readLine());
			
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			arr[a][b] = arr[b][a] = 1;
		}
		
		dfs(start);
		sb.append("\n");
		check = new boolean[node+1];
		
		bfs(start);
		
		System.out.println(sb);
	}
	
	
	// DFS ์ •์˜
	private static void dfs(int start) {
		check[start] = true;
		sb.append(start + " ");
		
		for (int i = 0; i <= node; i++) {
			if (arr[start][i] == 1 && !check[i]) {
				dfs(i);
			}
		}
		
	}
	
	// BFS ์ •์˜
	private static void bfs(int start) {
		q.add(start);
		check[start] = true;
		
		while(!q.isEmpty()) {
			start = q.poll();
			sb.append(start + " ");
			
			for (int i = 0; i <=node; i++) {
				if (arr[start][i] == 1 && !check[i]) {
					q.add(i);
					check[i] = true;
				}
			}
		}
	}
		
}

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

  • ๊ทธ๋ž˜ํ”„์˜ ์ธ์ ‘ ํ–‰๋ ฌ ์ƒ์„ฑ: O(N^2) (N์€ ๋…ธ๋“œ์˜ ์ˆ˜)
  • DFS ํ•จ์ˆ˜: O(N^2) (๋ชจ๋“  ๋…ธ๋“œ์™€ ๊ฐ„์„ ์„ ์ˆœํšŒ)
  • BFS ํ•จ์ˆ˜: O(N^2) (๋ชจ๋“  ๋…ธ๋“œ์™€ ๊ฐ„์„ ์„ ์ˆœํšŒ)

๋”ฐ๋ผ์„œ O(N^2) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

๐Ÿ’กย ๋А๋‚€์  or ๊ธฐ์–ตํ• ์ •๋ณด

  • BFS & DFS ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ๋•Œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ํ™œ์šฉํ•œ๋‹ค.
  • BFS ๋Š” ํ๋ฅผ ํ™œ์šฉํ•˜๋ฉฐ, DFS๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€