简体   繁体   English

图算法

[英]Graph Algorithms

What is the maximum and minimum number of edges to be considered in krushkal's algorithm with an example for both cases. 两种情况下的示例在krushkal算法中要考虑的最大和最小边数是多少。

What I thought was since the Krushkal's algorithm is for finding minimum spanning tree the maximum number of edges is (V-1) where V is the number of vertices. 我以为是因为Krushkal算法是用于查找最小生成树,所以最大边数是(V-1),其中V是顶点数。 Adding one more edge would result in a cycle in the graph. 再增加一条边将导致图形循环。 How can we obtain at a minimum value ? 我们如何获得最小值?

A tree of N vertices always has N-1 edges. N个顶点的树始终具有N-1个边。 Consequently you have to consider at least N-1 edges during Kruskal's algorithm. 因此,在Kruskal算法中,您必须至少考虑N-1个边。 An example may be a graph which is a tree. 一个例子可以是作为树的图。

Kruskal's algorithm stops when you've added V - 1 edges to your MST, so this is the minimum that have to be considered. 向MST添加V - 1边后,Kruskal算法停止运行,因此这是必须考虑的最小值。 This happens when the lowest value V - 1 edges of your graph do not form a cycle, and they will be added one after the other by the algorithm, after which it will stop. 当图形的最低值V - 1边没有形成循环时,会发生这种情况,并且算法将它们一个接一个地添加,然后停止。

For example, consider a complete graph with edges with cost 1 , which is minimum in the graph, between node 1 and every other node. 例如,考虑在节点1和其他每个节点之间具有成本为1边的完整图,该边在图中最小。 Make all the other edges have cost 2 . 使其他所有边都具有成本2

The worst case is when you must inspect every edge (of which there are O(V^2) ) until you finally select V - 1 . 最坏的情况是必须检查每个边(其中有O(V^2) ),直到最终选择V - 1为止。 This means that you have to force a lot of cycles to be created before the last edge is added. 这意味着您必须在添加最后一个边之前强制创建许多循环。

Consider a complete graph again. 再次考虑一个完整的图。 Have the V - 2 edges between node 1 and V - 2 nodes have cost 1, which is minimum in the graph. 节点1V - 2节点之间的V - 2边的成本为1,在图中最小。 These will be selected first. 将首先选择这些。 Now let node k be the one that is not part of a selected edge, so that is left out of the graph. 现在,让节点k为不属于所选边的节点,因此将其排除在图形之外。 Have the edge between node k and node 1 have the largest cost. 使节点k和节点1之间的边缘成本最大。 This will cause it to be inspected and added to the MST last, forcing the algorithm to inspect all O(V^2) edges before building the MST. 这将导致对其进行最后检查并添加到MST中,从而迫使算法在构建MST之前检查所有O(V^2)边缘。

Remember the Kruskal's algorithm processes edges in increasing order of their cost, rejecting edges that would form a cycle if added to the MST we are building. 请记住,Kruskal算法按成本递增顺序处理边,拒绝添加到我们正在构建的MST中会形成循环的边。

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

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