Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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[0]) - 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<Integer> 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<Integer>[] child = new Vector[n + 1];
        for (int i = 0; i <= n; i++)
        {
            child[i] = new Vector<Integer>();
        }
         
        // 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




<script>
 
// Javascript implementation of the approach
 
// Function to return the depth of the tree
function findDepth(x, child)
{
    var mx = 0;
 
    // Find the maximum depth of all its children
    child[x].forEach(ch => {
         
        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
function minimumGroups(n, par)
{
    var child = Array.from(Array(n+1), ()=>Array());
 
    // For every node create a list of its children
    for (var i = 1; i <= n; i++)
        if (par[i] != -1)
            child[par[i]].push(i);
    var res = 0;
 
    for (var 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
var par = [0, -1, 1, 1, 2, 2, 5, 6 ];
var n = par.length - 1;
document.write( minimumGroups(n, par));
 
// This code is contributed by famously.
</script>


Output: 

5

 



Last Updated : 07 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads