Prim算法求最小生成樹


Prim算法求圖的最小生成樹(使用的圖的數據結構是圖的鄰接矩陣存儲表示)

/* minCost數組:該數組是結構數組,即每個元素是一個結構類型。該結構有兩個域:lowCost用來保存所有已經在
*最小生成樹中的頂點,到所有還沒有在最小生成樹中的頂點的所有權值中的最小的;vertax域用
* 來保存是哪個在最小生成樹中的頂點擁有着這個最小的權值。並且這個數組的下標隱含着頂點的
* 信息。例如,第2個數組元素在程序運行中的某一個時刻表示:已經形成的MST中的vertax頂點到
* 圖中的第2個頂點的權值為lowCost,並且是樹內頂點和數外頂點鏈接的最小的權值。
* 所以這個數組要不斷的更新。
* 假設整個生成樹的過程從頂點a開始,那么這個數組的初始元素為該圖的鄰接矩陣中頂點a的一行
* (該行表示的是a於其他的頂點的權值),並且將數組的vertax都設置為a。表示從MST中的頂點a到
* 數外的所有頂點的權值,然后從該數組中選出lowCost最小的值。為生成樹的第二個頂點。
* 現在樹中有兩個頂點,所以需要比較是哪個頂點和數外的其他頂點的權值比較小,minCost數組
* 中的信息是樹中頂點到其他頂點的最小權值,所以和將要加入的新的頂點的鄰接矩陣中的一行(
* 存放的是該頂點與所有其他的頂點的權值)
* 進行比較即可,小的就更新。
* */

  1 #include<stdio.h>
  2 
  3 #define MAX_VERTAX_SIZE 20
  4 #define INFINITE    65535
  5 #define OK         1
  6 #define ERROR         0
  7 
  8 //圖的鄰接矩陣表示的結構定義
  9 typedef int Status;
 10 typedef char VertaxElemType;
 11 typedef struct GraphAM{
 12     VertaxElemType VertaxArray[MAX_VERTAX_SIZE];
 13     int AdjacencyMatrix[MAX_VERTAX_SIZE][MAX_VERTAX_SIZE];
 14     int vertaxNum;
 15     int eageNum;
 16 }GraphAM;
 17 
 18 //用於Prim算法的輔助結構:
 19 typedef struct MinCostEage{
 20     VertaxElemType vertax;
 21     int lowCost;
 22 }MinCostEage;
 23 
 24 int LocateVertax(GraphAM G, VertaxElemType c){
 25     int i;
 26     for( i = 0; i < G.vertaxNum; i++ ){
 27         if( c == G.VertaxArray[i] )
 28             return i;
 29     }
 30     return -1;
 31 }
 32 Status CreateUDG(GraphAM* G){
 33     int i,j,index_v,index_w, weight;
 34     VertaxElemType v,w;
 35     printf("  Greate UndiGraph with Cost\n");
 36     printf("Please enter the number of Vertax and Eage:");
 37     scanf("%d %d%*c", &(G->vertaxNum), &(G->eageNum));
 38 
 39     printf("ok, please enter the value of the vertaxes:\n");
 40     for( i = 0; i < G->vertaxNum; i++ ){
 41         scanf("%c%*c", &(G->VertaxArray[i]));
 42     }
 43     for( i = 0; i < G->vertaxNum; i++ )
 44         for( j = 0; j < G->vertaxNum; j++ )
 45             G->AdjacencyMatrix[i][j] = INFINITE;
 46     for( i = 0; i < G->eageNum; i++ ){
 47         printf("ok,please enter the two Vertax and Wight of eage %d,\nNote:Seperated by Space: ", i+1);
 48         scanf("%c %c %d%*c", &v, &w, &weight);
 49         if( LocateVertax(*G, v) != -1 )
 50             index_v = LocateVertax(*G, v);
 51         else
 52             return ERROR;
 53         if( LocateVertax(*G, w) != -1 )
 54             index_w = LocateVertax(*G, w);
 55         else
 56             return ERROR;
 57         G->AdjacencyMatrix[index_v][index_w] = G->AdjacencyMatrix[index_w][index_v] = weight;
 58     }
 59     return OK;
 60 }
 61 void PrintAdjacencyMatrix(GraphAM G){
 62     printf("Show the Adjacency Matrix of Graph('#' repersents infinite)\n");
 63     int i,j;
 64     for( i = 0; i < G.vertaxNum; i++ ){
 65         for( j = 0; j< G.vertaxNum; j++ ){
 66             if( G.AdjacencyMatrix[i][j] != INFINITE )
 67                 printf("%5d", G.AdjacencyMatrix[i][j]);
 68             else
 69                 printf("    #");
 70         }
 71         printf("\n\n");
 72     }
 73 }
 74 //求圖G的最小生成樹,從頂點v開始
 75 Status Prim(GraphAM G, VertaxElemType v){
 76     int i,index_v,min,min_index,j;
 77     index_v = LocateVertax(G, v);                
 78     MinCostEage minCost[MAX_VERTAX_SIZE];                //數組的下標隱含是第幾個頂點
 79     for( i = 0; i < G.vertaxNum; i++ ){        
 80         minCost[i].lowCost = G.AdjacencyMatrix[index_v][i];     //初始化數組為起始頂點的AdjacencyMatrix[起始頂點]信息
 81         minCost[i].vertax = v;                        //MST中的哪個頂點與數外有本數組元素中的lowCost值
 82     }
 83     minCost[index_v].lowCost = 0;                    //已經在MST中的標志,使得沒有機會參加數組中lowCost的選取
 84     printf("MST(Minimum Cost Spinning Tree):\n");
 85     for( i = 1; i < G.vertaxNum; i++ ){
 86         for( j = 0; j < G.vertaxNum; j++ ){            //找第一個非0的作為min,為了避免第一個是0
 87             if( minCost[j].lowCost != 0 ){
 88                 min = minCost[j].lowCost;
 89                 min_index = j;
 90                 break;
 91             }
 92         }
 93         for( j = 0; j < G.vertaxNum; j++ ){            //尋找該數組中的當前的最小權值
 94             if( minCost[j].lowCost > 0 && minCost[j].lowCost < min ){
 95                 min = minCost[j].lowCost;
 96                 min_index = j;
 97             }
 98         }
 99         printf("(%c, %c)\t", minCost[min_index].vertax, G.VertaxArray[min_index]);//數組的下標存儲了第二個%c的需要的信息,
100                                               //vertax域提供了第一個%c需要的信息
101         minCost[min_index].lowCost = 0;
102         for( j = 0; j < G.vertaxNum; j++ ){
103             if( G.AdjacencyMatrix[min_index][j] < minCost[j].lowCost ){
104                 minCost[j].lowCost = G.AdjacencyMatrix[min_index][j];
105                 minCost[j].vertax = G.VertaxArray[min_index];
106             }
107         }
108     }
109 }
110 
111 int main(){
112     GraphAM G;
113     CreateUDG(&G);
114     PrintAdjacencyMatrix(G);
115     Prim(G, 'a');
116     return 0;
117 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM