Here is a Graph,我需要使用 Prim's 和 Kruskal's 算法找到G的最小生成树。
我使用Prim的算法找到了最小生成树。 Here is my attempt。
使用Kruskal算法很难找到最小生成树。我看过许多有关Kruskal图算法的视频,但最终得到了与Prim算法相同的图。
谁能告诉我如何使用Kruskal算法找到图的最小生成树。
答案 0 :(得分:1)
Prims and Kruskals will always give you the same answer if all the edges of the graph have distinct weights, as there is only a single min-spanning tree that exists. For graph having many edges with same weights, the algorithms could give you a different answer but not always. Depends on the way the nodes are explored in the implementation. This graph can have many different min-spanning trees.
As your graph has all distinct edge weights, you will always get the same answer.
答案 1 :(得分:0)
Prim和Kruskal的算法都找到最小的生成树。因为您给出的图的边权重互不相同,所以不会存在最小的生成树,只要两种算法正确实现,它们都将找到同一棵树。表示MST之间没有任何差异。