Number of ways to reach at starting node after travelling through exactly K edges in a complete graph
Given a complete graph having N nodes and N*(N-1)/2 edges and a positive integer K, the task is to find the number of ways if you are starting at node 1 and end at the same node if exactly K edges have to be traversed.
Input: N = 4, K = 3
Output: 6
Explanation: The possible ways are-
1?2?3?1
1?2?4?1
1?3?2?1
1?3?4?1
1?4?2?1
1?4?3?1
Input: N = 5, K = 3
Output: 12
Naive Approach: The simplest approach to solve this problem is to traverse from every edge from the current vertex it will lead the solution to be exponential time.
Time Complexity: O(N^N)
Auxiliary Space: O(N), due to stack space in the recursive function call.
Efficient Approach: This problem has Overlapping Subproblems property and Optimal Substructure property. So this problem can be solved using Dynamic Programming. Like other typical Dynamic Programming(DP) problems, recomputation of the same subproblems can be avoided by constructing a temporary array that stores results of subproblems. Follow the steps below to solve the problem:
- Initialize a dp[] array where dp[i] to stores the number of ways to reach at ith node and initialize dp[0] as 1.
- Iterate in the range [1, K] using the variable i and perform the following steps:
- Initialize a variable numWays as 0 to store the number of ways to reach all the nodes.
- Iterate in the range [0, N-1] using the variable j, then add dp[j] in numWays.
- Iterate in the range [0, N-1] using the variable j, then update the value of dp[j] as maximum of dp[j] and numWays – dp[j].
- After performing the above steps, print dp[0] as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numberOfWays( int n, int k)
{
int dp[1000];
for ( int i = 0; i < n; i++) {
dp[i] = 0;
}
dp[0] = 1;
for ( int i = 1; i <= k; i++) {
int numWays = 0;
for ( int j = 0; j < n; j++) {
numWays += dp[j];
}
for ( int j = 0; j < n; j++) {
dp[j] = numWays - dp[j];
}
}
cout << dp[0] << endl;
}
int main()
{
int N = 5, K = 3;
numberOfWays(N, K);
return 0;
}
|
Java
class GFG{
static void numberOfWays( int n, int k)
{
int [] dp = new int [ 1000 ];
for ( int i = 0 ; i < n; i++)
{
dp[i] = 0 ;
}
dp[ 0 ] = 1 ;
for ( int i = 1 ; i <= k; i++)
{
int numWays = 0 ;
for ( int j = 0 ; j < n; j++)
{
numWays += dp[j];
}
for ( int j = 0 ; j < n; j++)
{
dp[j] = numWays - dp[j];
}
}
System.out.println(dp[ 0 ] + "\n" );
}
public static void main(String args[])
{
int N = 5 , K = 3 ;
numberOfWays(N, K);
}
}
|
Python3
def numberOfWays(n, k):
dp = [ 0 for i in range ( 1000 )]
dp[ 0 ] = 1
for i in range ( 1 , k + 1 , 1 ):
numWays = 0
for j in range (n):
numWays + = dp[j]
for j in range (n):
dp[j] = numWays - dp[j]
print (dp[ 0 ])
if __name__ = = '__main__' :
N = 5
K = 3
numberOfWays(N, K)
|
C#
using System;
class GFG{
static void numberOfWays( int n, int k)
{
int [] dp = new int [1000];
for ( int i = 0; i < n; i++)
{
dp[i] = 0;
}
dp[0] = 1;
for ( int i = 1; i <= k; i++)
{
int numWays = 0;
for ( int j = 0; j < n; j++)
{
numWays += dp[j];
}
for ( int j = 0; j < n; j++)
{
dp[j] = numWays - dp[j];
}
}
Console.Write(dp[0]);
}
static public void Main ()
{
int N = 5, K = 3;
numberOfWays(N, K);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N×K)
Auxiliary Space: O(N)
Last Updated :
23 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...