Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got one very hard problem.

Given a tree with nodes with integers. We need to find the largest sum of label values for a set of nodes which does not include any adjacent pair of nodes. Algorithm should run in poly time.

Can you help me to solve it ?

share|cite|improve this question
1. Does this question has some context (i.e. is it a homework maybe, it does sound like one)? 2. Have you heard of dynamic programming? – dtldarek Jun 17 '12 at 18:48
Sorry i forgot about tag. Yes, i heard about dynamic programming. – user33884 Jun 17 '12 at 18:58
Well then, I left you a hint, if you need some more help, just post comments to it and I will try to explain. – dtldarek Jun 17 '12 at 19:00
If we have a root in our tree, that we walk up and trying to find the best sum, aren't we ? – user33884 Jun 17 '12 at 19:03
And what we can go with this tree?… – user33884 Jun 17 '12 at 19:19
up vote 1 down vote accepted


Root the tree in an arbitrary vertex and going bottom-up consider the best solution for every subtree if the top node of it is taken (and therefore none of the children are) or when it is not taken (so for any child you can, but don't have to take it).

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.