πŸ’»

[자료ꡬ쑰] λ„ˆλΉ„ μš°μ„  탐색(BFS) λ³Έλ¬Έ

자료ꡬ쑰

[자료ꡬ쑰] λ„ˆλΉ„ μš°μ„  탐색(BFS)

λ˜νš¨λ‹ˆ 2020. 2. 21. 17:06

λ„ˆλΉ„μš°μ„ νƒμƒ‰(BFS= Breadth First Search) λŠ” λ„ˆλΉ„λ₯Ό μš°μ„ μœΌλ‘œ νƒμƒ‰ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μž…λ‹ˆλ‹€. κΈ°λ³Έμ μœΌλ‘œ 큐(Queue)λ₯Ό μ΄μš©ν•˜μ—¬ κ΅¬ν˜„ν•©λ‹ˆλ‹€. 

 

1. 탐색 μ‹œμž‘ λ…Έλ“œλ₯Ό 큐에 μ‚½μž…ν•˜κ³  λ°©λ¬Έ 처리λ₯Ό ν•©λ‹ˆλ‹€ 

2. νμ—μ„œ λ…Έλ“œλ₯Ό κΊΌλ‚΄ ν•΄λ‹Ή λ…Έλ“œμ˜ 인접 λ…Έλ“œ μ€‘μ—μ„œ λ°©λ¬Έν•˜μ§€ μ•Šμ€ λ…Έλ“œλ“€μ„ λͺ¨λ‘ 큐에 μ‚½μž…ν•˜κ³ , λ°©λ¬Έ 처리λ₯Ό ν•©λ‹ˆλ‹€ .

3. 2번의 과정을 더 이상 μˆ˜ν–‰ν•  수 없을 λ•ŒκΉŒμ§€ λ°˜λ³΅ν•©λ‹ˆλ‹€. 

 

큐에 첫번째 λ…Έλ“œ 1을 λ„£κ³ , λ‹€μŒμœΌλ‘œλŠ” λ…Έλ“œ1κ³Ό μ—°κ²°λœ λ…Έλ“œ 2,3,4 λ₯Ό λ„£λŠ”λ‹€. 더이상 λ…Έλ“œ 1κ³Ό μ—°κ²°λœ λ…Έλ“œκ°€ μ—†μœΌλ―€λ‘œ λ…Έλ“œ 2와 μ—°κ²°λœ λ…Έλ“œ 5,6을 λ„£κ³  λ§ˆμ§€λ§‰μœΌλ‘œ λ…Έλ“œ 6κ³Ό μ—°κ²°λœ λ…Έλ“œ 7을 λ„£λŠ”λ‹€.

 

λ„ˆλΉ„ μš°μ„  탐색은 큐(Queue)μžλ£Œκ΅¬μ‘°μ— κΈ°μ΄ˆν•œλ‹€λŠ” μ μ—μ„œ κ΅¬ν˜„μ΄ κ°„λ‹¨ν•©λ‹ˆλ‹€. μ‹€μ œλ‘œ κ΅¬ν˜„ν•¨μ— μžˆμ–΄ 큐 STL을 μ‚¬μš©ν•˜λ©΄ μ’‹μœΌλ©° 탐색을 μˆ˜ν–‰ν•¨μ— μžˆμ–΄μ„œ O(N)μ‹œκ°„μ΄ μ†Œμš”λ©λ‹ˆλ‹€. 일반적인 경우 μ‹€μ œ μˆ˜ν–‰ μ‹œκ°„μ€ DFS보닀 쒋은 νŽΈμž…λ‹ˆλ‹€. 

 

#include <stdio.h>
#include <stdlib.h>
#define INF 999999999
#define MAX_SIZE 1001

typedef struct{
	int index;
    struct Node *next;
} Node;

typedef struct{
	Node *front;
    Node *rear;
    int count;
} Queue;

Node++ a;
int n, m, c[MAX_SIZE]; //λ°°μ—΄ cλŠ” λ°©λ¬Έν•œ 정보

void addFront(Node *root, int index){
	Node *node = (Node*)malloc(sizeof(Node));
    node->index = index;
    node->next = root->next;
    root->next = node;
}

void queuePush(Queue *queue, int index){
	Node *node = (Node*)malloc(sizeof(Node));
    node->index = index;
    node->next = NULL;
    if(queue->count == 0){   //ν˜„μž¬ 큐가 λΉ„μ–΄μžˆμœΌλ©΄
    	queue->front = node; //μ•žμ— μ‚½μž…
    }else{
    	queue->rear->next = node; //뒀에 μ‚½μž…
    }
    queue->rear = node;
    queue->count++;
}

int queuePop(Queue *queue){
	if(queue->count == 0){   //κΊΌλ‚Ό λ…Έλ“œκ°€ 없을 λ•Œ 
    	printf("큐 μ–Έλ”ν”Œλ‘œμš° λ°œμƒ\n");
        return -INF; 
    }
    Node *node = queue->front;
    int index = node->index;
    queue->front = node->next;
    free(node);
    queue->count--;
    return index;
}

void bfs(int start){
	Queue q;
    q.front = q.rear = NULL;
    q.count = 0;
    queuePush(&q, start);
    c[start] = 1;
    while(q.count!=0){  //큐가 빌 λ•ŒκΉŒμ§€ 반볡
    	int x = queuePop(&q);
        printf("%d ",x);
        Node *cur = a[x]->next;
        while(cur!=NULL){
        	int next = cur->index;
            if(!c[next]){     //ν•΄λ‹Ή 인접 λ…Έλ“œκ°€ λ°©λ¬Έν•˜μ§€ μ•Šμ€ μƒνƒœλΌλ©΄
            	queuePush(&q, next);
                c[next] = 1;  //λ°©λ¬Έ
            }
            cur = cur->next;
        }
    }
}

int main(void){
	scanf("%d %d", &n, &m);
    a = (Node**)malloc(sizeof(Node*) * (MAX_SIZE));
    for(int i=1; i<=n; i++){
    	a[i] = (Node*)malloc(sizeof(Node));
        a[i]->next = NULL;
    }
    for(int i=0; i<m; i++){
    	int x, y;
        scanf("%d %d", &x, &y); //κ°„μ„ μ˜ 갯수만큼 μž…λ ₯λ°›λŠ”λ‹€
        addFront(a[x], y);
        addFront(a[y], x);
    }
    bfs(1);
    return 0;
}

 

 

 

λ°˜μ‘ν˜•
Comments