Prim 演算法的實現

2021-08-20 11:10:41 字數 1563 閱讀 8409

最小生成樹除了prim還有kruskal演算法。

prim演算法的實現,用到了優先佇列。

#include #include #include #include #include #include #include #include #include using namespace std;

/**********邊表************/

class edgenode

};/******====頂點表**********==*/

class vertexnode

};/*********自定義比較函式(優先佇列使用)************/

class mycompare

bool operator() (edgenode *a, edgenode *b) const }

};/*********無向圖的資料結構*************/

class graph

init_adjlist();

while(getline(fin, line))

}/********初始化鄰接表的頂點表*******/

void init_adjlist()

}/************加邊,頭插法*****************/

void addedge(int a, int b, int _cost)

/************列印看結果************/

void print()

fout << endl;

} }/*********************************/

public:

int numvertexes; //頂點數目

int numedges; //邊數目

vectoradjlist; //圖的鄰接表

};/***********prim演算法實現**********/

class prims

p = p->next;

}edgenode *p1 = new edgenode(0,0,0);

p1 = pq.top();

pq.pop();

s = p1->adjvex;

if(!is_marked(s)) //如果沒有加入進最小生成樹

} }/*******************判斷是否加入最小生成樹***************/

bool is_marked(int a)

}return false;

}/***************輸出看結果**************/

void write()

fout << endl;

} }public:

int length;

int sum = 0;

vectormarked;//最小生成樹木的頂點表

vector> tree; //最小生成樹的邊 ,陣列儲存

priority_queue, mycompare> pq; //優先佇列

};int main()

prim演算法的實現

新增鏈結描述 執行了一下prim 演算法 覺得這個程式還可以 後期可以在此基礎上做較大的改動 故此分享了這個 include define maxvex 10 define true 1 define false 0 define inf 65535 此處是權值極大值 define length a...

Prim演算法實現

include stdio.h define max vertex num 20 typedef struct arccellarccell,adjmatrix max vertex num max vertex num typedef struct mgraph struct closedge m...

Prim演算法實現

prim 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 4...