> can i have the algorithm only in plain simple english
> rather than havin the whole code ???
> it ll be really helpful if u tell me the logic
Suppose there are n elements in the set. You can do backtracking.
Let's call the backtracking procedure recur(int l). Here l represent
the level you have reached in the backtracking process. If l >= n, it
means we have reached the end and what we need to do is to print out
the elements selected during the backtracking process. If l < n, then
we are dealing with the l-th element. There are two possibilities:
Either it's selected in the current subset, or it's not. For each
possibility, we continue to consider the next element. That's all we
need to do.
The code below precisely implements the idea in the paragraph above.
You can check it out if there is any doubt.
const int n = 3;
bool v[n];
recur(int l) {
if (l >= n) { // we have reached the end
// print out the result
cout << "{";
bool first = true;
for (int i = 0; i < n; i++)
if (v[i]) {
cout << (first ? "" : ", ") << i;
first = false;
}
cout << "}\n";
} else { // we are dealing with the ith element
v[i] = false; // suppose it's not in the subset
recur(l + 1);
v[i] = true; // suppose it's in the subset
recur(l + 1);
}
}
int main() {
recur(0);
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---