Kruskal算法的C ++实现

时间:2017-02-15 21:10:57

标签: c++ algorithm quicksort minimum-spanning-tree kruskals-algorithm

我正在尝试实施Kruskal的算法。这是我正在使用的结构的地图:

g =边缘阵列,它保持左端和右端以及边缘的重量;

c =记忆conex组件的数组; c [N] =我们在其中找到第N个顶点的conex分量;

a =记忆MST的数组;

m = nr个顶点;

n = nr个节点。

以下代码存在两个问题:

1)对于以下输入,它输出MST的成本 18 (这是错误的,成本实际上 14 ):

7(= m)

6(= n)

1 2 9

1 3 5

1 4 2

2 3 7

3 5 3

4 6 1

5 6 1

2)逐步编译代码并没有给出任何错误,虽然程序的实际执行在某个时刻停止,但是在打印MST的成本时我会想到它。

感谢您的帮助!这是代码:

#include<stdio.h>
#include<stdlib.h>
#define grafMAX 101

FILE *fin = fopen("grafin.txt","r");
FILE *fout = fopen("grafout.txt","w");

struct Vertex{
    int first,last,Cost;
};

void read_graf(Vertex **g, int *c, int &m, int &n){
    int x,y,w;
    fscanf(fin,"%d%d",&m,&n);
    *g = (Vertex *)malloc(m*sizeof(Vertex));
    for(int i=1;i<=m;++i){
        fscanf(fin,"%d%d%d",&x,&y,&w);
        (*g+i)->first = x;
        (*g+i)->last = y;
        (*g+i)->Cost = w;
    }
    for(int i=1;i<=n;++i)
        c[i] = i;
}

int costMST(Vertex *g, int *a, int n){
    int MST = 0;
    for(int i=1;i<n;++i)
        MST += g[a[i]].Cost;
    return MST;
}

void Kruskal(Vertex *g, int *c, int *a, int n){
    int nr = 0, mini, maxi;
    for(int i=1;nr<n-1;++i)
        if(c[g[i].first] != c[g[i].last]){
            a[++nr] = i;
            if(c[g[i].first] < c[g[i].last]){
                mini = c[g[i].first];
                maxi = c[g[i].last];
            }
            else{
                maxi = c[g[i].first];
                mini = c[g[i].last];
            }
            for(int j=1;j<=n;++j)
                if(c[j] == maxi)
                    c[j] = mini;
        }
}

inline int cmp(const void *a, const void *b){
    return (((Vertex *)a)->Cost - ((Vertex *)b)->Cost);
}

int a[grafMAX], c[grafMAX];

int main(){
    Vertex *g;
    int m, n;
    read_graf(&g,c,m,n);
    qsort(g,m,sizeof(Vertex),cmp);
    Kruskal(g,c,a,n);
    fprintf(fout,"The cost of the MST is: %d.\n",costMST(g,a,n));
    fclose(fin);
    fclose(fout);
    return 0;
}

1 个答案:

答案 0 :(得分:3)

你的代码中有很多错误,我认为因为你是从1而不是0对你的顶点进行编号。其中一个错误导致它崩溃,我认为另一个导致了错误的结果要生产。

我将所有内部编号更改为基于0并且已经使其工作。我重命名了你的变量,因为它们是非常荒谬的命名(你称之为Vertex是一个边缘),我无法理解它们的代码。

我担心我失去了改变的所有内容,但是如果你将它与原始代码进行比较,我希望你能看到我做了什么。

请注意,我添加了一些调试行。当你无法弄清楚你的代码在做什么时,只需打印相关变量,你很快就会发现问题所在。

#include<stdio.h>
#include<stdlib.h>
#define grafMAX 101

FILE *fin = fopen("grafin.txt","r");
FILE *fout = fopen("grafout.txt","w");

struct Edge {
    int first,last,Cost;
};

void read_graf(Edge **g, int *components, int &num_edges, int &num_vertices){
    int x,y,w;
    fscanf(fin,"%d %d",&num_edges,&num_vertices);
    *g = (Edge *)malloc(num_edges*sizeof(Edge));
    for(int i=0;i<num_edges;++i){
        fscanf(fin,"%d %d %d",&x,&y,&w);
        (*g+i)->first = x - 1;
        (*g+i)->last = y - 1;
        (*g+i)->Cost = w;
    }
    for(int i=0;i< num_vertices;++i)
        components[i] = i;
}

int costMST(Edge *edges, int *answer, int num_edges){
    int MST = 0;
    for(int i=0;i<num_edges;++i)
        MST += edges[answer[i]].Cost;
    return MST;
}

void print_components(const int* components, int num_components) 
{
    for (int i = 0; i < num_components; i++) {
        printf("Vertex %d is in component %d\n", i, components[i]);
    }
    putchar('\n');
}

void print_edge(const Edge& edge, int index)
{
    printf("Edge %d connecting %d to %d with weight %d", index, edge.first, edge.last, edge.Cost);
}

void Kruskal(Edge *edges, int *components, int *answer, int num_edges, int num_vertices){
    int nr = 0, mini, maxi;
    for(int i=0;i<num_edges && nr < num_vertices - 1;++i) {
        printf("Considering ");
        print_edge(edges[i], i);
        putchar('\n');
        if(components[edges[i].first] != components[edges[i].last]){
            printf("Adding ");
            print_edge(edges[i], i);
            putchar('\n');
            answer[nr++] = i;
            if(components[edges[i].first] < components[edges[i].last]){
                mini = components[edges[i].first];
                maxi = components[edges[i].last];
            }
            else{
                maxi = components[edges[i].first];
                mini = components[edges[i].last];
            }
            for(int j=0;j<num_vertices;++j)
                if(components[j] == maxi)
                    components[j] = mini;
            print_components(components, num_vertices);
        }
        else {
            printf("Rejecting ");
            print_edge(edges[i], i);
            putchar('\n');
        }
    }
}

inline int cmp(const void *a, const void *b){
    return (((Edge *)a)->Cost - ((Edge *)b)->Cost);
}

int answer[grafMAX], components[grafMAX];

int main(){
    Edge *edges;
    int num_edges, num_vertices;
    read_graf(&edges,components,num_edges,num_vertices);
    qsort(edges,num_edges,sizeof(Edge),cmp);
    Kruskal(edges,components,answer,num_edges,num_vertices);
    fprintf(fout,"The cost of the MST is: %d.\n",costMST(edges,answer,num_vertices - 1));
    fclose(fin);
    fclose(fout);
    return 0;
}