Tutorialspoint.dev

Convert a tree to forest of even nodes

Given a tree of n even nodes. The task is to find the maximum number of edges to be removed from the given tree to obtain forest of trees having even number of nodes. This problem is always solvable as given graph has even nodes.

Examples:

Input : n = 10
Edge 1: 1 3
Edge 2: 1 6
Edge 3: 1 2
Edge 4: 3 4
Edge 5: 6 8
Edge 6: 2 7
Edge 7: 2 5
Edge 8: 4 9
Edge 9: 4 10

Output : 2

By removing 2 edges we can obtain the forest with even node tree.
tree_forest
Dotted line shows removed edges. Any further removal of edge will not satisfy 
the even nodes condition.



Find a subtree with even number of nodes and remove it from rest of tree by removing the edge connecting it. After removal, we are left with tree with even node only because initially we have even number of nodes in the tree and removed subtree has also even node. Repeat the same procedure until we left with the tree that cannot be further decomposed in this manner.

To do this, the idea is to use Depth First Search to traverse the tree. Implement DFS function in such a manner that it will return number of nodes in the subtree whose root is node on which DFS is performed. If the number of nodes is even then remove the edge, else ignore.

Below is implementation of this approach:

C++

// C++ program to find maximum number to be removed
// to convert a tree into forest containg trees of
// even number of nodes
#include<bits/stdc++.h>
#define N 12
using namespace std;
  
// Return the number of nodes of subtree having
// node as a root.
int dfs(vector<int> tree[N], int visit[N],
                      int *ans, int node)
{
    int num = 0, temp = 0;
  
    // Mark node as visited.
    visit[node] = 1;
  
    // Traverse the adjacency list to find non-
    // visited node.
    for (int i = 0; i < tree[node].size(); i++)
    {
        if (visit[tree[node][i]] == 0)
        {
            // Finding number of nodes of the subtree
            // of a subtree.
            temp = dfs(tree, visit, ans, tree[node][i]);
  
            // If nodes are even, increment number of
            // edges to removed.
            // Else leave the node as child of subtree.
            (temp%2)?(num += temp):((*ans)++);
        }
    }
  
    return num+1;
}
  
// Return the maxium number of edge to remove
// to make forest.
int minEdge(vector<int> tree[N], int n)
{
    int visit[n+2];
    int ans = 0;
    memset(visit, 0, sizeof visit);
  
    dfs(tree, visit, &ans, 1);
  
    return ans;
}
  
// Driven Program
int main()
{
    int n = 10;
  
    vector<int> tree[n+2];
    tree[1].push_back(3);
    tree[3].push_back(1);
  
    tree[1].push_back(6);
    tree[6].push_back(1);
  
    tree[1].push_back(2);
    tree[2].push_back(1);
  
    tree[3].push_back(4);
    tree[4].push_back(3);
  
    tree[6].push_back(8);
    tree[8].push_back(6);
  
    tree[2].push_back(7);
    tree[7].push_back(2);
  
    tree[2].push_back(5);
    tree[5].push_back(2);
  
    tree[4].push_back(9);
    tree[9].push_back(4);
  
    tree[4].push_back(10);
    tree[10].push_back(4);
  
    cout << minEdge(tree, n) << endl;
    return 0;
}

Python3

# Python3 program to find maximum 
# number to be removed to convert 
# a tree into forest containg trees 
# of even number of nodes 
  
# Return the number of nodes of  
# subtree having node as a root. 
def dfs(tree, visit, ans, node):
    num = 0
    temp = 0
  
    # Mark node as visited. 
    visit[node] = 1
  
    # Traverse the adjacency list  
    # to find non-visited node.
    for i in range(len(tree[node])):
        if (visit[tree[node][i]] == 0):
              
            # Finding number of nodes of 
            # the subtree of a subtree. 
            temp = dfs(tree, visit, ans, 
                          tree[node][i]) 
  
            # If nodes are even, increment 
            # number of edges to removed. 
            # Else leave the node as child 
            # of subtree. 
            if(temp % 2):
                num += temp
            else:
                ans[0] += 1
  
    return num + 1
  
# Return the maxium number of 
# edge to remove to make forest. 
def minEdge(tree, n):
    visit = [0] * (n + 2
    ans = [0]
    dfs(tree, visit, ans, 1
  
    return ans[0]
  
# Driver Code
N = 12
n = 10
  
tree = [[] for i in range(n + 2)]
tree[1].append(3
tree[3].append(1
  
tree[1].append(6
tree[6].append(1
  
tree[1].append(2
tree[2].append(1
  
tree[3].append(4
tree[4].append(3
  
tree[6].append(8
tree[8].append(6
  
tree[2].append(7
tree[7].append(2
  
tree[2].append(5
tree[5].append(2
  
tree[4].append(9
tree[9].append(4
  
tree[4].append(10
tree[10].append(4
  
print(minEdge(tree, n))
  
# This code is contributed by pranchalK


Output:

2

Time Complexity: O(n).

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter