int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); }
This is a slice of code of disjoin set algorithm can i ask what is the meaning and purpose of return parent[v] = find_set(parent[v]);? usually the return are integers,boolean,or other data types.
That line of code (
parent[v] = find_set(parent[v]);
) is an heuristic optimization called path compression, a simple and highly effective heuristic. Path compression makes that each node on thefind_set
path points directly to the root. Further calls to find_set on any node in the path before will be very fast. The time complexity of this heuristic is shown below.As stated in the book Introduction to Algorithms (Cormen, page 571-572), 3rd Edition:
You can learn more about it by reading that book.