算法與數據結構|最小生成樹(最小支撐樹)

概念:設G=(V,E)是一個無向連通圖,生成樹上各邊的權值之和爲該生成樹的代價,在G的全部生成樹中,代價最小的生成樹就稱爲最小支撐樹,或稱最小生成樹。node

區別:最小生成樹是各邊權值和最小的數ios

   最優歸併樹是帶權外部路徑長度最短的樹算法

算法:Kruskal算法 && Prim算法數組

代碼來自 http://blog.csdn.net/luoshixian099/article/details/51908175spa

  1 #include <iostream>
  2 #include <vector>
  3 #include <queue>
  4 #include <algorithm>
  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<Arc> &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<vector<VertexData> > &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<Arc> vertexArc;
129     ReadArc(adjMat, vertexArc);//讀取邊信息
130     sort(vertexArc.begin(), vertexArc.end(), compare);//邊按從小到大排序
131     vector<vector<VertexData> > 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 }
相關文章
相關標籤/搜索