Count of subtrees possible from an N-ary Tree
Given an N-ary tree consisting of N nodes having values 0 to (N – 1), the task is to find the total number of subtrees present in the given tree. Since the count can be very large, so print the count modulo 1000000007.
Examples:
Input: N = 3
0
/
1
/
2
Output: 7
Explanation:
The total number of subtrees nodes are {}, {0}, {1}, {2}, {0, 1}, {1, 2}, {0, 1, 2}.
Input: N = 2
0
/
1
Output: 4
Approach: The approach for solving the given problem is to perform DFS Traversal on the given tree. Follow the steps below to solve the problem:
- Initialize a variable, say count as 0, to store the count of the total number of subtrees present in the given tree.
- Declare a function DFS(int src, int parent) to count the number of subtrees for the node src and perform the following operations:
- Initialize a variable, say res as 1.
- Traverse the adjacency list of the current node and if the node in the adjacency list, say X is not the same as the parent node, then recursively call the DFS function for the node X and node src as the parent node as DFS(X, src).
- Let the value returned to the above recursive call is value, then update the value of res as (res * (value + 1)) % (109 + 7).
- Update the value of count as (count + res) % (109 + 7).
- Return the value of res from each recursive call.
- Call the function DFS() for the root node 1.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define MAX 300004
using namespace std;
vector< int > graph[MAX];
int mod = 1e9 + 7;
int ans = 0;
int countSubtreesUtil( int cur, int par)
{
int res = 1;
for ( int i = 0;
i < graph[cur].size(); i++) {
int v = graph[cur][i];
if (v == par)
continue ;
res = (res
* (countSubtreesUtil(
v, cur)
+ 1))
% mod;
}
ans = (ans + res) % mod;
return res;
}
void countSubtrees( int N,
vector<pair< int , int > >& adj)
{
for ( int i = 0; i < N - 1; i++) {
int a = adj[i].first;
int b = adj[i].second;
graph[a].push_back(b);
graph[b].push_back(a);
}
countSubtreesUtil(1, 1);
cout << ans + 1;
}
int main()
{
int N = 3;
vector<pair< int , int > > adj
= { { 0, 1 }, { 1, 2 } };
countSubtrees(N, adj);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX = 300004 ;
static ArrayList<ArrayList<Integer>> graph;
static long mod = ( long )1e9 + 7 ;
static int ans = 0 ;
static int countSubtreesUtil( int cur, int par)
{
int res = 1 ;
for ( int i = 0 ;
i < graph.get(cur).size(); i++)
{
int v = graph.get(cur).get(i);
if (v == par)
continue ;
res = ( int )((res * (countSubtreesUtil(
v, cur) + 1 )) % mod);
}
ans = ( int )((ans + res) % mod);
return res;
}
static void countSubtrees( int N, int [][] adj)
{
for ( int i = 0 ; i < N - 1 ; i++)
{
int a = adj[i][ 0 ];
int b = adj[i][ 1 ];
graph.get(a).add(b);
graph.get(b).add(a);
}
countSubtreesUtil( 1 , 1 );
System.out.println(ans + 1 );
}
public static void main(String[] args)
{
int N = 3 ;
int [][] adj = { { 0 , 1 }, { 1 , 2 } };
graph = new ArrayList<>();
for ( int i = 0 ; i < MAX; i++)
graph.add( new ArrayList<>());
countSubtrees(N, adj);
}
}
|
Python3
MAX = 300004
graph = [[] for i in range ( MAX )]
mod = 10 * * 9 + 7
ans = 0
def countSubtreesUtil(cur, par):
global mod, ans
res = 1
for i in range ( len (graph[cur])):
v = graph[cur][i]
if (v = = par):
continue
res = (res * (countSubtreesUtil(v, cur) + 1 )) % mod
ans = (ans + res) % mod
return res
def countSubtrees(N, adj):
for i in range (N - 1 ):
a = adj[i][ 0 ]
b = adj[i][ 1 ]
graph[a].append(b)
graph[b].append(a)
countSubtreesUtil( 1 , 1 )
print (ans + 1 )
if __name__ = = '__main__' :
N = 3
adj = [ [ 0 , 1 ], [ 1 , 2 ] ]
countSubtrees(N, adj)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int MAX = 300004;
static List<List< int >> graph;
static long mod = ( long ) 1e9 + 7;
static int ans = 0;
static int countSubtreesUtil( int cur, int par) {
int res = 1;
for ( int i = 0; i < graph[cur].Count; i++) {
int v = graph[cur][i];
if (v == par)
continue ;
res = ( int ) ((res * (countSubtreesUtil(v, cur) + 1)) % mod);
}
ans = ( int ) ((ans + res) % mod);
return res;
}
static void countSubtrees( int N, int [,] adj) {
for ( int i = 0; i < N - 1; i++) {
int a = adj[i,0];
int b = adj[i,1];
graph[a].Add(b);
graph[b].Add(a);
}
countSubtreesUtil(1, 1);
Console.WriteLine(ans + 1);
}
public static void Main(String[] args) {
int N = 3;
int [,] adj = { { 0, 1 }, { 1, 2 } };
graph = new List<List< int >>();
for ( int i = 0; i < MAX; i++)
graph.Add( new List< int >());
countSubtrees(N, adj);
}
}
|
Javascript
<script>
var MAX = 300004;
var graph = Array.from(Array(MAX),()=> new Array());
var mod = 1000000007;
var ans = 0;
function countSubtreesUtil(cur, par)
{
var res = 1;
for ( var i = 0;
i < graph[cur].length; i++) {
var v = graph[cur][i];
if (v == par)
continue ;
res = (res
* (countSubtreesUtil(
v, cur)
+ 1))
% mod;
}
ans = (ans + res) % mod;
return res;
}
function countSubtrees(N, adj)
{
for ( var i = 0; i < N - 1; i++) {
var a = adj[i][0];
var b = adj[i][1];
graph[a].push(b);
graph[b].push(a);
}
countSubtreesUtil(1, 1);
document.write( ans + 1);
}
var N = 3;
var adj
= [[ 0, 1 ], [1, 2 ]];
countSubtrees(N, adj);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
02 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...