Related Articles

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

• Last Updated : 07 Jun, 2021

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:
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:

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`

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up