I want to write the maximal clique algorithm for use with an adjacency matrix. I'm following a video which explains how to code and implement the algorithm using Python. I'm currently trying to code the powerset function at 2 minutes into the video.
def powerSet(elts):
if len(elts) == 0:
return [[]]
else:
smaller = powerSet(elts[1:])
elt = [elts[0]]
withElt = []
for s in smaller:
withElt.append(s + elt)
allofthem = smaller + withElt
return allofthem
print powerSet([1, 2, 3, 4, 5])
I want to rewrite this in C++. I'm not sure if I should be using arrays or not. So far I have coded the below but I don't know how to return an empty array inside an empty array (when the elts
list
is of size 0).
I wrote an isEmpty
function for the array since I cant use len(elts)
like in Python. My approach is probably not the best approach to take so I am open to any advice.
UPDATE:
array powerSet(int elts[])
{
if (isEmpty(elts)==1)
{
return {{}};
}
}
In my int main I have:
list<int> elts;
list<int>::iterator i;
for (i=elts.begin(); i != elts.end(); ++i)
cout << *i << " ";
cout << endl;
powerSet(elts);
I don't know what to do from here.
The code should use either an array
/list
/vector
that we call 'elts' (short for elements).
Then firstly, it should add the empty list []
, then the rest of the power set (all shown in the video).
So for example, in the case that elts = [1,2,3,4]
, my code should return:
`[ [],[4],[3],[4,3],[2],[4,2],[3,2],[4,3,2],[1],[4,1],[3,1],[4,3,1],[2,1],[4,2,1],[3,2,1],[4,3,2,1] ] `
I don't know how to use array
/list
/vector
to do the above.