普里姆演算法

2021-10-09 06:48:53 字數 1317 閱讀 3042

**實現:

package datastructure;

public

class

primdemo

;for

(string vertex : vertexs)

// a-b(5) a-c(7) a-g(2) b-d(9) b-g(3) c-e(8) d-f(4) e-g(4) e-f(5) f-g(6)

// 100表示不連通

int[

] weights =,,

,,,,

};graph.

insertedge

(weights)

;primmintree

(graph,0)

;/* output:

a-g 權值:2

g-b 權值:3

g-e 權值:4

e-f 權值:5

f-d 權值:4

a-c 權值:7

*/}/**

* 普里姆演算法計算最小生成樹

* @param graph 原始帶權圖

* @param index 從哪個頂點開始

*/public

static

void

primmintree

(graph graph,

int index)}}

isvisited[v2]=1

; system.out.

println

(graph.

getvertex

(v1)

+"-"

+ graph.

getvertex

(v2)

+" 權值:"

+ minweight)

; minweight =

100;}}

}class

graph2

extends

graph

public

void

insertedge

(int

edges)

}

普里姆演算法

include include include include using namespace std define max name 5 define max vertex num 20 權的上限值 typedef char vertex max name 頂點名字串 typedef int ad...

普里姆演算法

普里姆演算法的基本思想如下 假設n 是連通網,te是n上最小生成樹中邊的集合。從u 開始,te 開始,重複執行下述操作 在所有的u屬於u,v屬於v u的邊 u,v 的邊中找到權值最小的一條邊,並且併入te,同時u併入u,直到u v 先設乙個輔助陣列closedge max 初始時先把第乙個結點存入c...

普里姆演算法

include include include using namespace std define maxint 32767 define maxnum 100 typedef struct vnode,closedge maxnum typedef struct amgraph intlocat...