Skip to main content
deleted 28 characters in body
Source Link
Glorfindel
  • 2.8k
  • 6
  • 28
  • 38

I've converted the algorithm to JavaScript; I don't think MathOverflow supports Stack Snippets but I've managed to host a working demo here in the Sandbox on Meta Stack Exchange. Click the 'Run code snippet' below the code, change the values of $n$ and $k$ and click 'Generate'.

You can also try it online here; you can easily change the values of $k$ and $n$here in the first lines. The subsets are output to the browser console (which you'll need to open first atcase the bottom right)Sandbox fails.

I've converted the algorithm to JavaScript; I don't think MathOverflow supports Stack Snippets but I've managed to host a working demo here on Meta Stack Exchange.

You can also try it online here; you can easily change the values of $k$ and $n$ in the first lines. The subsets are output to the browser console (which you'll need to open first at the bottom right).

I've converted the algorithm to JavaScript; I don't think MathOverflow supports Stack Snippets but I've managed to host a working demo here in the Sandbox on Meta Stack Exchange. Click the 'Run code snippet' below the code, change the values of $n$ and $k$ and click 'Generate'.

You can also try it online here in case the Sandbox fails.

added 628 characters in body
Source Link
Glorfindel
  • 2.8k
  • 6
  • 28
  • 38

I've converted the algorithm to JavaScript; I don't think MathOverflow supports Stack Snippets but I've managed to host a working demo here on Meta Stack Exchange.

You can also try it online here; you can easily change the values of $k$ and $n$ in the first lines. The subsets are output to the browser console (which you'll need to open first at the bottom right).

It seems this algorithm produces Hamiltonian cycles, but to prove it I'll need some sleep first.

I've converted the algorithm to JavaScript; I don't think MathOverflow supports Stack Snippets but I've managed to host a working demo here on Meta Stack Exchange.

You can also try it online here; you can easily change the values of $k$ and $n$ in the first lines. The subsets are output to the browser console (which you'll need to open first at the bottom right).

It seems this algorithm produces Hamiltonian cycles, but to prove it I'll need some sleep first.

Source Link
Glorfindel
  • 2.8k
  • 6
  • 28
  • 38

This seems to be possible for all choices of $k$ and $n$. I found a page here by Dr. Ronald D. BAKER describing a more than sixty year old 'revolving door algorithm'.

When enumerating the k-element subsets of and n-set we are implicitly enumerating the partitions of the n-set into parts, one of size s=k and the other of size t=n-k. Hence the problem is often described as the enumeration of (s,t)-combinations. Suppose the think of the set as a collection of people and imagine them being divided into two adjacent rooms, k people in one room with the remaining n-k people in the other room. Now further imagine that there is a revolving door connecting the two rooms, and a change consists of an individual from each room entering that revolving door and exchanging sides. This analogy is the source of the moniker revolving door algorithm.

W. H. Payne created the following algorithm in 1959. The call to visit might, for example, output the k-subset or it might do the computations of an algorithm which needs all k-element subsets. Each k-subset is referenced by an index-list $c_k \dots c_2c_1$, the indices of the elements belonging to the subset sorted in order. Notice the code makes extensive use of conditionals, the branching command goto and line labels†.

algorithm RevDoorSubset(n, k)
     Array C[1..k+1]
R1:  for i ← 1 to k do    // initialize C
        C[i] ← i-1
     end for
     C[k+1] ← n
R2:  visit(C[ ], k)   // Do whatever is needed w/ subset (just print?)
R3:  if (k is odd)    // the easy cases
       if ( C[1]+1 < C[2] )                            
          C[1] ← C[1]+1
          goto R2
       else
           j ← 2
          goto R4
       fi
     else
       if ( C[1] > 0 )
          C[1] ← C[1]-1
          goto R2
       else
           j ← 2
          goto R5
       fi
     fi
    
R4:  if ( C[j] ≥ j )  // try to decrease C[j]
       C[j] ← C[j-1]
       C[j-1] ← j-2
       goto R2
     else
       j ← j+1
     fi
R5:  if ( C[j] + 1 < C[j+1] )     // try to increase C[j]
       C[j-1] ← C[j]
       C[j] ← C[j] + 1
       goto R2
     else
       j ← j + 1
       if ( j ≤ k)
         goto R4
       fi
     fi
     return
end