Problem: Start with a set S of size 2n+1 and a subset A of S of size n. You have functions addElement(A,x) and removeElement(A,x) that can add or remove an element of A. Write a function that cycles through all the subsets of S of size n or n+1 using just these two operations on A.
I figured out that there are (2n+1 choose n) + (2n+1 choose n+1) = 2 * (2n+1 choose n) subsets that I need to find. So here’s the structure for my function:
for (int k=0; k<2*binomial(2n+1,n); ++k) {
if (k mod 2) {
// somehow choose x from S-A
A = addElement(A,x);
printSet(A,n+1);
} else
// somehow choose x from A
A = removeElement(A,x);
printSet(A,n);
}
}
The function binomial(2n+1,n) just gives the binomial coefficient, and the function printSet prints the elements of A so that I can see if I hit all the sets.
I don’t know how to choose the element to add or remove, though. I tried lots of different things, but I didn’t get anything that worked in general.
For n=1, here’s a solution that I found that works:
for (int k=0; k<6; ++k) {
if (k mod 2) {
x = S[A[0] mod 3];
A = addElement(A,x);
printSet(A,2);
} else
x = A[0];
A = removeElement(A,x);
printSet(A,1);
}
}
and the output for S = [1,2,3] and A=[1] is:
[1,2]
[2]
[2,3]
[3]
[3,1]
[1]
But even getting this to work for n=2 I can’t do. Can someone give me some help on this one?
This isn’t a solution so much as it’s another way to think about the problem.
Make the following graph:
Sof sizesnorn+1.vandwif the two sets differ by one element.For example, for n=1, you get the following cycle:
Your problem is to find a Hamiltonian cycle:
In other words, this problem is hard.
There are a handful of theorems giving sufficient conditions for a Hamiltonian cycle to exist in a graph (e.g. if all vertices have degree at least
N/2whereNis the number of vertices), but none that I know immediately implies that this graph has a Hamiltonian cycle.You could try one of the myriad algorithms to determine if a Hamiltonian cycle exists. For example, from the wikipedia article on the Hamiltonian path problem:
Hope this helps.
Good News: Though the Hamiltonian cycle problem is difficult in general, this graph is very nice: it’s bipartite and
(n+1)-regular. This means there may be a nice solution for this particular graph.Bad News: After doing a bit of searching, it turns out that this problem is known as the Middle Levels Conjecture, and it seems to have originated around 1980. As best I can tell, the problem is still open in general, but it has been computer verified for
n <= 17(and I found a preprint from 12/2009 claiming to verifyn=18). These two pages have additional information about the problem and references: