The Wayback Machine - https://web.archive.org/web/20220927182204/https://www.geeksforgeeks.org/count-paths-of-tree-such-that-starting-and-ending-node-values-are-same/
Skip to content
Related Articles

Related Articles

Count paths of Tree such that starting and ending node values are same

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Sep, 2022
View Discussion
Improve Article
Save Article

Given a tree with N nodes, an array A[] of size N denoting the value of each node and array edges [][] of size (N-1), the task is to count the total number of paths such that the starting and the ending node values of the path are equal and no other node in the path has a greater value than the starting node.

Examples:

Input: N = 5, edges[][] = {{1, 2}, {1, 3}, {3, 4}, {3, 5}}, A = {2, 3, 1, 3, 2}
Output: 2
Explanation: There are only 2 paths possible: 1 -> 3 -> 5 and  2 -> 1 -> 3 -> 4.

Input: N = 3, edges[][] = {{1, 2}, {2, 3}}, A = {0, 1, 4}
Output: 0
Explanation: There isn’t any path possible.

Approach: This problem can be solved using depth first search:

From each node traverse the tree and check how many nodes have the same value as this one. Add those as a possible path. The final count will be twice the answer as each path will be considered twice in this way.

Follow the steps mentioned below to implement the idea:

  • Declare a variable ‘Ans’ and initialize to 0.
  • Start a depth-first search from every node in the tree.
    • For every node whose value is equal to starting node increment the ‘Ans’ variable to 1.
    • If a node is reached whose value is greater than the starting node, then there is no path possible below this node.
  • The number of paths calculated is twice the actual count. So divide the answer by 2.
  • Return the value of the Ans variable.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function for dfs
void dfs(int src, int StartValue, vector<int> edge[],
         int A[], int& ans, int parent)
{
    for (auto it : edge[src]) {
        if (it != parent) {
            int CurrentValue = A[it - 1];
            if (CurrentValue > StartValue)
                continue;
            if (CurrentValue == StartValue)
                ans++;
            dfs(it, StartValue, edge, A, ans, src);
        }
    }
    return;
}
  
// Function for counting total number of path
int CountSpecialPath(int N, int edges[][2], int A[])
{
    // Construct the graph
    vector<int> edge[N + 1];
    for (int i = 0; i < N - 1; i++) {
        int x = edges[i][0];
        int y = edges[i][1];
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    int ans = 0;
    for (int i = 1; i <= N; i++) {
        int StartValue = A[i - 1];
        dfs(i, StartValue, edge, A, ans, -1);
    }
  
    // As each path is considered twice,
    // divide the answer by 2
    ans /= 2;
    return ans;
}
  
// Driver code
int main()
{
    int N = 5;
    int edges[][2]
        = { { 1, 2 }, { 1, 3 }, { 3, 4 }, { 4, 5 } };
    int A[] = { 2, 3, 1, 2, 3 };
  
    // Functions call
    cout << CountSpecialPath(N, edges, A);
    return 0;
}

Output

2

Time complexity: O(N2) 

  • As for every node we are traversing the whole tree in a depth-first search so the time taken to traverse a tree would be O(N). 
  • As there is N number of total nodes present in the tree, So the overall time complexity would be O(N2).

Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!