Gray code for all k element subset of {1,…,n}


I am seeking for an algorithm which iterates through all k element subsets of an n element set. I do not want to generate all those subset explicitly.

There is an easy algorithm to do this, namely sorting the corresponding bit vectors lexographically and then go from the current subset to the next one.

Nevertheless, I seek for an algorithm which only switches 2 bits in each step. I have read that such a code is a called "gray-code" but I did not found an algorithm for my problem.

Is there a straight forward implementation for this?


This isn't going to be a complete answer, but it's also not going to fit in a comment.

The relationship to gray code that you need is the mirroring. Every time gray code sets a new bit, all the bits below it progress <em>backwards</em> from that point on (until that bit is cleared or a bit above that reverses the reversal).

To reproduce this with your lexicographic ordering you need to invert the order in which you iterate through the remaining bits after each swap.

You might be able to implement this as an iterative transformation, taking your regular ordering and repeatedly reversing the order of the remaining bits every time you encounter a transition from 1 to 0.


  • SQL Server varchar(50) and varchar(128) performance difference [duplicate]
  • how to play a mp3 file from the middle
  • Inputting a character value into an int array? [duplicate]
  • windows store certification kit - c++ library validation issues
  • better way to communicate between ad hoc wifi windows mobile devices
  • Switch to a remote branch getting detached head [duplicate]
  • Vector Drawables in Layer List on Android API 16 and higher
  • Matlab to Python Conversion binary file read
  • Magento get URL before current
  • abstracting over a collection
  • Prevent Tomcat from caching request during starup
  • Can't remove headers after they are sent
  • DIV instruction jumping to random location?
  • Debugging VB6 Code From Visual Studio 2010
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • C++ pointer value changes with static_cast
  • How do I signal completion of my dataflow?
  • Z3: Convert between FP and BitVector?
  • Diff between two dataframes in pandas
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How to use JavaScript to determine whether a file exists in a directory?
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Jquery - Jquery Wysiwyg return html as a string
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Arrays break string types in Julia
  • PHP: When would you need the self:: keyword?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?