# Level Ancestor Problem

The level ancestor problem is the problem of preprocessing a given rooted tree T into a data structure that can determine the ancestor of a given node at a given depth from the root of the tree. Here **depth** of any node in a tree is the number of edges on the shortest path from the root of the tree to the node.

Given tree is represented as **un-directed connected graph** having n nodes and **n-1 edges**.

The idea to solve the above query is to use **Jump Pointer Algorithm** and pre-processes the tree in **O( n log n )** time and answer level ancestor queries in **O( logn )** time. In jump pointer, there is a pointer from node N to Nâ€™s j-th ancestor, for

j = 1, 2, 4, 8, …, and so on. We refer to these pointers as Jump_{N}[i], where

Jump_{u}[i] = LA(N, depth(N) – 2^{i}).

When the algorithm is asked to process a query, we repeatedly jump up the tree using these jump pointers. The number of jumps will be at most log n and therefore queries can be answered in O( logn ) time.

So we store **2 ^{i}th ancestor** of each node and also find the depth of each node from the root of the tree.

Now our task reduces to find the ( depth(N) – 2

^{i})th ancestor of node N. Let’s denote X as ( depth(N) – 2

^{i}) and let

**b**bits of the X are

**set bits**(1) denoted by s1, s2, s3, …sb.

**X = 2**

^{(s1)}+ 2^{(s2)}+ … + 2^{(sb)}Now the problem is how to find 2

^{j}ancestors of each node and depth of each node from the root of the tree?

Initially, we know the 2

^{0}th ancestor of each node is its parent. We can recursively compute 2

^{j}-th ancestor. We know 2

^{j}-th ancestor is 2

^{j-1}-th ancestor of 2

^{j-1}-th ancestor. To calculate the depth of each node we use the ancestor matrix. If we found the root node present in the array of the kth element at jth index then the depth of that node is simply 2

^{j}but if root node doesn’t present in the array of ancestors of the kth element than the depth of kth element is 2

^{( index of last non zero ancestor at kth row )}+ depth of ancestor present at last index of kth row.

Below is the algorithm to fill the ancestor matrix and depth of each node using dynamic programming. Here, we denote root node as

**R**and initially assume the ancestor of root node as

**0**. We also initialize depth array with

**-1**means the depth of the current node is not set and we need to find its depth. If the depth of the current node is not equal to -1 means we have already computed its depth.

