TITLE: Looping over $k$-element subsets by switching elements
QUESTION [5 upvotes]: I would like to iterate over the $k$-element subsets of $\{1,2, \dots, n\}$ in a natural way by switching elements.
Two subsets $v,w$ are adjacent if $|v \cap w| = k-1$ or equivalently if their symmetric difference is $|v \triangle w| = 2$.
Is it possible to loop over all $k$-element subsets so that each one is adjacent to the last?  This would be a Hamiltonian circuit in the Johnson Graph (see Chapter 7 in this pdf notes)

REPLY [3 votes]: This survey paper, "A Survey of Combinatorial Gray Codes", by C. Savage, provides several references for your problem on pages 13–14.  In particular, there is a simple algorithm to loop over the $k$-element subsets so that each differs from the previous in only one position:

As observed in [BER76], a Gray code for combinations
  can be extracted from the binary reflected Gray code for $n$-bit numbers: delete from the
  binary reflected Gray code list all those elements corresponding to subsets which do not
  have exactly $k$ elements. That which remains is a list of all $k$-subsets in which successive
  sets differ in exactly one element.

[BER76] is J. R. Bitner, G. Ehrlich, and E. M. Reingold. "Efficient generation of the binary reflected
Gray code". Communications of the ACM, 19(9):517–521, 1976. Savage also refers to a 1989 paper of Wilf that she says contains several other solutions.
(I found this by guessing that this would have something to do with Gray codes, and then doing Google search for gray code with fix number of bits set. The Savage paper was the first hit. I mention this only in the hope that it will be helpful in the future.)