码迷,mamicode.com
首页 > 编程语言 > 详细

最小生成树(2)—— Kruskal算法

时间:2016-06-27 21:44:29      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

【由于时间不是很充足,现将完整代码放出,具体分析和思路,以后再更新】

【详细代码】经codeblocks-13.12调试

  1 #include<iostream>
  2 #include<stdio.h>
  3 #define MAX_ARC 1001    //边的最大权值
  4 #define MAX_LINE 401    //图的最大边数
  5 #define MAX_NUM 21      //图的最大顶点数
  6 #define ERROR 0
  7 #define OK 1
  8 using namespace std;
  9 
 10 typedef int VertexType;
 11 
 12 typedef struct Edges{
 13     VertexType head;
 14     VertexType tail;
 15     int weight;
 16 }Edges;
 17 
 18 typedef struct MGraph
 19 {
 20     VertexType vexs[MAX_NUM];
 21     Edges edges[MAX_LINE];
 22     int vexnum;
 23     int arcnum;
 24 }MGraph;
 25 
 26 int Find(int *parent,int f)//判断是否回路
 27 {
 28     while(parent[f]>0)
 29     {
 30         f=parent[f];
 31     }
 32     return f;
 33 }
 34 
 35 int LocateVertex(MGraph G,VertexType u)//根据顶点返回序号,主要用于顶点是字符的情况
 36 {
 37     int i;
 38     for(i=1;i<=G.vexnum;i++)
 39     {
 40         if(u==G.vexs[i])
 41         {
 42             return i;
 43         }
 44     }
 45     return ERROR;
 46 }
 47 
 48 void MiniSpanTree_Kruskal(MGraph G)
 49 {
 50     int parent[MAX_NUM];//判断边与边是否形成环路的辅助数组
 51     int i,n,m;
 52 
 53     for(i=1;i<=G.vexnum;i++)//初始化辅助数组
 54     {
 55         parent[i]=0;
 56     }
 57     for(i=1;i<=G.arcnum;i++)//循环每一条边
 58     {
 59         n=Find(parent,LocateVertex(G,G.edges[i].head));
 60         m=Find(parent,LocateVertex(G,G.edges[i].tail));
 61 
 62         if(n!=m)//不等说明没有形成环路,具体原因,可以自己画个图
 63         {
 64             parent[n]=m;
 65             cout<<"("<<G.edges[i].head<<","<<G.edges[i].tail<<")";
 66         }
 67     }
 68     cout<<endl;
 69 }
 70 
 71 int main()
 72 {
 73     MGraph G;
 74     cin>>G.vexnum>>G.arcnum;//输入总顶点书和总边数
 75 
 76     int i;
 77 
 78     for(i=1;i<=G.vexnum;i++)//输入顶点
 79     {
 80         cin>>G.vexs[i];
 81     }
 82 
 83     for(i=1;i<=G.arcnum;i++)//输入边(包含头,尾,权值)
 84     {
 85         cin>>G.edges[i].head>>G.edges[i].tail>>G.edges[i].weight;
 86     }
 87 
 88     //将edges按升序排序
 89     VertexType temp1;
 90     int temp2,j;
 91     for(i=1;i<=G.arcnum;i++)
 92     {
 93         for(j=1;j<=G.arcnum-i;j++)
 94         {
 95             if(G.edges[j].weight>G.edges[j+1].weight)
 96             {
 97                 temp1=G.edges[j].head;
 98                 G.edges[j].head=G.edges[j+1].head;
 99                 G.edges[j+1].head=temp1;
100 
101                 temp1=G.edges[j].tail;
102                 G.edges[j].tail=G.edges[j+1].tail;
103                 G.edges[j+1].tail=temp1;
104 
105                 temp2=G.edges[j].weight;
106                 G.edges[j].weight=G.edges[j+1].weight;
107                 G.edges[j+1].weight=temp2;
108             }
109         }
110     }
111 
112     MiniSpanTree_Kruskal(G);
113     return 0;
114 }

【运行结果】不同VertexType

技术分享

技术分享

 【后记】

算法思想提高智商,算法实现积累经验

最小生成树(2)—— Kruskal算法

标签:

原文地址:http://www.cnblogs.com/wust-owen/p/5621521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!