![]() |
VOOZH | about |
Prerequisite: Introduction and DFS
The task is to find LCA of two given nodes in a tree (not necessarily a Binary Tree). In previous posts, we have seen how to calculate LCA using the Sparse Matrix DP approach. In this post, we will see an optimization done on the Naive method by sqrt decomposition technique that works well over the Naive Approach.
Naive Approach
To calculate the LCA of two nodes first we will bring both the nodes to the same height by making the node with greater depth jump one parent up the tree till both the nodes are at the same height. Once, both the nodes are at the same height we can then start jumping one parent up for both the nodes simultaneously till both the nodes become equal and that node will be the LCA of the two originally given nodes.
Consider the below n-ary Tree with depth 9 and let's examine how the naive approach works for this sample tree.
Here in the above Tree we need to calculate the LCA of node 6 and node 30
Clearly, node 30 has greater depth than node 6. So first of all we start jumping one parent above for node 30 till we reach the depth value of node 6 i.e at depth 2.
The orange colored path in the above figure demonstrates the jumping sequence to reach depth 2. In this procedure, we just simply jump one parent above the current node.
Now both nodes are at the same depth 2.Therefore, now both the nodes will jump one parent up till both the nodes become equal. This end node at which both the nodes become equal for the first time is our LCA.
The blue color path in the above figure shows the jumping route for both the nodes
Implementation:
LCA(11,8) : 4 LCA(3,13) : 3
Time Complexity: We pre-calculate the depth for each node using one DFS traversal in O(n). Now in the worst case, the two nodes will be two bottom-most nodes on the tree in different child branches of the root node. Therefore, in this case, the root will be the LCA of both the nodes. Hence, both the nodes will have to jump exactly h height above, where h is the height of the tree. So, to answer each LCA query Time Complexity will be O(h).
Auxiliary Space :O(MAXN)
The Sqrt Decomposition Trick :
We categorize nodes of the tree into different groups according to their depth. Assuming the depth of the tree h is a perfect square. So once again like the general sqrt decomposition approach, we will be having sqrt(h) blocks or groups. Nodes from depth 0 to depth sqrt(h) - 1 lie in the first group; then nodes having depth sqrt(H) to 2*sqrt(h)-1 lie in second group and so on till the last node.
We keep track of the corresponding group number for every node and also the depth of every node. This can be done by one single dfs on the tree (see the code for better understanding).
Sqrt trick :-
In the naive approach, we were jumping one parent up the tree till both nodes aren't on the same depth. But here we perform the group-wise jump. To perform this group-wise jump, we need two-parameter associated with each node: 1) parent and 2) jump parent
Here parent for each node is defined as the first node above the current node that is directly connected to it, whereas jump_parent for each node is the node that is the first ancestor of the current node in the group just above the current node.
So, now we need to maintain 3 parameters for each node :
All these three parameters can be maintained in one dfs(refer to the code for better understanding)
Pseudo code for the optimization process
LCAsqrt(u, v){
// assuming v is at greater depth
while (jump_parent[u]!=jump_parent[v]){
v = jump_parent[v];
}
// now both nodes are in same group
// and have same jump_parent
return LCAnaive(u,v);
}
The key concept here is that first we bring both the nodes in same group and having same jump_parent by climbing decomposed blocks above the tree one by one and then when both the nodes are in same group and have same jump_parent we use our naive approach to find LCA of the nodes.
This optimized group jumping technique reduces the iterating space by a factor of sqrt(h) and hence reduces the Time Complexity(refer below for better time complexity analysis)
Lets decompose the above tree in sqrt(h) groups (h = 9) and calculate LCA for node 6 and 30.
👁 16492369_1290074911076976_1745511031_o
In the above decomposed tree
Jump_parent[6] = 0 parent[6] = 3 Jump_parent[5] = 0 parent[5] = 2 Jump_parent[1] = 0 parent[1] = 0 Jump_parent[11] = 6 parent[11] = 6 Jump_parent[15] = 6 parent[15] = 11 Jump_parent[21] = 6 parent[21] = 15 Jump_parent[25] = 21 parent[25] = 21 Jump_parent[26] = 21 parent[26] = 21 Jump_parent[30] = 21 parent[30] = 25
Now at this stage Jump_parent for node 30 is 21 and Jump_parent for node 5 is 0, So we will climp to jump_parent[30] i.e to node 21
Now once again Jump_parent of node 21 is not equal to Jump_parent of node 5, So once again we will climb to jump_parent[21] i.e node 6
At this stage jump_parent[6] == jump_parent[5], So now we will use our naive climbing approach and climb one parent above for both the nodes till it reach node 1 and that will be the required LCA .
Blue path in the above figure describes jumping path sequence for node 6 and node 5.
Implementation:
LCA(11,8) : 4 LCA(3,13) : 3
Note : The above code works even if height is not perfect square.
Now Lets see how the Time Complexity is changed by this simple grouping technique :
Time Complexity Analysis:
We have divided the tree into sqrt(h) groups according to their depth and each group contain nodes having max difference in their depth equal to sqrt(h). Now once again take an example of worst case, let's say the first node 'u' is in first group and the node 'v' is in sqrt(h)th group(last group). So, first we will make group jumps(single group jumps) till we reach group 1 from last group; This will take exactly sqrt(h) - 1 iterations or jumps. So, till this step the Time Complexity is O(sqrt(h)).
Now once we are in same group, we call the LCAnaive function. The Time complexity for LCA_Naive is O(sqrt(h')), where h' is the height of the tree. Now, in our case value of h' will be sqrt(h), because each group has a subtree of at max sqrt(h) height. So the complexity for this step is also O(sqrt(h)).
Hence, the total Time Complexity will be O(sqrt(h) + sqrt(h)) ~ O(sqrt(h)).