最小生成樹(克魯斯卡爾方法)

#include<stdio.h>
#include<stdlib.h>
#define M 20
#define MAX 20
typedef struct
{

     int begin;
     int end;
     int weight;
}edge;

typedef struct
{

     int adj;
     int weight;

}AdjMatrix [MAX][MAX];

typedef struct
{

     AdjMatrix arc;
     int vexnum,arcnum;

}MGraph;

 

void CreatGraph(MGraph*);

void sort(edge*,MGraph*);

void MiniSpanTree(MGraph*);

int Find(int*,int);

void Swapn(edge*,int,int);

 

void CreatGraph(MGraph*G)//構造圖

{

     int i,j,n,m;

     printf("請輸入城市數及邊數:");

     scanf("%d%d",&G->vexnum,&G->arcnum);

     for(i=1;i<=G->vexnum;i++)//初始化圖

     {

          for(j=1;j<=G->vexnum;j++)

          {

               G->arc[i][j].adj=G->arc[j][i].adj=0;

          }

     }

     printf("請輸入有道路連通的2個城市及他們之間的造價費用(城市1  城市2  費 用):\n");
     for(i=1;i<=G->arcnum;i++)//輸入邊和費用

     {

          scanf("%d%d",&n,&m);

          G->arc[n][m].adj=G->arc[m][n].adj=1;

          scanf("%d",&G->arc[n][m].weight);

     }

     printf("鄰接矩陣爲:\n");

     for(i=1;i<=G->vexnum;i++)

     {

          for(j=1;j<=G->vexnum;j++)

          {

               if(G->arc[i][j].adj==1)

                    printf("%d ",G->arc[i][j].weight);

               else
       printf("%d ",G->arc[i][j].adj);

               G->arc[j][i].weight=G->arc[i][j].weight;

          }

          printf("\n");

     }

}

 

void sort(edge edges[],MGraph *G)//對權值進行排序

{

     int i,j;
     for(i=1;i<G->arcnum;i++)

     {

         for(j=i+1;j<=G->arcnum;j++)

         {

              if(edges[i].weight>edges[j].weight)

              {

                   Swapn(edges,i,j);

              }

         }

     }

     printf("按造價排序之後的邊順序爲(序號 邊 費用):\n");

     for(i=1;i<=G->arcnum;i++)

     {

         printf("%d. <%d,%d>  %d\n",i,edges[i].begin,edges[i].end,edges[i].weight);

     }

}

 

void Swapn(edge*edges,int i,int j)
{

     int temp;

     temp=edges[i].begin;

     edges[i].begin=edges[j].begin;

     edges[j].begin=temp;

     temp=edges[i].end;

     edges[i].end=edges[j].end;

     edges[j].end=temp;

     temp=edges[i].weight;

     edges[i].weight=edges[j].weight;

     edges[j].weight=temp;

}

 

void MiniSpanTree(MGraph *G)//生成最小生成樹

{

     int i,j,n,m,Mincost=0;

     int k=1;

     int parent[M];

     edge edges[M];

     for(i=1;i<G->vexnum;i++)

     {

         for(j=i+1;j<=G->vexnum;j++)

         {

              if(G->arc[i][j].adj==1)

              {

                   edges[k].begin=i;
                    edges[k].end=j;

                    edges[k].weight=G->arc[i][j].weight;

                    k++;

               }

          }

     }

     sort(edges,G);

     for(i=1;i<=G->arcnum;i++)

     {

          parent[i]=0;

     }

     printf("最小生成樹爲:\n");

     for(i=1;i<=G->arcnum;i++)//核心部分

     {

          n=Find(parent,edges[i].begin);

          m=Find(parent,edges[i].end);

          if(n!=m)     //如果m、n不等說明此邊沒有與現有的生成樹形成環路

          {

               parent[n]=m; //數組下標記錄了邊起點,數組值記錄了邊終點

               printf("<%d,%d>  %d\n",edges[i].begin,edges[i].end,edges[i].weight);

               Mincost+=edges[i].weight;

          }

     }

     printf("使各城市間能夠通信的最小費用爲:Mincost=%d\n",Mincost);

}

 

int Find(int*parent,int f) //查找從f這個座標開始能走到的最遠的結點是哪裏
{
     while(parent[f]>0) //如果最後走到的最遠的結點相同,說明是有環路生成的
     {

          f=parent[f];

     }
     return f;

}

 

void main()//主函數

{

     MGraph*G;

     G=(MGraph*)malloc(sizeof(MGraph));

     CreatGraph(G);

     MiniSpanTree(G);
  system("pause");
 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章