๐Ÿ’ป

[์ž๋ฃŒ๊ตฌ์กฐ] ๊ทธ๋ž˜ํ”„์˜ ๊ฐœ๋…๊ณผ ๊ตฌํ˜„ ๋ณธ๋ฌธ

์ž๋ฃŒ๊ตฌ์กฐ

[์ž๋ฃŒ๊ตฌ์กฐ] ๊ทธ๋ž˜ํ”„์˜ ๊ฐœ๋…๊ณผ ๊ตฌํ˜„

๋˜ํšจ๋‹ˆ 2020. 2. 21. 15:55

๊ทธ๋ž˜ํ”„๋ž€ ์‚ฌ๋ฌผ์„ ์ •์ (Vertex)๊ณผ ๊ฐ„์„ (Edge)์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. 

๊ทธ๋ž˜ํ”„๋Š” 2๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

1) ์ธ์ ‘ํ–‰๋ ฌ(Adjacency Matrix) : 2์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹

2) ์ธ์ ‘๋ฆฌ์ŠคํŠธ(Adjacency List) : ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹

 

 

 

๋ฌด๋ฐฉํ–ฅ ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์™€ ์ธ์ ‘ ํ–‰๋ ฌ

- ๋ชจ๋“  ๊ฐ„์„ ์ด ๋ฐฉํ–ฅ์„ฑ์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

- ๋ชจ๋“  ๊ฐ„์„ ์— ๊ฐ€์ค‘์น˜๊ฐ€ ์—†๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

- ๋ฌด๋ฐฉํ–ฅ ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์„ ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

#include <stdio.h>

int a[1001][1001];
int n,m;

int main(void){
	scanf("%d %d", &n, &m);  //๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
    for(int i=0; i<m; i++){
    	int x,y;
 
        scanf("%d %d", &x, &y); //์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์ 
        
        //๋ฐฉํ–ฅ์„ฑ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. 
        a[x][y] = 1;
        a[y][x] = 1;
    }
    for(int i=1; i<=n; i++){
    	for(int j=1; j<=n; j++){
        	printf("%d ", a[i][j]); //๋ฐฐ์—ด ์ถœ๋ ฅ
        }
    }
}
     

 

 

๋ฐฉํ–ฅ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์™€ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ

- ๋ชจ๋“  ๊ฐ„์„ ์ด ๋ฐฉํ–ฅ์„ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

- ๋ชจ๋“  ๊ฐ„์„ ์— ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

- ๋ฐฉํ–ฅ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์„ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

#include <stdio.h>
#include <stdlib.h>

/* ์Šคํƒ๊ธฐ๋ฐ˜์˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ */
typedef struct {
	int index;
    int distance;
    struct Node *next; // ๋‹ค์Œ ๋…ธ๋“œ๋ฅด ๊ธฐ๋ก
} Node;

/* ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์•ž์—๋‹ค๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ */
void addFront(Node *root, int index, int distance){
	Node *node = (Node*)malloc(sizeof(Node));
    node->index = index;
    node->distance = distance;
    node->next = root->next;
    root->next = node;
}

void showAll(Node *root){
	Node *cur = root->next;
    while(cur!=NULL){
    	printf("%d(๊ฑฐ๋ฆฌ: %d)", cur->index, cur->distance);
        cur = cur -> next;
    }
}

int main(void){
	int n,m;
    scanf("%d %d", &n, &m);
    Node** a = (Node**)malloc(sizeof(Node*) * (n+1))l
    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, distance;
        scanf("%d %d %d", &x, &y, &distance);
        addFront(a[x], y, distance);
    }
    for(int i=1; i<=n; i++){
    	printf("์›์†Œ [%d]: ", i);
        showAll(a[i]); //ํŠน์ •ํ•œ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋‹ค๋ฅธ ์ •์ ๋“ค์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        printf("\n");
    }
    return 0;
}
    

 

* ์ •๋ฆฌ

1) ์ธ์ ‘ ํ–‰๋ ฌ์€ ๋ชจ๋“  ์ •์ ‘๋“ค์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜์—ฌ O(V^2)์˜ ๊ณต๊ฐ„์„ ์š”๊ตฌํ•˜๋ฏ€๋กœ ๊ณต๊ฐ„ ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง€์ง€๋งŒ ๋‘ ์ •์ ์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด O(1)์˜ ์‹œ๊ฐ„์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. 

2) ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ์ •๋ณด๋งŒ์„ ์ €์žฅํ•˜์—ฌ O(E)์˜ ๊ณต๊ฐ„์„ ์š”๊ตฌํ•˜๋ฏ€๋กœ ๊ณต๊ฐ„ ํšจ์œจ์„ฑ์ด ์šฐ์ˆ˜ํ•˜์ง€๋งŒ ๋‘ ์ •์ ์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด O(V)์˜ ์‹œ๊ฐ„์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. 

๋ฐ˜์‘ํ˜•
Comments