C++ Prims算法节点优先级
所以我得到了Prims算法的伪代码C++ Prims算法节点优先级,c++,algorithm,prims-algorithm,C++,Algorithm,Prims Algorithm,所以我得到了Prims算法的伪代码 INPUT: GRAPH G = (V,E) OUTPUT: Minimum spanning tree of G Select arbitrary vertex s that exists within V Construct an empty tree mst Construct an empty priority queue Q that contain nodes ordered by their “distance” from mst Insert
INPUT: GRAPH G = (V,E)
OUTPUT: Minimum spanning tree of G
Select arbitrary vertex s that exists within V
Construct an empty tree mst
Construct an empty priority queue Q that contain nodes ordered by their “distance” from mst
Insert s into Q with priority 0
while there exists a vertex v such that v exists in V and v does not exist in mst do
let v = Q.findMin()
Q.removeMin()
for vertex u that exists in neighbors(v) do
if v does not exist in mst then
if weight(u, v) < Q.getPriority(u) then
//TODO: What goes here?
end if
end if
end for
end while
return mst
输入:图G=(V,E)
输出:G的最小生成树
选择V中存在的任意顶点
构造一个空树mst
构造一个空的优先级队列Q,其中包含按其与mst的“距离”排序的节点
在优先级为0的Q中插入s
而存在一个顶点v,使得v存在于v中,而v不存在于mst do中
设v=Q.findMin()
Q.removeMin()
对于存在于相邻点(v)中的顶点u,请执行以下操作
如果mst中不存在v,则
如果重量(u,v)
在//TODOTODO中的内容是
Q.setPriority(u) = weight(u, v);
另外,你的队伍排得不好。除s之外的节点的优先级应初始化为∞.
作为psuedocode,我将其重写如下:
MST-PRIM(G,w,s)
for each u in G.V
u.priority = ∞
u.p = NULL //u's parent in MST
s.key = 0
Q = G.V // Q is a priority queue
while(Q!=∅)
u = EXTRACT-MIN(Q)
for each v in u's adjacent vertex
if v∈Q and w(u,v) < v.priority
v.p = u
v.priority = w(u,v)
MST-PRIM(G、w、s)
对于G.V.中的每个u
u、 优先级=∞
u、 p=NULL//u在MST中的父级
s、 键=0
Q=G.V//Q是一个优先级队列
while(Q=∅)
u=提取最小值(Q)
对于u的相邻顶点中的每个v
如果v∈Q和w(u,v)
您可以在算法介绍的第23.2章中找到它的原型