0

I'm looking to understand how this problem shows optimal substructure:

Problem: Given any binary tree with only positive integers as nodes, how can you find a disjoint subset (made from nodes that have no edges between them) to achieve the biggest possible product.

So far, I've determined that since the nodes can only be positive, the largest product would either be a result of multiplying as many nodes together as possible, selecting nodes with the higher values to multiply, or some sort of combination between the two. If I were to go about the first solution, I can start from the lowest level and take the nodes from every other level. However, this doesn't show optimal substructure. Any pointers would be appreciated!

enzo
  • 9,861
  • 3
  • 15
  • 38
coco
  • 1
  • 1

2 Answers2

0

Choose root R if given tree is an free (unrooted) tree.

Let's define on tree T the function F(R) as a set of nodes having the greatest product among all valid set of nodes that have R as one of its elements. Valid here refers to "made from nodes that have no edges between them".

Also, let's define on tree T the function H(R) similar to F(R) but on sets that don't have R as one of its elements.

We can state that an optimal set of nodes O can have (or not) node R as one of its elements.

  1. If RO then an optimal set of nodes O' is
    {R, H(R.children[0]), H(R.children[1]), ..., H(R.children[R.children.size() - 1])} where H(R.children[i]) is defined over the subtree that has R.children[i] as root. Because R was selected in O none of its children can belong to O and multiplication of nodes in H(R.children[i]) is greater or equal than the multiplication of nodes in O that belong to subtree defined by R.children[i]. As every subtree answer is independent from other subtrees' answers and O is optimal we know that multiplication of nodes in H(R.children[i]) is lower or equal than the multiplication of nodes in O that belong to subtree defined by R.children[i]. Then O' is optimal too.

  2. If RO then an optimal set of nodes O' is
    {R, max(F(R.children[0]), H(R.children[0])), max(F(R.children[1]), H(R.children[1])), ..., max(F(R.children[R.children.size() - 1]), H(R.children[R.children.size() - 1]))} where F(R.children[i]) and H(R.children[i]) are defined over the subtree that has R.children[i] as root. More or less same explanation as #1 but as R wasn't selected on O then any of R's children can be selected (or not) on their subtree.

As an optimal choice is unknown then the answer is max(F(R), H(R)) as there are only two possible cases on the choice of R in an optimal set.

Eloy Pérez Torres
  • 1,050
  • 4
  • 14
0

According to Wikipedia, "if a problem can be solved optimally by breaking it into sub-problems and then recursively finding the optimal solutions to the sub-problems, then it is said to have optimal substructure."

We seem to have a clear case for breaking this problem into subproblems and solving recursively. Let t be the current node evaluated by the function. If we include t, we cannot include any of its children so we multiply its value by the results for the grandchildren as parameters; otherwise, we multiply the results for the children as parameters. Generally:

f(t) = max(
  value(t) * product(f(g)),
  product(f(c))
)

where g is a granchild of t
      c is a child of t
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61