we know the first ancestor of each node so we take j>=1, For j>=1 ancstr[k][j] = 2jth ancestor of k = 2j-1th ancestor of (2j-1th ancestor of k) = ancstr[ancstr[i][j-1][j-1] if ancstr[k][j] == R && depth[k] == -1 depth[k] = 2^{j}else if ancstr[k][j] == -1 && depth[k] == -1 depth[k] = 2^{(j-1)}+ depth[ ancstr[k][j-1] ]

Let’s understand this algorithm with below diagram.

In the given figure we need to compute 1st level ancestor of the node with value **8**. First, we make ancestor matrix which stores 2^{ith} ancestor of nodes. Now, **2 ^{0}** ancestor of node

**8**is

**10**and similarly

**2**ancestor of node

^{0}**10**is

**9**and for node

**9**it is

**1**and for node

**1**it is

**5**. Based on the above algorithm 1st level ancestor of node 8 is

**( depth(8)-1 )th ancestor**of node 8. We have pre computed depth of each node and depth of 8 is 5 so we finally need to find

**(5-1) = 4th**ancestor of node 8 which is equal to

**2**and

^{1}th ancestor of [2^{1}ancestor of node 8]**2**is

^{1}th ancestor of node 8**2**So,

^{0}th ancestor of [2^{0}th ancestor of node 8].**2**is node with value

^{0}th ancestor of [2^{0}th ancestor of node 8]**9**and

**2**of node 9 is node with value

^{1}th ancestor**5**. Thus in this way we can compute all query in O(logn) time complexity.

## CPP

`// CPP program to implement Level Ancestor Algorithm` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `R = 0;` `// n -> it represent total number of nodes` `// len -> it is the maximum length of array to hold` `// ancestor of each node. In worst case,` `// the highest value of ancestor a node can have is n-1.` `// 2 ^ len <= n-1` `// len = O(log2n)` `int` `getLen(` `int` `n)` `{` ` ` `return` `(` `int` `)(` `log` `(n) / ` `log` `(2)) + 1;` `}` `// ancstr represents 2D matrix to hold ancestor of node.` `// Here we pass reference of 2D matrix so that the change` `// made occur directly to the original matrix` `// depth[] stores depth of each node` `// len is same as defined above` `// n is total nodes in graph` `// R represent root node` `void` `setancestor(vector<vector<` `int` `> >& ancstr,` ` ` `vector<` `int` `>& depth, ` `int` `* node, ` `int` `len, ` `int` `n)` `{` ` ` `// depth of root node is set to 0` ` ` `depth[R] = 0;` ` ` `// if depth of a node is -1 it means its depth` ` ` `// is not set otherwise we have computed its depth` ` ` `for` `(` `int` `j = 1; j <= len; j++) {` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `ancstr[node[i]][j] = ancstr[ancstr[node[i]][j - 1]][j - 1];` ` ` `// if ancestor of current node is R its height is` ` ` `// previously not set, then its height is 2^j` ` ` `if` `(ancstr[node[i]][j] == R && depth[node[i]] == -1) {` ` ` `// set the depth of ith node` ` ` `depth[node[i]] = ` `pow` `(2, j);` ` ` `}` ` ` `// if ancestor of current node is 0 means it` ` ` `// does not have root node at its 2th power` ` ` `// on its path so its depth is 2^(index of` ` ` `// last non zero ancestor means j-1) + depth` ` ` `// of 2^(j-1) th ancestor` ` ` `else` `if` `(ancstr[node[i]][j] == 0 &&` ` ` `node[i] != R && depth[node[i]] == -1) {` ` ` `depth[node[i]] = ` `pow` `(2, j - 1) +` ` ` `depth[ancstr[node[i]][j - 1]];` ` ` `}` ` ` `}` ` ` `}` `}` `// c -> it represent child` `// p -> it represent ancestor` `// i -> it represent node number` `// p=0 means the node is root node` `// R represent root node` `// here also we pass reference of 2D matrix and depth` `// vector so that the change made occur directly to` `// the original matrix and original vector` `void` `constructGraph(vector<vector<` `int` `> >& ancstr,` ` ` `int` `* node, vector<` `int` `>& depth, ` `int` `* isNode,` ` ` `int` `c, ` `int` `p, ` `int` `i)` `{` ` ` `// enter the node in node array` ` ` `// it stores all the nodes in the graph` ` ` `node[i] = c;` ` ` `// to confirm that no child node have 2 ancestors` ` ` `if` `(isNode == 0) {` ` ` `isNode = 1;` ` ` `// make ancestor of x as y` ` ` `ancstr[0] = p;` ` ` `// ifits first ancestor is root than its depth is 1` ` ` `if` `(R == p) {` ` ` `depth = 1;` ` ` `}` ` ` `}` ` ` `return` `;` `}` `// this function will delete leaf node` `// x is node to be deleted` `void` `removeNode(vector<vector<` `int` `> >& ancstr,` ` ` `int` `* isNode, ` `int` `len, ` `int` `x)` `{` ` ` `if` `(isNode[x] == 0)` ` ` `cout << ` `"node does not present in graph "` `<< endl;` ` ` `else` `{` ` ` `isNode[x] = 0;` ` ` `// make all ancestor of node x as 0` ` ` `for` `(` `int` `j = 0; j <= len; j++) {` ` ` `ancstr[x][j] = 0;` ` ` `}` ` ` `}` ` ` `return` `;` `}` `// x -> it represent new node to be inserted` `// p -> it represent ancestor of new node` `void` `addNode(vector<vector<` `int` `> >& ancstr,` ` ` `vector<` `int` `>& depth, ` `int` `* isNode, ` `int` `len,` ` ` `int` `x, ` `int` `p)` `{` ` ` `if` `(isNode[x] == 1) {` ` ` `cout << ` `" Node is already present in array "` `<< endl;` ` ` `return` `;` ` ` `}` ` ` `if` `(isNode[p] == 0) {` ` ` `cout << ` `" ancestor not does not present in an array "` `<< endl;` ` ` `return` `;` ` ` `}` ` ` `isNode[x] = 1;` ` ` `ancstr[x][0] = p;` ` ` `// depth of new node is 1 + depth of its ancestor` ` ` `depth[x] = depth[p] + 1;` ` ` `int` `j = 0;` ` ` `// while we don't reach root node` ` ` `while` `(ancstr[x][j] != 0) {` ` ` `ancstr[x][j + 1] = ancstr[ancstr[x][j]][j];` ` ` `j++;` ` ` `}` ` ` `// remaining array will fill with 0 after` ` ` `// we find root of tree` ` ` `while` `(j <= len) {` ` ` `ancstr[x][j] = 0;` ` ` `j++;` ` ` `}` ` ` `return` `;` `}` `// LA function to find Lth level ancestor of node x` `void` `LA(vector<vector<` `int` `> >& ancstr, vector<` `int` `> depth,` ` ` `int` `* isNode, ` `int` `x, ` `int` `L)` `{` ` ` `int` `j = 0;` ` ` `int` `temp = x;` ` ` `// to check if node is present in graph or not` ` ` `if` `(isNode[x] == 0) {` ` ` `cout << ` `"Node is not present in graph "` `<< endl;` ` ` `return` `;` ` ` `}` ` ` `// we change L as depth of node x -` ` ` `int` `k = depth[x] - L;` ` ` `// int q = k;` ` ` `// in this loop we decrease the value of k by k/2 and` ` ` `// increment j by 1 after each iteration, and check for set bit` ` ` `// if we get set bit then we update x with jth ancestor of x` ` ` `// as k becomes less than or equal to zero means we` ` ` `// reach to kth level ancestor` ` ` `while` `(k > 0) {` ` ` `// to check if last bit is 1 or not` ` ` `if` `(k & 1) {` ` ` `x = ancstr[x][j];` ` ` `}` ` ` `// use of shift operator to make k = k/2` ` ` `// after every iteration` ` ` `k = k >> 1;` ` ` `j++;` ` ` `}` ` ` `cout << L << ` `"th level ancestor of node "` ` ` `<< temp << ` `" is = "` `<< x << endl;` ` ` `return` `;` `}` `int` `main()` `{` ` ` `// n represent number of nodes` ` ` `int` `n = 12;` ` ` `// initialization of ancestor matrix` ` ` `// suppose max range of node is up to 1000` ` ` `// if there are 1000 nodes than also length` ` ` `// of ancestor matrix will not exceed 10` ` ` `vector<vector<` `int` `> > ancestor(1000, vector<` `int` `>(10));` ` ` `// this vector is used to store depth of each node.` ` ` `vector<` `int` `> depth(1000);` ` ` `// fill function is used to initialize depth with -1` ` ` `fill(depth.begin(), depth.end(), -1);` ` ` `// node array is used to store all nodes` ` ` `int` `* node = ` `new` `int` `[1000];` ` ` `// isNode is an array to check whether a` ` ` `// node is present in graph or not` ` ` `int` `* isNode = ` `new` `int` `[1000];` ` ` `// memset function to initialize isNode array with 0` ` ` `memset` `(isNode, 0, 1000 * ` `sizeof` `(` `int` `));` ` ` `// function to calculate len` ` ` `// len -> it is the maximum length of array to` ` ` `// hold ancestor of each node.` ` ` `int` `len = getLen(n);` ` ` `// R stores root node` ` ` `R = 2;` ` ` `// construction of graph` ` ` `// here 0 represent that the node is root node` ` ` `constructGraph(ancestor, node, depth, isNode, 2, 0, 0);` ` ` `constructGraph(ancestor, node, depth, isNode, 5, 2, 1);` ` ` `constructGraph(ancestor, node, depth, isNode, 3, 5, 2);` ` ` `constructGraph(ancestor, node, depth, isNode, 4, 5, 3);` ` ` `constructGraph(ancestor, node, depth, isNode, 1, 5, 4);` ` ` `constructGraph(ancestor, node, depth, isNode, 7, 1, 5);` ` ` `constructGraph(ancestor, node, depth, isNode, 9, 1, 6);` ` ` `constructGraph(ancestor, node, depth, isNode, 10, 9, 7);` ` ` `constructGraph(ancestor, node, depth, isNode, 11, 10, 8);` ` ` `constructGraph(ancestor, node, depth, isNode, 6, 10, 9);` ` ` `constructGraph(ancestor, node, depth, isNode, 8, 10, 10);` ` ` `// function to pre compute ancestor matrix` ` ` `setancestor(ancestor, depth, node, len, n);` ` ` `// query to get 1st level ancestor of node 8` ` ` `LA(ancestor, depth, isNode, 8, 1);` ` ` `// add node 12 and its ancestor is 8` ` ` `addNode(ancestor, depth, isNode, len, 12, 8);` ` ` `// query to get 2nd level ancestor of node 12` ` ` `LA(ancestor, depth, isNode, 12, 2);` ` ` `// delete node 12` ` ` `removeNode(ancestor, isNode, len, 12);` ` ` `// query to get 5th level ancestor of node` ` ` `// 12 after deletion of node` ` ` `LA(ancestor, depth, isNode, 12, 1);` ` ` `return` `0;` `}` |

**Output**

1th level ancestor of node 8 is = 5 2th level ancestor of node 12 is = 1 Node is not present in graph