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 }