Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain forest out of tree with even number of nodes

Tags:

algorithm

tree

I'm stuck on a code challenge, and I want a hint.

PROBLEM: You are given a tree data structure (without cycles) and are asked to remove as many "edges" (connections) as possible, creating smaller trees with even numbers of nodes. This problem is always solvable as there are an even number of nodes and connections.

Your task is to count the removed edges.

Input: The first line of input contains two integers N and M. N is the number of vertices and M is the number of edges. 2 <= N <= 100. Next M lines contains two integers ui and vi which specifies an edge of the tree. (1-based index)

Output: Print the number of edges removed.

Sample Input

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

Sample Output : 2

Explanation : On removing the edges (1, 3) and (1, 6), we can get the desired result.

like image 878
g4ur4v Avatar asked Aug 20 '12 18:08

g4ur4v


People also ask

How can a forest be converted to a tree in data structure?

The correspondence between trees and a forests – Removing the root node from a tree, its subtrees become a forest. – Adding an extra node as the root of the trees in a forest, the forest becomes a tree.

How do you find the number of nodes in a tree?

If binary search tree has height h, minimum number of nodes is h+1 (in case of left skewed and right skewed binary search tree). If binary search tree has height h, maximum number of nodes will be when all levels are completely full. Total number of nodes will be 2^0 + 2^1 + …. 2^h = 2^(h+1)-1.

How do you find the number of edges in a tree with n nodes?

Elements of trees are called their nodes. The nodes without child nodes are called leaf nodes. A tree with 'n' vertices has 'n-1' edges.

What is balanced forest tree?

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest.


2 Answers

I used BFS to travel through the nodes. First, maintain an array separately to store the total number of child nodes + 1. So, you can initially assign all the leaf nodes with value 1 in this array. Now start from the last node and count the number of children for each node. This will work in bottom to top manner and the array that stores the number of child nodes will help in runtime to optimize the code.

Once you get the array after getting the number of children nodes for all the nodes, just counting the nodes with even number of nodes gives the answer. Note: I did not include root node in counting in final step.

like image 61
g4ur4v Avatar answered Sep 22 '22 06:09

g4ur4v


This is my solution. I didn't use bfs tree, just allocated another array for holding eachnode's and their children nodes total number.

import java.util.Scanner; import java.util.Arrays;  public class Solution {         public static void main(String[] args) {                 int tree[];                 int count[];                  Scanner scan = new Scanner(System.in);                  int N = scan.nextInt(); //points                 int M = scan.nextInt();                  tree = new int[N];                 count = new int[N];                 Arrays.fill(count, 1);                  for(int i=0;i<M;i++)                 {                         int u1 = scan.nextInt();                     int v1 = scan.nextInt();                      tree[u1-1] = v1;                      count[v1-1] += count[u1-1];                      int root = tree[v1-1];                      while(root!=0)                     {                         count[root-1] += count[u1-1];                         root = tree[root-1];                     }                 }                  System.out.println("");                  int counter = -1;                 for(int i=0;i<count.length;i++)                 {                         if(count[i]%2==0)                         {                                 counter++;                         }                  }                 System.out.println(counter);          }  } 
like image 32
Eren Yagdiran Avatar answered Sep 21 '22 06:09

Eren Yagdiran