Jump Pointer Algorithm | Python3 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 ; 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
import math NEW_LINE def getLen ( n ) : NEW_LINE INDENT len = int ( ( math . log ( n ) ) // ( math . log ( 2 ) ) ) + 1 NEW_LINE return len NEW_LINE DEDENT def set_jump_pointer ( len , n ) : NEW_LINE INDENT global jump , node NEW_LINE for j in range ( 1 , len + 1 ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT jump [ node [ i ] ] [ j ] = jump [ jump [ node [ i ] ] [ j - 1 ] ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT def constructGraph ( c , p , i ) : NEW_LINE INDENT global jump , node , isNode NEW_LINE node [ i ] = c NEW_LINE if ( isNode == 0 ) : NEW_LINE INDENT isNode = 1 NEW_LINE jump [ 0 ] = p NEW_LINE DEDENT return NEW_LINE DEDENT def jumpPointer ( x , L ) : NEW_LINE INDENT j = 0 NEW_LINE n = x NEW_LINE k = L NEW_LINE global jump , isNode NEW_LINE if ( isNode [ x ] == 0 ) : NEW_LINE INDENT print ( " Node ▁ is ▁ not ▁ present ▁ in ▁ graph ▁ " ) NEW_LINE return NEW_LINE DEDENT while ( L > 0 ) : NEW_LINE INDENT if ( ( L & 1 ) != 0 ) : NEW_LINE INDENT x = jump [ x ] [ j ] NEW_LINE DEDENT L = L >> 1 NEW_LINE j += 1 NEW_LINE DEDENT print ( str ( k ) + " th ▁ parent ▁ of ▁ node ▁ " + str ( n ) + " ▁ is ▁ = ▁ " + str ( x ) ) NEW_LINE return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 11 NEW_LINE DEDENT jump = [ [ 0 for j in range ( 10 ) ] for i in range ( 1000 ) ] NEW_LINE node = [ 0 for i in range ( 1000 ) ] NEW_LINE isNode = [ 0 for i in range ( 1000 ) ] NEW_LINE INDENT len = getLen ( n ) NEW_LINE R = 2 NEW_LINE constructGraph ( 2 , 0 , 0 ) NEW_LINE constructGraph ( 5 , 2 , 1 ) NEW_LINE constructGraph ( 3 , 5 , 2 ) NEW_LINE constructGraph ( 4 , 5 , 3 ) NEW_LINE constructGraph ( 1 , 5 , 4 ) NEW_LINE constructGraph ( 7 , 1 , 5 ) NEW_LINE constructGraph ( 9 , 1 , 6 ) NEW_LINE constructGraph ( 10 , 9 , 7 ) NEW_LINE constructGraph ( 11 , 10 , 8 ) NEW_LINE constructGraph ( 6 , 10 , 9 ) NEW_LINE constructGraph ( 8 , 10 , 10 ) NEW_LINE set_jump_pointer ( len , n ) NEW_LINE jumpPointer ( 2 , 0 ) NEW_LINE jumpPointer ( 4 , 2 ) NEW_LINE jumpPointer ( 8 , 3 ) NEW_LINE jumpPointer ( 20 , 5 ) NEW_LINE DEDENT