博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法与数据结构|最小生成树(最小支撑树)
阅读量:6878 次
发布时间:2019-06-26

本文共 4993 字,大约阅读时间需要 16 分钟。

概念:设G=(V,E)是一个无向连通图,生成树上各边的权值之和为该生成树的代价,在G的所有生成树中,代价最小的生成树就称为最小支撑树,或称最小生成树。

区别:最小生成树是各边权值和最小的数

   最优归并树是带权外部路径长度最短的树

算法:Kruskal算法 && Prim算法

代码来自

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 #define INFINITE 0xFFFFFFFF 7 #define VertexData unsigned int //顶点数据 8 #define UINT unsigned int 9 #define vexCounts 6 //顶点数量 10 char vextex[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; 11 struct node 12 { 13 VertexData data; 14 unsigned int lowestcost; 15 }closedge[vexCounts]; //Prim算法中的辅助信息 16 typedef struct 17 { 18 VertexData u; 19 VertexData v; 20 unsigned int cost; //边的代价 21 }Arc; //原始图的边信息 22 void AdjMatrix(unsigned int adjMat[][vexCounts]) //邻接矩阵表示法 23 { 24 for (int i = 0; i < vexCounts; i++) //初始化邻接矩阵 25 for (int j = 0; j < vexCounts; j++) 26 { 27 adjMat[i][j] = INFINITE; 28 } 29 adjMat[0][1] = 6; adjMat[0][2] = 1; adjMat[0][3] = 5; 30 adjMat[1][0] = 6; adjMat[1][2] = 5; adjMat[1][4] = 3; 31 adjMat[2][0] = 1; adjMat[2][1] = 5; adjMat[2][3] = 5; adjMat[2][4] = 6; adjMat[2][5] = 4; 32 adjMat[3][0] = 5; adjMat[3][2] = 5; adjMat[3][5] = 2; 33 adjMat[4][1] = 3; adjMat[4][2] = 6; adjMat[4][5] = 6; 34 adjMat[5][2] = 4; adjMat[5][3] = 2; adjMat[5][4] = 6; 35 } 36 int Minmum(struct node * closedge) //返回最小代价边 37 { 38 unsigned int min = INFINITE; 39 int index = -1; 40 for (int i = 0; i < vexCounts;i++) 41 { 42 if (closedge[i].lowestcost < min && closedge[i].lowestcost !=0) 43 { 44 min = closedge[i].lowestcost; 45 index = i; 46 } 47 } 48 return index; 49 } 50 void MiniSpanTree_Prim(unsigned int adjMat[][vexCounts], VertexData s) 51 { 52 for (int i = 0; i < vexCounts;i++) 53 { 54 closedge[i].lowestcost = INFINITE; 55 } 56 closedge[s].data = s; //从顶点s开始 57 closedge[s].lowestcost = 0; 58 for (int i = 0; i < vexCounts;i++) //初始化辅助数组 59 { 60 if (i != s) 61 { 62 closedge[i].data = s; 63 closedge[i].lowestcost = adjMat[s][i]; 64 } 65 } 66 for (int e = 1; e <= vexCounts -1; e++) //n-1条边时退出 67 { 68 int k = Minmum(closedge); //选择最小代价边 69 cout << vextex[closedge[k].data] << "--" << vextex[k] << endl;//加入到最小生成树 70 closedge[k].lowestcost = 0; //代价置为0 71 for (int i = 0; i < vexCounts;i++) //更新v中顶点最小代价边信息 72 { 73 if ( adjMat[k][i] < closedge[i].lowestcost) 74 { 75 closedge[i].data = k; 76 closedge[i].lowestcost = adjMat[k][i]; 77 } 78 } 79 } 80 } 81 void ReadArc(unsigned int adjMat[][vexCounts],vector
&vertexArc) //保存图的边代价信息 82 { 83 Arc * temp = NULL; 84 for (unsigned int i = 0; i < vexCounts;i++) 85 { 86 for (unsigned int j = 0; j < i; j++) 87 { 88 if (adjMat[i][j]!=INFINITE) 89 { 90 temp = new Arc; 91 temp->u = i; 92 temp->v = j; 93 temp->cost = adjMat[i][j]; 94 vertexArc.push_back(*temp); 95 } 96 } 97 } 98 } 99 bool compare(Arc A, Arc B)100 {101 return A.cost < B.cost ? true : false;102 }103 bool FindTree(VertexData u, VertexData v,vector
> &Tree)104 {105 unsigned int index_u = INFINITE;106 unsigned int index_v = INFINITE;107 for (unsigned int i = 0; i < Tree.size();i++) //检查u,v分别属于哪颗树108 {109 if (find(Tree[i].begin(), Tree[i].end(), u) != Tree[i].end())110 index_u = i;111 if (find(Tree[i].begin(), Tree[i].end(), v) != Tree[i].end())112 index_v = i;113 }114 115 if (index_u != index_v) //u,v不在一颗树上,合并两颗树116 {117 for (unsigned int i = 0; i < Tree[index_v].size();i++)118 {119 Tree[index_u].push_back(Tree[index_v][i]);120 }121 Tree[index_v].clear();122 return true;123 }124 return false;125 }126 void MiniSpanTree_Kruskal(unsigned int adjMat[][vexCounts])127 {128 vector
vertexArc;129 ReadArc(adjMat, vertexArc);//读取边信息130 sort(vertexArc.begin(), vertexArc.end(), compare);//边按从小到大排序131 vector
> Tree(vexCounts); //6棵独立树132 for (unsigned int i = 0; i < vexCounts; i++)133 {134 Tree[i].push_back(i); //初始化6棵独立树的信息135 }136 for (unsigned int i = 0; i < vertexArc.size(); i++)//依次从小到大取最小代价边137 {138 VertexData u = vertexArc[i].u; 139 VertexData v = vertexArc[i].v;140 if (FindTree(u, v, Tree))//检查此边的两个顶点是否在一颗树内141 {142 cout << vextex[u] << "---" << vextex[v] << endl;//把此边加入到最小生成树中143 } 144 }145 }146 147 int main()148 {149 unsigned int adjMat[vexCounts][vexCounts] = { 0 };150 AdjMatrix(adjMat); //邻接矩阵151 cout << "Prim :" << endl;152 MiniSpanTree_Prim(adjMat,0); //Prim算法,从顶点0开始.153 cout << "-------------" << endl << "Kruskal:" << endl;154 MiniSpanTree_Kruskal(adjMat);//Kruskal算法155 return 0;156 }

 

转载于:https://www.cnblogs.com/qq1337822982/p/8373003.html

你可能感兴趣的文章
10.C# -- 函数参数,参数数组,值传递函数,引用传递函数,输出函数,无参函数...
查看>>
BT5设置ip地址
查看>>
转载/验证码
查看>>
Surface、SurfaceView、SurfaceHolder和SurfaceHolder.Callback之间的联系
查看>>
什么是Data Store and Data Collector?
查看>>
我的友情链接
查看>>
php培训11.30
查看>>
Effective Java读后感
查看>>
windows下两个无线网卡 一个内网 一个外网
查看>>
tcp nat 负载均衡
查看>>
起点,游戏开发的梦想与技能点
查看>>
MPLS 流量工程的配置与各大属性调整详解
查看>>
107个常用Javascript语句
查看>>
我的友情链接
查看>>
Dataram_RAMDisk_v4_0_0安装和配置
查看>>
在window XP下使用vsphere client 5.5 访问vCenter 或者 ESXi5.5 连接错误
查看>>
35 个超棒的 Coming Soon 页面设计案例
查看>>
C语言第四天(位运算)
查看>>
硬RAID可以为NVMe SSD数据可靠性保驾护航吗?
查看>>
iPad 2 移植Siri 新手完全教程 适用所有越狱设备
查看>>