且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

普里姆算法

更新时间:2022-06-12 00:03:25

问题是您没有从队列中删除所有顶点实例=>同一顶点可以多次添加到结果中.

The problem is you do not remove all instances of vertex from the queue => the same vertex can be added several times into the result.

假设下图:

weight(0,1) = 1
weight(0,2) = 2
weight(1,2) = 3
weight(1,3) = 4
weight(2,3) = 5

在"FIRST ITERATION"之后,队列包含PriorityVertex(1,1),PriortyVertex(2,2).

After the "FIRST ITERATION" the queue contains PriorityVertex(1, 1), PriortyVertex(2, 2).

while循环的迭代次数:

Iterations of while cycle:

1) removed: PriorityVertex(1, 1) - edge (0,1) 
   added: PriorityVerterx(2, 3) and PriorityVertex(3, 4)
   queue: PriorityVertex(2, 2), PriorityVertex(2, 3), PriorityVertex(3, 4)

2) removed: PriorityVertex(2, 2) - edge (0,2)
   added: PriorityVertex(3, 5)
   queue: PriorityVertex(2, 3), PriorityVertex(3, 4), PriorityVertex(3, 5)

3) removed: PriorityVertex(2, 3) - edge (1,2), cycle in the result!