wu :: forums
« wu :: forums - Tree »

Welcome, Guest. Please Login or Register.
May 3rd, 2024, 6:50pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, Grimbal, william wu, towr, Eigenray, Icarus, ThudnBlunder)
   Tree
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Tree  (Read 834 times)
witee
Newbie
*





   
Email

Posts: 48
Tree  
« on: Jul 28th, 2010, 4:06pm »
Quote Quote Modify Modify

A binary tree is given with a positive weight attached to each node. Find the subset of nodes in the tree whose sum give you the maximum weight such that if a node is in the subset, none of its children can be in that subset.
Pseudo code plz?
IP Logged
sateesp
Newbie
*





   


Gender: male
Posts: 35
Re: Tree  
« Reply #1 on: Jul 28th, 2010, 8:09pm »
Quote Quote Modify Modify

Here we will get only two sets...
Set1= {root,secondlevel nodes, fourth level nodes....}
set2 = {first levl nodes, thrid level nodes...}
 
Do the level order traversal and find the sum of set1 elements and sum of set2 elements. Return whichever is the maximum.
 
 
IP Logged
nakli
Junior Member
**






   
WWW Email

Gender: male
Posts: 62
Re: Tree  
« Reply #2 on: Jul 28th, 2010, 9:29pm »
Quote Quote Modify Modify

on Jul 28th, 2010, 8:09pm, sateesp wrote:
Here we will get only two sets...

There are more than two sets possible. Let a node at fourth level and first level have a very heavy weight. Then in my subset i would include both of them, skipping the node at second and third level.
 
 
This seems like a dynamic programming problem.
 
Pseudo code:
maximumweight current node
{
If  (weight of current node) heavier than (sum of maximumweight of its children)
return (weight of current node)
else return (sum of maximumweight of its children)
}
IP Logged

I was born naked on a bed with a lady.
sateesp
Newbie
*





   


Gender: male
Posts: 35
Re: Tree  
« Reply #3 on: Jul 28th, 2010, 10:26pm »
Quote Quote Modify Modify

In given problem, it has been mentioned that,
A binary tree is given with a positive weight attached to each node.  
 
If you skip any node, then it's not maximum subset.
 
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Tree  
« Reply #4 on: Jul 29th, 2010, 2:19am »
Quote Quote Modify Modify

on Jul 28th, 2010, 10:26pm, sateesp wrote:
If you skip any node, then it's not maximum subset.
You may want to skip a level in a particular subtree.
for example if you have
     1
  2    20
10 3  4  5
neither 2+20, nor 1+10+3+4+5 are maximum. The maximum is 20+10+3
 
You should do this recursively, returning a pair of values for each node, the weight when you do and do not include it. If you don't include the current node, you take the sum of the maximum of all pairs of all children, if you do include it, you take the sum of the total weights of child subtrees excluding the immediate children.
That gives you in O(n) time what you want.
 
 
Pseudo Code:
foo(root)
{
 if (!root)
   return [0,0];
 else
 {
  in=root->val;
  ex=0;
  foreach child of root
  {
    [a,b] = foo(child);
    in += b;
    ex += max(a,b);
  }
  return [in, ex];
 }
}

 
[edit]Fixed mistake pointed out below[/edit]
« Last Edit: Jul 29th, 2010, 4:48am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
sateesp
Newbie
*





   


Gender: male
Posts: 35
Re: Tree  
« Reply #5 on: Jul 29th, 2010, 3:21am »
Quote Quote Modify Modify

Thanks Tower. I got it.
 
Just small correction,  in for loop you have mentioned, in += a; but it should be in += b;  
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Tree  
« Reply #6 on: Jul 29th, 2010, 4:47am »
Quote Quote Modify Modify

on Jul 29th, 2010, 3:21am, sateesp wrote:
Just small correction,  in for loop you have mentioned, in += a; but it should be in += b;
Ah, yes; thank you. I'll correct that.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board