数据结构复习代码——基于图的邻接矩阵实现下的Prim算法、kruskal算法

1、基于图的邻接矩阵实现下的Prim算法

https://www.cnblogs.com/lx06/p/16478599.html


  #define T char
  #define E int
  #define MAX_COST 0x7FFFFFFF

//其他相关代码,可在上述网页链接中找到
E    GetWeight(GraphMtx *g,int v1,int v2)
{
    if(v1 == -1||v2 == -1)
    {
        return MAX_COST;
    }
    return g->Edge[v1][v2];
}

void MinSpanTree_Prim(GraphMtx *g,T vertex)
{
    int n = g->NumVertices;
    E *lowcost = (E*)malloc(sizeof(E)*n);           //lowcost[n]
    int *mst = (int*)malloc(sizeof(int)*n);         //mst[n]
    assert(lowcost!=nullptr && mst!=nullptr);
    int k = GetVertexPos(g,vertex);

    for(int i=0;i<n;i++)
    {
        if(i != k)
        {
            lowcost[i] = GetWeight(g,k,i);         //获取k到i的权值
            mst[i] = k;                             //记录k---下标
        }
        else
        {
            lowcost[i] = 0;
        }

    }
    int mins,min_index;
    int begin,end;
    E cost;
    for(int i=0;i<n-1;++i)
    {
        mins = MAX_COST;
        min_index = -1;
        for(int j=0;j<n;++j)
        {
            if(lowcost[j]!=0&&lowcost[j]<mins)
            {
                mins = lowcost[j];
                min_index = j;
            }
        }
        begin = mst[min_index];
        end = min_index;
        printf("%c-->%c:%d\n",g->VerticesList[begin],g->VerticesList[end],mins);

        lowcost[min_index] = 0;
        for(int j=0;j<n;++j)
        {
            cost = GetWeight(g,min_index,j);
            if(cost<lowcost[j])
            {
                lowcost[j] = cost;
                mst[j] = min_index;
            }
        }

    }

}

 

2、kruskal

bool Is_Same(int *father,int i,int j)
{
    while(father[i] != i)
    {
        i = father[i];
    }
    while(father[j] != j)
    {
        j = father[j];
    }
    return i==j;
}

void Mark_Same(int *father,int i,int j)
{
    while(father[i]!=i)
    {
        i = father[i];
    }
    while(father[j]!=j)
    {
        j = father[j];
    }
    father[j] = i;
}

int cmp(const void *a,const void *b)
{
    return (*(Edge*)a).cost - (*(Edge*)b).cost;
}
void MinSpanTree_Kruskal(GraphMtx *g)
{
    int n = g->NumVertices;
    Edge *edge = (Edge*)malloc(sizeof(Edge)*(n*(n-1)/2));
    assert(edge!=NULL);

    int k = 0;
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            if(g->Edge[i][j]!=0 && g->Edge[i][j]!=MAX_COST)
            {
                edge[k].x = i;
                edge[k].y = j;
                edge[k].cost = g->Edge[i][j];
                k++;
            }
        }
    }
    int v1,v2;
    for(int i=0;i<k;i++)
    {
        v1 = edge[i].x;
        v2 = edge[i].y;
        printf("%c-->%c:%d\n",g->VerticesList[v1],g->VerticesList[v2],edge[i].cost);
    }
    printf("-----------------------------\n");

    qsort(edge,k,sizeof(Edge),cmp);
    int *father = (int*)malloc(sizeof(int)*n);
    assert(father!=nullptr);
    for(int i=0;i<n;++i)
    {
        father[i] = i;
    }
    for(int i=0;i<=n;++i)
    {
        if(!Is_Same(father,edge[i].x,edge[i].y))
        {
            v1 = edge[i].x;
            v2 = edge[i].y;
            printf("%c-->%c:%d\n",g->VerticesList[v1],g->VerticesList[v2],edge[i].cost);
            Mark_Same(father,edge[i].x,edge[i].y);
        }
    }
}
View Code

 

posted @ 2022-07-23 19:58  往心。  阅读(42)  评论(0编辑  收藏  举报