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 ; 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 ; Print dp [ 0 ] as the answer ; Driver Code ; Given Input ; Function Call
def numberOfWays ( n , k ) : NEW_LINE INDENT dp = [ 0 for i in range ( 1000 ) ] NEW_LINE dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT numWays = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT numWays += dp [ j ] NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT dp [ j ] = numWays - dp [ j ] NEW_LINE DEDENT DEDENT print ( dp [ 0 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE numberOfWays ( N , K ) NEW_LINE DEDENT