简体   繁体   中英

Finding MST in O(V+E) with two DFS runs?

Given undirecred connected graph with edges of costs either x or y (where x is less than y and both are positive integers) find MST in O(V+E)

The idea involves using two DFS runs and collapsing nodes of lower weight into supernode (after first DFS run), but I'm not entirely certain. Any help is appreciated. I have seen such solution hinted in several answers, but couldn't find an explanation of it anywhere.

I think that your intuition is correct that an MST of an undirected connected graph can be found with a running time of O(V+E). There is an algorithm called Kruskal's which can compute the MST of an undirected graph in O(V+Eα(V)), α(V) is the inverse of Ackermann's function which is very slow growing. The way that Kruskal's algorithm reaches O(V+Eα(V)) is by using the union-find data structure. Union-find is a data structure that tracks elements that have been partitioned into disjoint subsets. When an element is searched (find(x)) in this data structure the tree is compressed so that each node's pointer between the root and X is switched from its parent to the root of the tree. The union(x,y) function uses find to determine if the nodes belong to the same subset compressing the trees in the process if they are separate trees then they are combined. The tree with the lower rank (height of the tree) is moved to point to the root of the larger rank tree. Kruskal's uses the union-find data structure to check if vertices are connected yet. In general Kruskal's works by adding all the vertices into the union-find data structure then continuously adding the lowest edge assuming they are sorted in increasing order. When adding the lowest edge check if the vertices are connected, if not add that edge and perform a union between the two vertices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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