Maximum weighted edge in path between two nodes in an N | Javascript implementation to find the maximum weighted edge in the simple path between two nodes in N - ary Tree ; Depths of Nodes ; Parent at every 2 ^ i level ; Maximum node at every 2 ^ i level ; Graph that stores destinations and its weight ; Function to traverse the nodes using the Depth - First Search Traversal ; Condition to check if its equal to its parent then skip ; DFS Recursive Call ; Function to find the ansector ; Loop to set every 2 ^ i distance ; Loop to calculate for each node in the N - ary tree ; Storing maximum edge ; Swaping if node a is at more depth than node b because we will always take at more depth ; Difference between the depth of the two given nodes ; Changing Node B to its parent at 2 ^ i distance ; Subtracting distance by 2 ^ i ; Take both a , b to its lca and find maximum ; Loop to find the maximum 2 ^ ith parent the is different for both a and b ; Updating ans ; Changing value to its parent ; Function to compute the Least common Ansector ; Undirected tree ; Computing LCA
let N = 100005 ; let level = new Array ( N ) ; level . fill ( 0 ) ; let LG = 20 ; let dp = new Array ( LG ) ; for ( let i = 0 ; i < LG ; i ++ ) { dp [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let mx = new Array ( LG ) ; for ( let i = 0 ; i < LG ; i ++ ) { mx [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { mx [ i ] [ j ] = 0 ; } } let v = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { v . push ( [ ] ) ; } let n = 0 ; function dfs_lca ( a , par , lev ) { dp [ 0 ] [ a ] = par ; level [ a ] = lev ; for ( let i = 0 ; i < 2 ; i ++ ) { if ( v [ a ] [ 0 ] == par ) continue ; mx [ 0 ] [ v [ a ] [ 0 ] ] = v [ a ] [ 1 ] ; dfs_lca ( v [ a ] [ 0 ] , a , lev + 1 ) ; } } function find_ancestor ( ) { for ( let i = 1 ; i < 16 ; i ++ ) { for ( let j = 1 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ; mx [ i ] [ j ] = Math . max ( mx [ i - 1 ] [ j ] , mx [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ) ; } } } function getMax ( a , b ) { if ( level [ b ] < level [ a ] ) { let temp = a ; a = b ; b = temp ; } let ans = 0 ; let diff = level [ b ] - level [ a ] ; while ( diff > 0 ) { let log = parseInt ( Math . log ( diff ) / Math . log ( 2 ) , 10 ) ; ans = Math . max ( ans , mx [ log ] [ b ] ) ; b = dp [ log ] [ b ] ; diff -= ( 1 << log ) ; } while ( a == b ) { i = parseInt ( Math . log ( level [ a ] ) / Math . log ( 2 ) , 10 ) ; while ( i > 0 && dp [ i ] [ a ] == dp [ i ] [ b ] ) { i -= 1 ; } ans = Math . max ( ans , mx [ i ] [ a ] ) ; ans = Math . max ( ans , mx [ i ] [ b ] ) ; a = dp [ i ] [ a ] ; b = dp [ i ] [ b ] ; } return ans * 2 + 1 ; } function compute_lca ( ) { dfs_lca ( 1 , 0 , 0 ) ; find_ancestor ( ) ; } n = 5 ; v [ 1 ] . push ( 2 ) ; v [ 1 ] . push ( 2 ) ; v [ 2 ] . push ( 1 ) ; v [ 2 ] . push ( 2 ) ; v [ 1 ] . push ( 3 ) ; v [ 1 ] . push ( 5 ) ; v [ 3 ] . push ( 1 ) ; v [ 3 ] . push ( 5 ) ; v [ 3 ] . push ( 4 ) ; v [ 3 ] . push ( 3 ) ; v [ 4 ] . push ( 3 ) ; v [ 4 ] . push ( 4 ) ; v [ 3 ] . push ( 5 ) ; v [ 3 ] . push ( 1 ) ; v [ 5 ] . push ( 3 ) ; v [ 5 ] . push ( 1 ) ; compute_lca ( ) ; let queries = [ [ 3 , 5 ] , [ 2 , 3 ] , [ 2 , 4 ] ] ; let q = 3 ; for ( let i = 0 ; i < q ; i ++ ) { let max_edge = getMax ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ; document . write ( max_edge + " " ) ; }