日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最小生成树——Prim(普利姆)算法

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最小生成树——Prim(普利姆)算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【0】README

0.1) 本文總結于 數據結構與算法分析, 源代碼均為原創, 旨在 理解Prim算法的idea 并用 源代碼加以實現;
0.2)最小生成樹的基礎知識,參見 http://blog.csdn.net/pacosonswjtu/article/details/49947085


【1】Prim算法相關

1.1)計算最小生成樹的一種方法是使其連續地一步一步長成。在每一步, 都要吧一個節點當做根并往上加邊,這樣也就把相關聯的頂點加到增長中的樹上;
1.2)在算法中的任一時刻, 我們都可以看到一個已經添加到樹上的頂點集, 而其余頂點尚未加到這顆樹中。此時, 算法在每一階段都可以通過選擇邊(u, v),使得(u, v)的值是所有u 在樹上但v不在樹上的邊的值中的最小者, 而找出一個新的頂點并吧它添加到這顆樹中;
1.3)具體步驟概括為:

  • step1)給定一個頂點為根節點;
  • step2)每一步加一條邊和一個頂點; (這也迎合了 頂點個數-邊個數=1 );

1.4)看個荔枝:

對上圖的分析(Analysis):
A1)可以看到, 其實Prim算法基本上和求最短路徑的 Dijkstra算法一樣, 因此和前面一樣,我們對每一個頂點保留值 Dv和Pv 以及一個指標,指示該頂點是已知的還是未知的。這里,Dv是連接v 到已知頂點的最短邊的權, 而 Pv則是導致Dv改變的最后的頂點。
A2)算法的其余部分一樣, 唯一不同的是: 由于Dv的定義不同, 因此它的更新法則不一樣。事實上,Prim算法的更新法則比 Dijkstra算法簡單:在每一個頂點v被選取后, 對于每一個與 v 鄰接的未知的w, Dw=min(Dw, Cw,v);

對上圖的分析(Analysis):
A1)該算法整個的實現實際上和 Dijkstra算法的實現是一樣的, 對于 Dijkstra算法分析所做的每一件事都可以用到這里。 不過要注意, Prim算法是在無向圖上運行的, 因此當編寫代碼的時候要記住要吧每一條變都要放到兩個鄰接表中。
A2)不用堆時的運行時間為O(|V|^2), 它對于稠密圖來說是最優的; 使用二叉堆的運行時間為 O(|E|log|V|), 它對于稀疏圖是一個好的界限;


【2】source code + printing results(將我的代碼打印結果 同 上圖中的手動模擬的prim算法的結果進行比較,你會發現, 它們的結果完全相同,這也證實了我的代碼的可行性)

2.1)download source code: https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter9/p237_prim
2.2)source code at a glance(for complete code , please click the given link above):

#include "prim.h"//allocate the memory for initializing unweighted table WeightedTable *initWeightedTable(int size) { WeightedTable* table;int i;table = (WeightedTable*)malloc(sizeof(WeightedTable) * size);if(!table){Error("out of space ,from func initWeightedTable");return NULL;}for(i = 0; i < size; i++){table[i] = makeEmptyWeightedTable(); if(!table[i])return NULL;}return table; } // allocate the memory for every element in unweighted table WeightedTable makeEmptyWeightedTable() {WeightedTable element;element = (WeightedTable)malloc(sizeof(struct WeightedTable));if(!element){Error("out of space ,from func makeEmptyWeightedTable");return NULL;} element->known = 0; // 1 refers to accessed , also 0 refers to not accessedelement->distance = MaxInt;element->path = -1; // index starts from 0 and -1 means the startup vertex unreaches other vertexsreturn element; }// allocate the memory for storing index of vertex in heap and let every element -1 int *makeEmptyArray(int size) {int *array;int i;array = (int*)malloc(size * sizeof(int));if(!array){Error("out of space ,from func makeEmptyArray");return NULL;} for(i=0; i<size; i++)array[i] = -1;return array; }//computing the unweighted shortest path between the vertex under initIndex and other vertexs void prim(AdjTable* adj, int size, int startVertex, BinaryHeap bh) { int adjVertex; int tempDistance;WeightedTable* table;int vertex; AdjTable temp; Distance tempDisStruct;int *indexOfVertexInHeap;int indexOfHeap;table = initWeightedTable(size); tempDisStruct = makeEmptyDistance();indexOfVertexInHeap = makeEmptyArray(size);tempDisStruct->distance = table[startVertex-1]->distance;tempDisStruct->vertexIndex = startVertex-1;insert(tempDisStruct, bh, indexOfVertexInHeap); // insert the (startVertex-1) into the binary heap table[startVertex-1]->distance = 0;// update the distance table[startVertex-1]->path = 0;// update the path of starting vertexwhile(!isEmpty(bh)){ vertex = deleteMin(bh, indexOfVertexInHeap).vertexIndex; // return the minimal element in binary heap//printBinaryHeap(bh);table[vertex]->known = 1; // update the vertex as accessed, also let responding known be 1temp = adj[vertex]->next;while(temp){adjVertex = temp->index; if(table[adjVertex]->known == 1) // judge whether table[adjVertex]->known is 1 or not{temp = temp->next;continue;}//tempDistance = table[vertex]->distance + temp->weight; // update the distancetempDistance = temp->weight;if(tempDistance < table[adjVertex]->distance){table[adjVertex]->distance = tempDistance;table[adjVertex]->path = vertex; //update the path of adjVertex, also responding path evaluated as vertex // key, we should judge whether adjVertex was added into the binary heap //if true , obviously the element has been added into the binary heap(so we can't add the element into heap once again)if(indexOfVertexInHeap[adjVertex] != -1) {indexOfHeap = indexOfVertexInHeap[adjVertex];bh->elements[indexOfHeap]->distance = tempDistance; // update the distance of corresponding vertex in binary heap}else // if not ture{tempDisStruct->distance = table[adjVertex]->distance;tempDisStruct->vertexIndex = adjVertex;insert(tempDisStruct, bh, indexOfVertexInHeap); // insert the adjVertex into the binary heap}} temp = temp->next; } printPrim(table, size, startVertex); printBinaryHeap(bh);printf("\n");} printf("\n"); } //print unweighted table void printPrim(WeightedTable* table, int size, int startVertex) {int i; char *str[4] = {"vertex","known","distance","path"};printf("\n\t === storage table related to Prim alg as follows: === "); printf("\n\t %6s%6s%9s%5s", str[0], str[1], str[2], str[3]); for(i=0; i<size; i++){ if(i != startVertex-1 && table[i]->path!=-1) printf("\n\t %-3d %3d %5d v%-3d ", i+1, table[i]->known, table[i]->distance, table[i]->path+1);else if(table[i]->path == -1)printf("\n\t %-3d %3d %5d %-3d ", i+1, table[i]->known, table[i]->distance, table[i]->path);elseprintf("\n\t *%-3d %3d %5d %-3d ", i+1, table[i]->known, table[i]->distance, 0);} }int main() { AdjTable* adj; BinaryHeap bh;int size = 7;int capacity;int i;int j; int startVertex;int adjTable[7][7] = {{0, 2, 4, 1, 0, 0, 0},{2, 0, 0, 3, 10, 0, 0},{4, 0, 0, 2, 0, 5, 0},{1, 3, 2, 0, 7, 8, 4},{0, 10, 0, 7, 0, 0, 6},{0, 0, 5, 8, 0, 0, 1},{0, 0, 0, 4, 6, 1, 0},};printf("\n\n\t ====== test for Prim alg finding weighted shortest path from adjoining table ======\n");adj = initAdjTable(size); printf("\n\n\t ====== the initial weighted adjoining table is as follows:======\n");for(i = 0; i < size; i++)for(j = 0; j < size; j++) if(adjTable[i][j]) insertAdj(adj, j, i, adjTable[i][j]); // insertAdj the adjoining table overprintAdjTable(adj, size); capacity = 7;bh = initBinaryHeap(capacity+1);//conducting prim alg to find minimum spanning tree(MST)startVertex = 1; // you should know our index for storing vertex starts from 0prim(adj, size, startVertex, bh); return 0; }

