Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++


We are given a tree which can have endless levels, a variable child which will store the number of children a node can have, a variable weight which will store the weight associated with the path and a variable path that will store the path and task is to calculate the count of number of paths which has weights equals to the X and there must be an at least one edge with the given weight.

For Example

Input - int child = 4, weight = 4, path = 4; 

Output - Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 1

Explanation - As we are given with the node having 4 children connected with 4 paths and have weight of 4 associated with the path. So, we can see that there can only be one path with the weight as 4 i.e. 1 - 4 therefore the count is 1. 

Input - int child = 3, weight = 2, path = 4; 

Output - Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 4

Explanation - As we are given with the node having 3 children connected with 4 paths and have weight of 2 associated with the path. So, we can see that there can be four paths with the weight as 2 i.e. 1-1, 1 - 2, 2-1 and 2-1 therefore, the count is 4. 

Approach used in the below program is as follows

  • Input the total number of children, paths and weight associated with each path in the variable's child, weight and path respectively.
  • Declare an array of given size.
  • Start loop FOR from i to 0 till the size of an array. Inside the loop, start another loop FOR from j to 0 till j less than 2 and then set arr[i][j] as -1.
  • Now call the function total_weight() by passing path, 0, weight, child and arr as an argument to the function.
  • Inside the function,
    • Declare a temporary variable count to store the result.
    • Check IF path less than 0 then return 0
    • Check IF path equals 0 then return i
    • Check IF arr[path][i] not equals to 1 then return arr[path][i]
    • Start loop FOR from j to 1 till child. Inside the loop, check IF j equals weight than set count as a recursive call to the function total_weight() function by passing path-j, 1, weight, child and arr to the function as an argument.
    • Else, set count as a recursive call to the function total_weight() function by passing path-j, i, weight, child and arr to the function as an argument.
    • Set arr[path][i] as count and 
  • Return arr[path][i]
  • print the result.

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
#define size 4
#define col 4
int total_weight(int path, int i, int weight, int child, int arr[size + 1][col]) {
   int count = 0;
   if (path < 0) {
      return 0;
   }
   if (path == 0) {
      return i;
   }
   if (arr[path][i] != -1) {
      return arr[path][i];
   }
   for (int j = 1; j <= child; j++) {
      if (j == weight) {
         count += total_weight(path - j, 1, weight, child, arr);
      } else {
         count += total_weight(path - j, i, weight, child, arr);
      }
   }
   arr[path][i] = count;
   return arr[path][i];
}
int main() {
   int child = 4, weight = 4, path = 4;
   int arr[size + 1][col];
   for (int i = 0; i <= size; i++) {
      for (int j = 0; j < 2; j++) {
         arr[i][j] = -1;
      }
   }
   cout << "Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: " << total_weight(path, 0, weight, child, arr);
}

If we run the above code it will generate the following output −

Output

Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 1

Updated on: 29-Jan-2021

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements