简体   繁体   English

最小生成树分析的Prims算法

[英]Prims Algorithm for Minimum Spanning Tree Analysis

Algorithm: 算法: 在此处输入图片说明

Graph: 图形: 在此处输入图片说明

(Note: Missing edge weight, (T, Y, 8)) (注意:缺少边缘重量,(T,Y,8))

Before the first iteration: 在第一次迭代之前:

The min priority Queue(min key at start) 最小优先级队列(启动时的最小键)

Q = [(S, 0), (T, ∞), (X, ∞), (Y, ∞), (Z, ∞)] Q = [(S,0),(T,∞),(X,∞),(Y,∞),(Z,∞)]

Iteration 1: 迭代1:

U = S U = S

Q = [(T, 6), (Y, 7), (X, ∞), (Z, ∞)] Q = [(T,6),(Y,7),(X,∞),(Z,∞)]

Updates Keys of T and Y 更新T和Y的键

Iteration 2: 迭代2:

U = T U = T

Q = [(Z, -4), (X, 5), (Y, 7)] Q = [(Z,-4),(X,5),(Y,7)]

Updates Keys of X, Y, Z 更新X,Y,Z键

Iteration 3: 迭代3:

U = Z U = Z

Q = [(X, 5), (Y, 7)] Q = [(X,5),(Y,7)]

No updates 没有更新

Iteration 4: 迭代4:

U = X U = X

Q = [(Y, 7)] Q = [(Y,7)]

No updates 没有更新

Iteration 5: 迭代5:

U = Y U = Y

Q = [] Q = []

No updates 没有更新

Queue empty, loop terminates 队列为空,循环终止

We have the following edges in our minimum spanning tree: 在最小生成树中,我们具有以下优势:

(S, T, 6), (T, Z, 5), (T, Z, -4), (S, Y, 7) (S,T,6),(T,Z,5),(T,Z,-4),(S,Y,7)

Cost = 6 + 5 - 4 + 7 = 14 费用= 6 + 5-4 + 7 = 14

This is obviously not a MST because we have other trees with lesser cost, 这显然不是MST,因为我们还有其他成本更低的树,

(S, Y, 7), (Y, X, -3), (X, T -2), (T, Z, -4) (S,Y,7),(Y,X,-3),(X,T -2),(T,Z,-4)

Cost = 7 - 3 - 2 - 4 = -2 成本= 7-3-2-4 = -2

Please help me identify where I have gone wrong. 请帮助我确定哪里出了问题。

For reference: (Please ignore the red edges) 供参考:(请忽略红色边缘)

Iteration 1: 迭代1: 在此处输入图片说明

Iteration 2: 迭代2: 在此处输入图片说明

Iteration 3: 迭代3: 在此处输入图片说明

Iteration 4: 迭代4: 在此处输入图片说明

Iteration 5: 迭代5: 在此处输入图片说明

In computer science, Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph . 在计算机科学中,Prim's算法是一种贪婪算法,可以为加权无向图找到最小生成树。

Source: https://en.wikipedia.org/wiki/Minimum_spanning_tree 资料来源: https : //zh.wikipedia.org/wiki/Minimum_spanning_tree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM