# Minimum number of groups of nodes such that no ancestor is present in the same group

Given a tree of N nodes. The task is to form the minimum number of groups of nodes such that every node belong to exactly one group, and none of its ancestors are in the same group. The parent of each node is given (-1 if a node does not have a parent).

Examples:

Input: par[] = {-1, 1, 2, 1, 4}
Output: 3
The three groups can be:
Group 1: {1}
Group 2: {2, 4}
Group 3: {3, 5}

Input: par[] = {-1, 1, 1, 2, 2, 5, 6}
Output: 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The groups can be made by grouping nodes on the same level together (A node and any of it’s ancestors cannot be on the same level). So the minimum number of groups will be the maximum depth of the tree.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the depth of the tree ` `int` `findDepth(``int` `x, vector<``int``> child[]) ` `{ ` `    ``int` `mx = 0; ` ` `  `    ``// Find the maximum depth of all its children ` `    ``for` `(``auto` `ch : child[x]) ` `        ``mx = max(mx, findDepth(ch, child)); ` ` `  `    ``// Add 1 for the depth of the current node ` `    ``return` `mx + 1; ` `} ` ` `  `// Function to return ` `// the minimum number of groups required ` `int` `minimumGroups(``int` `n, ``int` `par[]) ` `{ ` `    ``vector<``int``> child[n + 1]; ` ` `  `    ``// For every node create a list of its children ` `    ``for` `(``int` `i = 1; i <= n; i++) ` `        ``if` `(par[i] != -1) ` `            ``child[par[i]].push_back(i); ` `    ``int` `res = 0; ` ` `  `    ``for` `(``int` `i = 1; i <= n; i++) ` ` `  `        ``// If the node is root ` `        ``// perform dfs starting with this node ` `        ``if` `(par[i] == -1) ` `            ``res = max(res, findDepth(i, child)); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `main() ` `{ ` `    ``int` `par[] = { 0, -1, 1, 1, 2, 2, 5, 6 }; ` `    ``int` `n = ``sizeof``(par) / ``sizeof``(par) - 1; ` `    ``cout << minimumGroups(n, par); ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to return the depth of the tree ` `    ``static` `int` `findDepth(``int` `x, Vector child[])  ` `    ``{ ` `        ``int` `mx = ``0``; ` ` `  `        ``// Find the maximum depth of all its children ` `        ``for` `(Integer ch : child[x]) ` `            ``mx = Math.max(mx, findDepth(ch, child)); ` ` `  `        ``// Add 1 for the depth of the current node ` `        ``return` `mx + ``1``; ` `    ``} ` ` `  `    ``// Function to return ` `    ``// the minimum number of groups required ` `    ``static` `int` `minimumGroups(``int` `n, ``int` `par[]) ` `    ``{ ` `        ``Vector[] child = ``new` `Vector[n + ``1``]; ` `        ``for` `(``int` `i = ``0``; i <= n; i++)  ` `        ``{ ` `            ``child[i] = ``new` `Vector(); ` `        ``} ` `         `  `        ``// For every node create a list of its children ` `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `            ``if` `(par[i] != -``1``) ` `                ``child[par[i]].add(i); ` `        ``int` `res = ``0``; ` ` `  `        ``for` `(``int` `i = ``1``; i <= n; i++) ` ` `  `            ``// If the node is root ` `            ``// perform dfs starting with this node ` `            ``if` `(par[i] == -``1``) ` `                ``res = Math.max(res, findDepth(i, child)); ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `par[] = { ``0``, -``1``, ``1``, ``1``, ``2``, ``2``, ``5``, ``6` `}; ` `        ``int` `n = par.length - ``1``; ` `        ``System.out.print(minimumGroups(n, par)); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the depth of the tree ` `def` `findDepth(x, child): ` `    ``mx ``=` `0` `     `  `    ``# Find the maximum depth  ` `    ``# of all its children ` `    ``for` `ch ``in` `child[x]: ` `        ``mx ``=` `max``(mx, findDepth(ch, child)) ` `         `  `    ``# Add 1 for the depth  ` `    ``# of the current node ` `    ``return` `mx ``+` `1` ` `  `# Function to return the minimum   ` `# number of groups required ` `def` `minimumGroups(n, par): ` `    ``child ``=` `[[] ``for` `i ``in` `range``(n ``+` `1``)] ` `     `  `    ``# For every node create a list ` `    ``# of its children ` `    ``for` `i ``in` `range``(``0``, n): ` `        ``if` `(par[i] !``=` `-``1``): ` `            ``child[par[i]].append(i) ` `    ``res ``=` `0` `    ``for` `i ``in` `range``(``0``, n): ` `         `  `        ``# If the node is root ` `        ``# perform dfs starting with this node ` `        ``if``(par[i] ``=``=` `-``1``): ` `            ``res ``=` `max``(res, findDepth(i, child)) ` `    ``return` `res ` ` `  `# Driver Code ` `array ``=` `[``0``, ``-``1``, ``1``, ``1``, ``2``, ``2``, ``5``, ``6``] ` `print``(minimumGroups(``len``(array), array)) ` ` `  `# This code is contributed  ` `# by SidharthPanicker `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to return the depth of the tree ` `    ``static` `int` `findDepth(``int` `x, List<``int``> []child)  ` `    ``{ ` `        ``int` `mx = 0; ` ` `  `        ``// Find the maximum depth of all its children ` `        ``foreach` `(``int` `ch ``in` `child[x]) ` `            ``mx = Math.Max(mx, findDepth(ch, child)); ` ` `  `        ``// Add 1 for the depth of the current node ` `        ``return` `mx + 1; ` `    ``} ` ` `  `    ``// Function to return ` `    ``// the minimum number of groups required ` `    ``static` `int` `minimumGroups(``int` `n, ``int` `[]par) ` `    ``{ ` `        ``List<``int``>[] child = ``new` `List<``int``>[n + 1]; ` `        ``for` `(``int` `i = 0; i <= n; i++)  ` `        ``{ ` `            ``child[i] = ``new` `List<``int``>(); ` `        ``} ` `         `  `        ``// For every node create a list of its children ` `        ``for` `(``int` `i = 1; i <= n; i++) ` `            ``if` `(par[i] != -1) ` `                ``child[par[i]].Add(i); ` `        ``int` `res = 0; ` ` `  `        ``for` `(``int` `i = 1; i <= n; i++) ` ` `  `            ``// If the node is root ` `            ``// perform dfs starting with this node ` `            ``if` `(par[i] == -1) ` `                ``res = Math.Max(res, findDepth(i, child)); ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `[]par = { 0, -1, 1, 1, 2, 2, 5, 6 }; ` `        ``int` `n = par.Length - 1; ` `        ``Console.Write(minimumGroups(n, par)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```5
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.