์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋„คํŠธ์›Œํฌ (๊นŠ์ด/๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(DFS/BFS), C++)

PROGRAMMING/Algorithm

by koharin 2021. 3. 2. 22:06

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void BFS(int start, vector<int> *graph, bool *visited){
    queue<int> q;
    visited[start] = true;
    q.push(start);
    printf("%d visited.\n", start);

    while(!q.empty()){
        int current = q.front();
        q.pop();
        for(int next: graph[current]){
            if(visited[next] == false){
                visited[next] = true;
                q.push(next);
                printf("%d visited.\n", next);
            }
        }
    }

}
int solution(int n, vector<vector<int>> computers){
    vector<int> graph[n];
    bool visited[n];
    int cnt=0;
    fill(visited, visited+n, false);

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(computers[i][j] == 1){
                graph[i].push_back(j);
            }
        }
    }
    
    
    for(int i=0; i<n; i++){
        if(visited[i] == false) { 
            BFS(i, graph, visited); 
            cnt++; printf("%d\n", cnt);
        }
    }
    
    return cnt;
}

int main(){
    int n; // n: # of computers
    scanf("%d", &n);
    vector<vector<int>> computers;

    for(int i=0; i<n; i++){
        vector<int> v;
        for(int j=0; j<n; j++){
            int num; scanf("%d", &num);
            v.push_back(num);
        }
        computers.push_back(v);
    }

    printf("%d\n", solution(n, computers));
}

computers์—์„œ computers[i][j] == 1์ธ ๊ฒฝ์šฐ์—๋งŒ vector์—์„œ graph[i]์— j๋ฅผ ๋„ฃ์–ด์คฌ๋‹ค.

๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฐ๊ฒฐ์„ฑ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

BFS๋กœ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•ด์„œ ์—ฐ๊ฒฐ์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ BFS ํ•จ์ˆ˜ ๋‚ด์—์„œ start ์ •์ ์— ๋Œ€ํ•ด ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด ๋ฐฉ๋ฌธ์„ ํ•˜๊ณ  ํ๊ฐ€ ๋น„๋ฉด ํƒ์ƒ‰์„ ๋๋‚ด๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค. ์ด๊ฒƒ์„ ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ์„ฑ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ  main์œผ๋กœ ๋ฆฌํ„ด ํ›„ ์นด์šดํŒ…์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋œ๋‹ค.

์œ„์™€ ๊ฐ™์ด ํ•œ๋ฒˆ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค ๋ฐฉ๋ฌธ์„ ๋๋‚ด๋ฉด cnt๋ฅผ ์ฆ๊ฐ€ํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ์ œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ดค๋‹ค.

 

void DFS(int start, vector<int> *graph, bool *visited){
    visited[start] = true;

    for(int next: graph[start]){
        if(!visited[next]) DFS(next, graph, visited);
    }
}

DFS์˜ ๊ฒฝ์šฐ, ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๊ตฌํ˜„ํ•ด๋ดค๊ณ , BFS ํ•จ์ˆ˜๋ฅผ DFS ํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

 

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <string>
#include <vector>
#include <queue>
using namespace std;

void BFS(int start, vector<int> *graph, bool *visited){
    queue<int> q;
    visited[start] = true;
    q.push(start);

    while(!q.empty()){
        int current = q.front();
        q.pop();
        for(int next: graph[current]){
            if(visited[next] == false){
                visited[next] = true;
                q.push(next);
            }
        }
    }

}

int solution(int n, vector<vector<int>> computers){
    vector<int> graph[n];
    bool visited[n];
    int cnt=0;
    fill(visited, visited+n, false);

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(computers[i][j] == 1){
                graph[i].push_back(j);
            }
        }
    }
    
    
    for(int i=0; i<n; i++){
        if(visited[i] == false) { 
            BFS(i, graph, visited); 
            cnt++; printf("%d\n", cnt);
        }
    }
    
    return cnt;
}
#include <string>
#include <vector>
#include <queue>
using namespace std;

void DFS(int start, vector<int> *graph, bool *visited){
    visited[start] = true;

    for(int next: graph[start]){
        if(!visited[next]) DFS(next, graph, visited);
    }
}

int solution(int n, vector<vector<int>> computers){
    vector<int> graph[n];
    bool visited[n];
    int cnt=0;
    fill(visited, visited+n, false);

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(computers[i][j] == 1){
                graph[i].push_back(j);
            }
        }
    }
    
    
    for(int i=0; i<n; i++){
        if(visited[i] == false) { 
            //BFS(i, graph, visited); 
            DFS(i, graph, visited);
            cnt++; printf("%d\n", cnt);
        }
    }
    
    return cnt;
}

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


BFS ์ฑ„์  ๊ฒฐ๊ณผ
DFS ์ฑ„์  ๊ฒฐ๊ณผ

728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