Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

61-tgyuuAn #212

Merged
merged 2 commits into from
Jun 30, 2024
Merged

61-tgyuuAn #212

merged 2 commits into from
Jun 30, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Jun 20, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์šฐ์ฃผ์‹ ๊ณผ์˜ ๊ต๊ฐ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋””์Šค์ฝ”๋“œ ์ถ”์ฒœ๋ฌธ์ œ ์ฑ„๋„ ์—์„œ ๋‚˜์™”๋˜

๋ณ„์ž๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ ๋ฌธ์ œ๋ž‘ ๋งค์šฐ ๋งค์šฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!







์–ด.. ์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋ ค๋ฉด ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”..

์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ด์š”..







๊ทผ๋ฐ ์œ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•ด์š”...

ํ ... ์กฐ๋งŒ๊ฐ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ธํŠธ ํ•˜๋‚˜ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค...........!

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Jun 20, 2024
@tgyuuAn tgyuuAn requested a review from alstjr7437 June 20, 2024 15:53
@tgyuuAn tgyuuAn self-assigned this Jun 20, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review June 20, 2024 15:58
@alstjr7437
Copy link
Member

์˜คํ˜ธ ์ด๋ฒˆ์— ํ•™๊ต ๊ฐ•์˜ ์ค‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (์žฌ)์ˆ˜๊ฐ•์„ ํ•˜๊ฒŒ ๋˜์–ด์„œ ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์šฐ๊ณ  C์–ธ์–ด๋กœ ๊ตฌํ˜„์„ ํ–ˆ์—ˆ๋Š”๋ฐ
์•„๋ž˜์™€ ๊ฐ™์ด ๊ตํ™ฉ๊ตฐ์˜ ๋„์›€์„ ๋ฐ›์•„ ์ž‘์„ฑํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค!!!

C์–ธ์–ด ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_NODE 1000
#define MAX_EDGES 10000

typedef struct {
    int start;
    int end;
    int weight;
} Edge;

int adjacency_matrix[MAX_NODE][MAX_NODE];
int result_matrix[MAX_NODE][MAX_NODE];

Edge edges[MAX_EDGES]; // ๊ฐ„์„  ๋ฐฐ์—ด
Edge result_edge[MAX_EDGES];
int parent[MAX_NODE];
int size[MAX_NODE];

int node_count = 0;
int edge_count = 0; // ํ˜„์žฌ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
int result_count = 0; // ํ˜„์žฌ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜

// ํŒŒ์ผ input ๋ฐ ๊ฐ„์„  ๋งŒ๋“ค๊ธฐ
int file_input(char *argv[]){
    FILE* fp = fopen(argv[1], "r");
    int max_node = 0;
    if (fp == NULL) {
        printf("File Not Found\n");
        exit(1);
    }

    char buffer[MAX_NODE];
    // ํŒŒ์ผ ์ฝ์–ด์˜ค๊ธฐ
    while (fgets(buffer, MAX_NODE, fp)) {
        char* p = strtok(buffer, " ");
        int col = 0;
        while (p) {
            adjacency_matrix[node_count][col++] = atoi(p);
            p = strtok(NULL, " ");
        }
        node_count++;
    }

    return 1;
}

// ์ •๋ ฌ ํ•จ์ˆ˜
int static compare (const void* first, const void* second)
{
    if (((Edge *)first)->weight > ((Edge *)second)->weight)
        return 1;
    else if (((Edge *)first)->weight < ((Edge *)second)->weight)
        return -1;
    else
        return 0;
}

void edge_setting(){
    // ํŒŒ์ผ ์ฝ์–ด์˜จ ๋ถ€๋ถ„ Edge๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
    for(int i = 0; i < node_count; ++i) {
        for(int j = i + 1; j < node_count; ++j) {
            if(adjacency_matrix[i][j] == -1){
                continue;
            }

            edges[edge_count].start = i;
            edges[edge_count].end = j;
            edges[edge_count].weight = adjacency_matrix[i][j];
            edge_count++;
        }
    }

    // weight๋ณ„๋กœ ์ •๋ ฌํ•ด์ฃผ๊ธฐ
    qsort(edges, edge_count, sizeof(Edge), compare);

    // // ์ •๋ ฌํ•œ ๊ฐ„์„  ์ถœ๋ ฅํ•˜๊ธฐ
    // for (int i = 0; i < edge_count; ++i) {
    //     printf("๊ฐ„์„  %d: start=%d, end=%d, weight=%d\n", i+1, edges[i].start, edges[i].end, edges[i].weight);
    // }

    for(int i = 0; i < edge_count; i++){
        parent[i] = i;
    }
}

