๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๋ฐฑ์ค€/C++] 1260๋ฒˆ : DFS์™€ BFS

by nitronium102 2021. 8. 24.

๋ฌธ์ œ

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

์ž…๋ ฅ

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

์ถœ๋ ฅ

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

 

ํ’€์ด

https://dev-minji.tistory.com/76

 

[๊ฐœ๋…] DFS์™€ BFS

๐Ÿ” ์ •์˜ 01. DFS(Depth First Search) : ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) ๋ฃจํŠธ ๋…ธํŠธ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•. ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค. ์Šคํƒ(

dev-minji.tistory.com

 

// 1260๋ฒˆ DFS์™€ BFS
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 10001

int N, M, V; // ์ •์  ๊ฐœ์ˆ˜, ๊ฐ„์„  ๊ฐœ์ˆ˜, ์‹œ์ž‘ ์ •์ 
vector<int> graph[MAX]; // ์ธ์ ‘ ํ–‰๋ ฌ ๊ทธ๋ž˜ํ”„
bool isVisited[MAX] = {false, }; // ์ •์  ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
queue<int> q;

// DFS : ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์—์„œ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋กœ ์˜ฎ๊ฒจ๊ฐ€๋Š” ์‹
void DFS(int v){
  isVisited[v] = true; // ๋ฐฉ๋ฌธ ํ‘œ์‹œ
  cout << v << " ";

  for (int i=0; i<graph[v].size(); i++){
    int next = graph[v][i];
    // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
    if (isVisited[next] == false)
      DFS(next); // ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ
  }
}

// BFS : ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ํ•œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹
void BFS(int v){
  q.push(v); // ๋ฃจํŠธ ๋…ธ๋“œ ์‚ฝ์ž…
  isVisited[v] = true;

  while(!q.empty()){
    int cur = q.front();
    q.pop();
    cout << cur << " ";

    for (int i=0; i<graph[cur].size(); i++){
      int next = graph[cur][i];
      // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
      if (isVisited[next] == false){
        // ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œ
        q.push(next);
        isVisited[next] = true;
      }
    }
  }
}

int main(){
  queue<int> q; // BFS์šฉ
  cin >> N >> M >> V;

  for (int i=0; i<M; i++){
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  // sort : ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค์Œ์„ ํƒ์ƒ‰ํ•  ๋–„ node์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•จ
  for (int i=0; i<MAX; i++){
    sort(graph[i].begin(), graph[i].end());
  }

  DFS(V);
  cout << "\n";

  fill_n(isVisited, MAX, false); // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ดˆ๊ธฐํ™”
  BFS(V);
}

๋Œ“๊ธ€