Jump Pointer Algorithm | C ++ program to implement Jump pointer algorithm ; n -> it represent total number of nodes len -> it is the maximum length of array to hold parent of each node . In worst case , the highest value of parent a node can have is n - 1. 2 ^ len <= n - 1 len = O ( log2n ) ; jump represent 2D matrix to hold parent of node in jump matrix here we pass reference of 2D matrix so that the change made occur directly to the original matrix len is same as defined above n is total nodes in graph ; c -> it represent child p -> it represent parent i -> it represent node number p = 0 means the node is 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 ; enter the node in node array it stores all the nodes in the graph ; to confirm that no child node have 2 parents ; make parent of x as y ; function to jump to Lth parent of any node ; to check if node is present in graph or not ; in this loop we decrease the value of L by L / 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 parent of x as L becomes less than or equal to zero means we have jumped to Lth parent of node x ; to check if last bit is 1 or not ; use of shift operator to make L = L / 2 after every iteration ; Driver code ; n represent number of nodes ; initialization of parent matrix suppose max range of a node is up to 1000 if there are 1000 nodes than also length of jump matrix will not exceed 10 ; node array is used to store all nodes ; isNode is an array to check whether a node is present in graph or not ; memset function to initialize isNode array with 0 ; function to calculate len len -> it is the maximum length of array to hold parent of each node . ; R stores root node ; construction of graph here 0 represent that the node is root node ; function to pre compute jump matrix ; query to jump to parent using jump pointers query to jump to 1 st parent of node 2 ; query to jump to 2 nd parent of node 4 ; query to jump to 3 rd parent of node 8 ; query to jump to 5 th parent of node 20
#include <bits/stdc++.h> NEW_LINE using namespace std ; int R = 0 ; int getLen ( int n ) { int len = ( int ) ( log ( n ) / log ( 2 ) ) + 1 ; return len ; } void set_jump_pointer ( vector < vector < int > > & jump , int * node , int len , int n ) { for ( int j = 1 ; j <= len ; j ++ ) for ( int i = 0 ; i < n ; i ++ ) jump [ node [ i ] ] [ j ] = jump [ jump [ node [ i ] ] [ j - 1 ] ] [ j - 1 ] ; } void constructGraph ( vector < vector < int > > & jump , int * node , int * isNode , int c , int p , int i ) { node [ i ] = c ; if ( isNode == 0 ) { isNode = 1 ; jump [ 0 ] = p ; } return ; } void jumpPointer ( vector < vector < int > > & jump , int * isNode , int x , int L ) { int j = 0 , n = x , k = L ; if ( isNode [ x ] == 0 ) { cout << " Node ▁ is ▁ not ▁ present ▁ in ▁ graph ▁ " << endl ; return ; } while ( L > 0 ) { if ( L & 1 ) x = jump [ x ] [ j ] ; L = L >> 1 ; j ++ ; } cout << k << " th ▁ parent ▁ of ▁ node ▁ " << n << " ▁ is ▁ = ▁ " << x << endl ; return ; } int main ( ) { int n = 11 ; vector < vector < int > > jump ( 1000 , vector < int > ( 10 ) ) ; int * node = new int [ 1000 ] ; int * isNode = new int [ 1000 ] ; memset ( isNode , 0 , 1000 * sizeof ( int ) ) ; int len = getLen ( n ) ; R = 2 ; constructGraph ( jump , node , isNode , 2 , 0 , 0 ) ; constructGraph ( jump , node , isNode , 5 , 2 , 1 ) ; constructGraph ( jump , node , isNode , 3 , 5 , 2 ) ; constructGraph ( jump , node , isNode , 4 , 5 , 3 ) ; constructGraph ( jump , node , isNode , 1 , 5 , 4 ) ; constructGraph ( jump , node , isNode , 7 , 1 , 5 ) ; constructGraph ( jump , node , isNode , 9 , 1 , 6 ) ; constructGraph ( jump , node , isNode , 10 , 9 , 7 ) ; constructGraph ( jump , node , isNode , 11 , 10 , 8 ) ; constructGraph ( jump , node , isNode , 6 , 10 , 9 ) ; constructGraph ( jump , node , isNode , 8 , 10 , 10 ) ; set_jump_pointer ( jump , node , len , n ) ; jumpPointer ( jump , isNode , 2 , 0 ) ; jumpPointer ( jump , isNode , 4 , 2 ) ; jumpPointer ( jump , isNode , 8 , 3 ) ; jumpPointer ( jump , isNode , 20 , 5 ) ; return 0 ; }