How can I check in linear time O(n) if the given tree is a MST?
Asked
Active
Viewed 1,297 times
1 Answers
-1
Are you familiar with the process Union-Find?
If you are just given a tree you will just need to check whether its connected graph or not. I mean just query if it has only one component.
Else maintain a map[ pair<int,int>
, int] or similar hashing library to store the minimum weight between two nodes and compare the given tree has each edge with minimum wights or not.
If not then you are sure its not MST, else you have look up for if its connected. If yes, then its MST.
Use tree shorting in Union Find. And to query if the tree is connected or not, you can easily check for each edge before union, if its already unioned cause then you are sure there is a cycle and its not tree at all, so not MST.

iMagur
- 721
- 1
- 6
- 11
-
Consider the graph K3, with equal-weight edges. One of the edges will not be in the MST, even if it has the minimum weight between two nodes (indeed, it is the only weight between those nodes). – VF1 May 14 '14 at 01:20