简体   繁体   English

在图中的O(V + E)时间中查找MST

[英]Find a MST in O(V+E) Time in a Graph

Similar question has been asked before, as in https://cs.stackexchange.com/questions/28635/find-an-mst-in-a-graph-with-edge-weights-from-1-2 之前也曾问过类似的问题,例如https://cs.stackexchange.com/questions/28635/find-an-mst-in-a-graph-with-edge-weights-from-1-2

The question is: Given a connected undirected graph G=(V,E) and a weight function w:E→{1,2}, suggest an efficient algorithm that finds an MST of the graph in O(V+E) without using Kruskal. 问题是:给定一个连通的无向图G =(V,E)和权重函数w:E→{1,2},建议一种高效的算法,无需使用O(V + E)即可找到图的MST克鲁斯卡。

I had a look at the suggested solutions on the thread above, but am still not sure how to make it work. 我在上面的线程中查看了建议的解决方案,但仍不确定如何使其工作。 The first suggestion doesn't consider components. 第一条建议不考虑组件。 The second suggestion doesn't provide more details on how to identify if the newly considered edge will form a cycle if it is added to the current MST. 第二个建议没有提供更多有关如何识别新考虑的边沿(如果将其添加到当前MST中)是否会形成一个循环的更多详细信息。 The tricky part is how to identify if two vertices are in the same component in liner time. 棘手的部分是如何确定直线时间中两个顶点是否在同一分量中。

My current thought is 我目前的想法是

  1. sort the vertices, which can be done in linear time 对顶点排序,可以在线性时间内完成
  2. consider edges with weight 1 first. 首先考虑权重为1的边。 add the edge to the MST when the number of edges is less than or equal to |V1|-1. 当边的数量小于或等于| V1 | -1时,将边添加到MST中。 V1 are the vertices on the edges with a weight of 1. We need to make sure all the vertices with a weight of 1 is checked. V1是权重为1的边上的顶点。我们需要确保选中权重为1的所有顶点。 Hash set can be used to store V1 and edges. 哈希集可用于存储V1和边。
  3. add V2 to the graph by using the same logic. 使用相同的逻辑将V2添加到图形中。

Could anyone suggest if my thought has flaws? 谁能说出我的想法是否有缺陷? If so, what is the best way to tackle this question? 如果是这样,解决这个问题的最佳方法是什么? Thank you very much! 非常感谢你!

I would suggest you to do something like the second answer in the given question: 我建议您对给定问题做类似第二个答案的操作:

This is prim's algorithm : 这是prim的算法:

Start by picking any vertex to be the root of the tree. 首先选择任何顶点作为树的根。

While the tree does not contain all vertices in the graph find shortest edge leaving the tree and add it to the tree . 虽然树在图中未包含所有顶点,但找到离开树的最短边并将其添加到树中。

So now if we can perform the finding in the set of edges leaving the tree in O(1) time and we can keep the set updated so the search can always happen in constant time in total O(|E|) time, then we are good to go. 因此,现在,如果我们可以在O(1)的时间内对离开树的边缘集合进行查找,并且可以使该集合保持更新,以便总O(| E |)的时间始终在恒定的时间内进行搜索,那么我们很好。

Now for this to happen, think of the set of edges leaving the tree as a linked list. 现在,要使这种情况发生,请考虑将树保留为链接列表的一组边。 now whenever a vertex is added to the set of vertexes that form the MST, iterate through its adjacency list and add the edges of weight 1 to the front of the list, and add the edges of weight 2 to the end of the list. 现在,只要将顶点添加到构成MST的一组顶点中,就迭代其邻接表,并将权重1的边添加到列表的前面,并将权重2的边添加到列表的末尾。 Now whenever you want the minimum edge leaving the tree just take one from the front of the linked list! 现在,只要您想离开树的最小边缘,就从链接列表的前面选择一个!

The only problem with this method is that you should only add the edges to the list that are "leaving the tree"! 这种方法的唯一问题是,您只应将边缘添加到“离开树”的列表中! because if we don't, we might end up having cycles! 因为如果不这样做,我们最终可能会陷入循环! For checking this "leaving the tree" property for each edge, we can use the set of selected vertices, and we can check each edge before adding, that it doesn't have both ends in the set, so simply when you are adding the edges of a newly added vertex to set of edges leaving the tree, first check if the vertex on the other end of the edge is in the set of selected vertices of tree, and add the edge to the list only if the other edge wasn't in the set. 为了检查每个边缘的“离开树”属性,我们可以使用一组选定的顶点,并且可以在添加之前检查每个边缘,因为它在集合中没有两端,所以简单地在添加将新添加的顶点的边缘添加到离开树的一组边上,首先检查该边的另一端的顶点是否在树的选定顶点集中,然后仅当另一边不是该边时才将其添加到列表中t在集合中。 You can check existence of an element in a set in O(1) time and this way you won't end up with cycles! 您可以在O(1)时间内检查集合中某个元素的存在,这样就不会导致循环!

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

相关问题 用两个DFS运行在O(V + E)中找到MST? - Finding MST in O(V+E) with two DFS runs? 图上 DFS 的时间复杂度 (O(V+E)) 与矩阵上的 DFS (3^(M*N)) - Time complexity of DFS on graph (O(V+E)) vs DFS on matrix (3^(M*N)) 在以下代码中,图上深度优先搜索的时间复杂度如何变为 O(V+E)? - How does time complexity for depth first search on a graph come out to be O(V+E) in the following code? 为什么只有我认为 Leetcode "133. Clone Graph" 的时间复杂度是 O(E) 而不是 O(V+E) - Why only I think the Time Complexity of Leetcode "133. Clone Graph" is O(E) instead of O(V+E) 为什么要进行拓扑排序以找到最短路径O(V + E) - Why is Topological Sorting to find the shortest path O(V+E) 使用 BFS 找到包含 O(V+E) 原点的最小循环 - Using BFS to find smallest cycle that includes origin in O(V+E) O((V + E)* W)中的Dijsktra算法 - Dijsktra's algorithm in O((V+E)*W) O(V + E)空间复杂度是什么意思? - What is the meaning of O(V+E) space complexity? Dijkstra算法在O((V + E)log W)时间内计算给定源顶点的最短路径 - Dijkstra’s algorithm to compute the shortest paths from a given source vertex s in O ((V+E) log W) time 为什么BFS O(V + E)的复杂性代替O(V * E)? - Why is the complexity of BFS O(V+E) instead of O(V*E)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM