Adobe Interview Question
Developer Program EngineersCountry: United States
Interview Type: In-Person
I have an idea; however, I am not sure if it is acceptable. As we can have a table, we can use it as the recursion stack and use a while(top>= 0) loop. We implement the memoization algorithm in the loop.
Largest Ind set(X) = MAX { (1 + sum of Largest Ind set for all grandchildren of X),
(sum of Largest Ind set for all children of X) }
A DP solution without using recursions. A technique similar to non-recursive DFS is used. A 1xN table is used as a stack. For memoization part, I have put two variables in every node to keep track of the largest independent set in the subtree rooted at that node. One for the case that, that node is included in the set, and another one for the case that, that node is not included in the set.
#include <iostream>
using namespace std;
#define mp make_pair
#define nodeID first
#define state second
typedef pair<int, int> pi;
const int BLACK = 0;
const int GREY = 0;
int max(int a, int b) { return (a < b ? b : a); }
class Node{
int id;
int childCount;
int chosenBestSet;
int notChosenBestSet;
int *children;
public:
int getChildCount()
{
return childCount;
}
int getChildIdAt(int i)
{
return children[i];
}
int getChosenBest()
{
return this->chosenBestSet;
}
int getNotChosenBest()
{
return this->notChosenBestSet;
}
void setChosenBest(int val)
{
this->chosenBestSet = val;
}
void setNotChosenBest(int val)
{
this->notChosenBestSet = val;
}
};
int solve(int numOfNodes, pi *stk, Node* nodes, int root)
{
int top = 0;
stk[top] = mp(root, BLACK);
while (top >= 0)
{
int topElemId = stk[top].nodeID;
Node *curNode = &(nodes[topElemId]);
int childCnt = curNode->getChildCount();
int topElemState = stk[top].state;
if (topElemState == GREY)
{
int chBestCnt = 0, notChBestCnt = 0;
for (int i = 0; i < childCnt; i++)
{
Node curChild = nodes[curNode->getChildIdAt(i)];
chBestCnt += curChild.getNotChosenBest();
notChBestCnt += max(curChild.getChosenBest(), curChild.getNotChosenBest());
}
curNode->setChosenBest(chBestCnt);
curNode->setNotChosenBest(notChBestCnt);
top--;
continue;
}
stk[top].state = GREY;
if (childCnt == 0)
{
curNode->setChosenBest(1);
curNode->setNotChosenBest(0);
top--;
continue;
}
for (int i = 0; i < childCnt; i++)
{
int childId = curNode->getChildIdAt(i);
stk[++top] = mp(childId, BLACK);
}
}
return max(nodes[root].getChosenBest(), nodes[root].getNotChosenBest());
}
This is how i would solve this :
Do a level order traversal of the tree iteratively (using Queue).So your end output should given an array of values which represent the sum of nodes at every level. Now inorder to satisfy the problem we use a DP approach and start from the lower to higher levels of tree.
MaxIndependent(i) = At 'ith' level the maximum independent set (i am not sure if maximum means cardinality or the sum of elements of nodes - change accordingly).
MaxIndependent(i) = Max( MaxIndependent(i-1) , SumArray[i] + MaxIndepedent(j) any of the below level j <i )
the base case is for the lowest level
In the question we only have to find the algorithm that finds the largest independent ser. The memoization solution to solve LIS(Largest independent Set) is not acceptable, We are restricted to using loops and tables
- nesh2391 April 12, 2014