Weighted Quick Union can be improved further by path compression. The idea here is to avoid flatten the tree. After computing the root of a node,
we set the id of each examined node to point to that root.
Running Time:
For M union-find operations on N elements, running time ≤ c ( N + M lg* N ) array accesses.
Running Time:
For M union-find operations on N elements, running time ≤ c ( N + M lg* N ) array accesses.
lg*N = number of times we have to take log of N to get 1.
It's almost linear and union find doesn't have any linear algorithm.
Following is the program:
Following is the program:
No comments:
Post a Comment