2.3)printing results:



總結

以上是生活随笔為你收集整理的最小生成树——Prim(普利姆)算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲最新色图 | 色久天| 亚洲国产97在线精品一区 | 正在播放经典国语对白 | 欧美日韩成人免费 | 日本亲与子乱人妻hd | 性生交大片免费看l | 国产精品国产三级国产aⅴ中文 | 91蜜臀精品国产自偷在线 | 国产精品一区二区在线看 | 在线99视频| 亚洲精品国产成人av在线 | 一区久久久 | 日韩三级成人 | 国产精品爱啪在线线免费观看 | 成人h网站 | 婷婷综合精品 | 午夜视频色| www.欧美一区二区三区 | 日韩久久久久久久久 | 男男gay羞辱feet贱奴vk | av激情在线 | 日韩色区 | 大毛片| 婷婷色综合网 | 中文字幕第66页 | 欧美综合精品 | av大全网站 | 热re99久久精品国产99热 | 免费看成人毛片 | 欲涩漫入口免费网站 | 国产一区二区三区免费在线观看 | 免费在线观看黄网 | 就是喜欢被他干 | 大学生三级中国dvd 日韩欧美一区二区区 | 最新91在线 | 国产无遮挡又黄又爽在线观看 | 日本美女视频一区 | 91精品国产乱码久久久久久久久 | 亚洲最新av网站 | 亚州av片 | 国产精品一二三四五区 | 日韩精品无码一区二区三区久久久 | 国内成人在线 | 黑人干日本少妇 | 欧美在线免费视频 | 黄色天堂 | 日本亲与子乱xxx | 另类激情亚洲 | 国产精品手机在线观看 | 男ji大巴进入女人的视频 | 国产91在线看 | 在线观看国产免费视频 | 日韩一区视频在线 | caoporn免费在线 | 亚洲成人激情小说 | 亚洲爱爱网站 | 欧美大片免费观看 | 婷婷丁香花五月天 | 久久久久亚洲精品系列色欲 | 污网站在线观看免费 | 最新不卡av | 天堂a在线 | 人人澡人人插 | 深夜在线网站 | 超碰95在线 | 中文字幕人妻一区二区 | 日本一区二区三区欧美 | 精品国产免费av | 国产一区自拍视频 | 怡红院成人影院 | 亚洲free性xxxx护士白浆 | 最新中文字幕在线 | 日本一区二区成人 | 91久久国产综合久久91精品网站 | 亚洲一区二区三区影视 | 女人扒开屁股让男人捅 | 国产精品久久伊人 | 桃色激情网 | 美女扒开腿让男生桶 | 韩国主播青草200vip视频 | 国产亚洲一区二区三区在线观看 | 成人av在线看 | 欧美日韩一卡二卡 | 久久这里只有精品首页 | 成年人在线免费看 | 激情综合站 | 国产色爱| 手机天堂av | 9l蝌蚪porny中文自拍 | 日韩精品成人一区二区在线 | 中文亚洲欧美 | 久久999| 亚洲欧美另类综合 | 中字av在线 | 女尊高h男高潮呻吟 | 欧洲视频一区二区三区 | 欧美天天 | 亚洲在线观看一区二区 |