1

I am given a (not necessarily binary) tree with integer (positive/negative) lables on the nodes, and have to find the binary subtree that maximizes the sum of the lables in the tree. I thought about dynamic programming approach - I defined f(u) that returns the maximum sum for all binary trees rooted in u, and calculate f(u) by choosing v,w such that v,w are children of u and they maximze the sum (f(v)+f(w)) over all pairs of u's children, and then f(u) = label(u) + f(v) + f(w). f can be calculated from the leaves up in a typical dynamic programming approach. My question are:

(1) is there a more efficient approach? (2) it seems the most costly step is to find the maximum among all pairs of u's children - if u has n children, the cost is O(n^2), but I think that for entire tree it's less than O(|E|^2), is it true? how can I calculate the cost more tightly?

Thanks.

1 Answer 1

2

If node U has n children, you can find the pair of children V,W that maximizes f(U) by simply taking the children that maximize f(V) and f(W). You don't need to actually examine each pair. You can easily do that in O(2n) = O(n) rather than needing O(n2).

For example, if child nodes have f-values { 6, 5, 2, 12, 8 }, then you just need the two biggest values, which are 8 and 12. You don't need to actually examine each pair of values and explicitly add them.

(Note: if either selected node has a negative f-value, then you should just drop it. Does your binary tree allow internal nodes with only one child?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't notice that...Yes, the binary tree is not necessarily full or complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.