Remember how a tree works, We can designate a particular node in this tree as th
ID: 3596509 • Letter: R
Question
Remember how a tree works, We can designate a particular node in this tree as the root. Our objective is to find an independent set in this tree which is a subset of all the nodes in the tree such that no two nodes in the set are adjacent (or alternatively, a parent and its child are not both in the subset). There coud be multiple such subsets; we want the one with the largest size (i.e. most nodes in it). Give a dynamic programming algorithm that determines the size of the largest independent set, please solve this in java and psudeocode.
Explanation / Answer
If we want to create a largest subset of nodes of a tree such that no two
nodes will have parent chile relationship, then we can follow following approach:
We will traverse the tree the whole tree and alternately place the nodes in the
required subset. For e.g we start from the root, the we will put the root
in one set and the next layer of children in another set. So the childeren will be placed alternately in the subset. Repeating this obserevtion for all the levels of the tree starting from root will give us the largest subset of tree
where no two nodes have parent child relationship.