In previous posts, we have discussed how to calculate the Lowest Common Ancestor (LCA) for a binary tree and a binary search tree (this, this and this). Now let’s look at a method that can calculate LCA for any tree (not only for binary tree). We use Dynamic Programming with Sparse Matrix Approach in our method. This method is very handy and fast when you need to answer multiple queries of LCA for a tree.
Pre-requisites : –
2) Basic DP knowledge (This and this)
3) Range Minimum Query (Square Root Decomposition and Sparse Table)
The naive approach for this general tree LCA calculation will be the same as the naive approach for the LCA calculation of Binary Tree (this naive approach is already well described here.
The C++ implementation for the naive approach is given below :-
LCA(4, 7) = 1 LCA(4, 6) = 2
Pre-computation :- Here we store the 2^i th parent for every node, where 0 <= i < LEVEL, here “LEVEL” is a constant integer that tells the maximum number of 2^i th ancestor possible.
Therefore, we assume the worst case to see what is the value of the constant LEVEL. In our worst case every node in our tree will have at max 1 parent and 1 child or we can say it simply reduces to a linked list.
So, in this case
LEVEL = ceil ( log(number of nodes) ).
We also pre-compute the height for each node using one dfs in O(n) time.
int n // number of nodes int parent[MAXN][LEVEL] // all initialized to -1 parent[node] : contains the 2^0th(first) parent of all the nodes pre-computed using DFS // Sparse matrix Approach for node -> 1 to n : for i-> 1 to LEVEL : if ( parent[node][i-1] != -1 ) : parent[node][i] = parent[ parent[node][i-1] ][i-1]
Now , as we see the above dynamic programming code runs two nested loop that runs over their complete range respectively.
Hence, it can be easily be inferred that its asymptotic Time Complexity is O(number of nodes * LEVEL) ~ O(n*LEVEL) ~ O(nlogn).
Return LCA(u,v) :-
1) First Step is to bring both the nodes at the same height. As we have already pre-computed the heights for each node. We first calculate the difference in the heights of u and v (let’s say v >=u). Now we need the node ‘v’ to jump h nodes above. This can be easily done in O(log h) time ( where h is the difference in the heights of u and v) as we have already stored the 2^i parent for each node. This process is exactly same as calculating x^y in O(log y) time. (See the code for better understanding).
2) Now both u and v nodes are at same height. Therefore now once again we will use 2^i jumping strategy to reach the first Common Parent of u and v.
Pseudo-code: For i-> LEVEL to 0 : If parent[u][i] != parent[v][i] : u = parent[u][i] v = parent[v][i]
C++ implementation of the above algorithm is given below:
LCA(4,7) = 1 LCA(4,6) = 2
Time Complexity: The time complexity for answering a single LCA query will be O(logn) but the overall time complexity is dominated by precalculation of the 2^i th ( 0<=i<=level ) ancestors for each node. Hence, the overall asymptotic Time Complexity will be O(n*logn) and Space Complexity will be O(nlogn), for storing the data about the ancestors of each node.
This article is contributed by Nitish Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.