Number of ways to reach at starting node after travelling through exactly K edges in a complete graph | Function to find number of ways to reach from node 1 to 1 again , after moving exactly K edges ; Initialize a dp [ ] array , where dp [ i ] stores number of ways to reach at a i node ; Initialize the dp array with 0 ; Base Case ; Iterate for the number of edges moved ; Sum will store number of ways to reach all the nodes ; Iterate for every possible state for the current step ; Update the value of the dp array after travelling each edge ; Prlet dp [ 0 ] as the answer ; Given Input ; Function Call
function numberOfWays ( n , k ) { let dp = Array ( 1000 ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 1 ; for ( let i = 1 ; i <= k ; i ++ ) { let numWays = 0 ; for ( let j = 0 ; j < n ; j ++ ) { numWays += dp [ j ] ; } for ( let j = 0 ; j < n ; j ++ ) { dp [ j ] = numWays - dp [ j ] ; } } document . write ( dp [ 0 ] ) ; } let N = 5 ; let K = 3 ; numberOfWays ( N , K ) ;