// ๋ถ€๋ชจ ์ฐพ๊ธฐ 
int find(int node){
    while(node != parent[node]){
        parent[node] = parent[parent[node]];
        node = parent[node];
    }
    return parent[node];
}

// cycle ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ(๋ถ€๋ชจ๊ฐ€ ๊ฐ™์€์ง€)
int is_cycle(int node1, int node2){
    if (find(node1) == find(node2)){
        return 1;
    } else {
        return 0;
    }
}

// union , find
void unite(int node1, int node2){
    int parent1 = find(node1);
    int parent2 = find(node2);

    // ํ•œ์ชฝ์— ๋ชฐ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด dept ์‚ฌ์šฉ
    if(size[parent1] > size[parent2]){
        parent[parent2] = parent1;
        size[parent1] += size[parent2];
    } else {
        parent[parent1] = parent2;
        size[parent2] += size[parent1];
    }
}

// krusckal ์‹คํ–‰(๋ชจ๋‘ ์‹คํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์ดํด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ)
void krusckal(){
    for(int i = 0; i < edge_count; i++){
        int start = edges[i].start;
        int end = edges[i].end;
        int weight = edges[i].weight;

        if(!is_cycle(start,end)){
            unite(start, end);
            result_edge[result_count++] = edges[i];
        }
    }
}

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ•จ์ˆ˜
void result_print(){
    // ๋ฉ”ํŠธ๋ฆญ์Šค ๋งŒ๋“ค๊ธฐ
    memset(result_matrix, -1, sizeof(result_matrix));
    for (int i = 0; i < node_count; i++){
        result_matrix[i][i] = 0;
    }

    for (int i = 0; i < result_count; i++){
        int start = result_edge[i].start;
        int end = result_edge[i].end;

        result_matrix[start][end] = result_edge[i].weight;
        result_matrix[end][start] = result_edge[i].weight;
    }

    for(int i = 0; i < node_count; i++){
        printf("\t%c", 'A' + i);
    }
    printf("\n");

    for(int i = 0; i < node_count; i++){
        printf("%c ", 'A' + i); // ๋…ธ๋“œ ์ถœ๋ ฅ
        for(int j = 0; j < node_count; j++){
            printf("\t%d", result_matrix[i][j]);
        }
        printf("\n");
    }
}

int main(int argc, char *argv[]) {
    file_input(argv);

    edge_setting();

    krusckal();

    // // krusckal๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ„์„  ์ถœ๋ ฅํ•˜๊ธฐ
    // for (int i = 0; i < result_count; ++i) {
    //     printf("๊ฐ„์„  %d: start=%d, end=%d, weight=%d\n", i+1, result_edge[i].start, result_edge[i].end, result_edge[i].weight);
    // }
    
    result_print();

    return 0;
}

์•„์ง ํŒŒ์ด์ฌ์œผ๋กœ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ ์ž‘์„ฑ์„ ํ•ด๋ณธ ์ ์€ ์—†๋Š”๋ฐ ๋”ฐ๋กœ ๋ฐ”์˜์ง€ ์•Š๊ฒŒ ์‹œ๊ฐ„์ด ๋˜๋ฉด(์•„๋งˆ ๋‹ค์Œ์ฃผ์ฏค..)์— ๋…ธํŠธ๋‚˜ ์ •๋ฆฌ๋ฅผ ํ•œ๋ฒˆ ํ•ด์•ผ๊ฒ ๊ตฐ์š” ์žŠ์–ด๋ฒ„๋ฆฌ์ง€ ์•Š๊ฒŒ!!

Comment on lines +22 to +23
def calculate_distance(first, second):
return (abs(first[0] - second[0])**2 + abs(first[1] - second[1])**2)**0.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„์€ ์ขŒํ‘œ๋“ค์˜ ์ œ๊ณฑ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ๋ฃจํŠธ ์”Œ์›Œ์„œ ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ ์ด์šฉํ•ด์„œ ๋‘์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ๊ฐ€์š”???

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งž์”๋‹ˆ๋‹ค!!!!!!!!!!!!!!!!!!!!!!!

@tgyuuAn tgyuuAn merged commit c7d059d into main Jun 30, 2024
@tgyuuAn tgyuuAn deleted the 61-tgyuuAn branch June 30, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants