Number of ways to reach at starting node after travelling through exactly K edges in a complete graph


Introduction

The number of ways to reach the beginning hub after traveling through precisely K edges in a total chart can be calculated utilizing different approaches within the C dialect. One approach is to utilize brute constrain recursion, where we investigate all conceivable ways. Another approach includes energetic programming, where we store and reuse halfway comes about to dodge excess computations. Moreover, a numerical equation exists to specifically compute the number of ways based on the number of hubs and edges. These strategies give effective arrangements to decide the check of ways driving back to the beginning hub in a total chart with precisely K edges.

Approach 1: Brute Force Recursion

Algorithm

  • Step 1 − Define a word count Paths that takes the current node, remaining edges (K), and the full number of nodes within the graph.

  • Step 2 − In case K is and the current node is the beginning node, return 1 (as we have come to the starting node).

  • Step 3 − If K is but the current node isn't the beginning node, return (as we have depleted the number of edges but haven't come to the starting node).

  • Step 4 − Initialize a variable tally to 0.

  • Step 5 − For each node i within the chart −

    In case i isn't the current node, call the countPaths work recursively with i as the current node and K - 1 as the remaining edges.

    Include the returned esteem to number.

  • Step 6 − Return check.

Example

#include <stdio.h>

int countPaths(int currNode, int remainingEdges, int totalNodes) {
   if (remainingEdges == 0 && currNode == 0) {
      return 1;
   }
   if (remainingEdges == 0) {
      return 0;
   }

   int count = 0;
   for (int i = 0; i < totalNodes; i++) { 
      if (i != currNode) {
         count += countPaths(i, remainingEdges - 1, totalNodes);
      }
   }
   return count;
}

int main() {
   int totalNodes = 4;
   int K = 3; // Number of edges to travel
   
   int numPaths = countPaths(0, K, totalNodes);
   printf("Number of ways to reach the starting node after %d edges: %d\n", K, numPaths);
   
   return 0;
}

Output

Number of ways to reach the starting node after 3 edges: 6

Approach 2: Dynamic Programming

Algorithm

  • Step 1 − Make a 2D array dp of estimate (totalNodes x K+1), initialized with all zeros.

  • Step 2 − Use for loop and for each i from 1 to K and each j from 1 to totalNodes −

    For each k from to totalNodes −

    If k doesn't rise to j, include dp[k][i-1] to dp[j][i].

  • Step 3 − Invoke the defined function countpaths() and pass it value to the variable numpaths.

  • Step 4 − Finally, print the resulting value.

Example

#include <stdio.h>

#define MAX_NODES 100

int countPaths(int totalNodes, int K) {
   int dp[MAX_NODES][MAX_NODES];
   for (int i = 0; i < totalNodes; i++) { 
      for (int j = 0; j <= K; j++) {
         dp[i][j] = 0;
      }
   }

   dp[0][0] = 1;
   for (int i = 1; i <= K; i++) {
      for (int j = 1; j < totalNodes; j++) {
         for (int k = 0; k < totalNodes; k++) {
            if (k != j) {
               dp[j][i] += dp[k][i - 1];
            }
         }
      }
   }

   int numPaths = 0;
   for (int i = 0; i < totalNodes; i++) {
      numPaths += dp[i][K];
   }
   return numPaths;
}

int main() {
   int totalNodes = 4; 
   int K = 3; // Number of edges to travel

   int numPaths = countPaths(totalNodes, K);
   printf("Number of ways to reach the starting node after %d edges: %d\n", K, numPaths);

   return 0;
}

Output

Number of ways to reach the starting node after 3 edges: 12

Conclusion

The brute constraint recursion approach comprehensively looks at all conceivable ways. The energetic programming approach optimizes the computation by putting away the middle comes about. At last, a numerical equation gives a coordinate calculation based on the number of hubs and edges. These approaches offer effective arrangements to decide the check of ways to drive back to the beginning hub in a total chart with K edges. By utilizing these strategies, we will successfully illuminate this issue in C.

Updated on: 25-Aug-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements