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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

5


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.



Improved By : SidharthPanicker



Article Tags :
Practice Tags :


2


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