Re: Combinations problem



Thomas Cameron wrote:
> I have an array of values which I would like to generate all possible
> combinations for. And example would be the values "a,b,c". This would
> produce the following:
> a
> ab
> abc
> ac
> b
> bc
> c
> Does anybody know of the algorithm to produce this?

I'm pretty sure this must be covered in Knuth, if not earlier
then in one of the preliminary facsicles for Volume 4.

Anyway, it is easy to figure out. Let's assume that all the
array values are distinct, or if not that you want the
duplicates that will be produced. (Otherwise, first sort
the array then walk it and collapse it down to one instance
each.) Then what you want can be characterized by: sets
such that whether or not each element is present in the set
varies through all possible combinations. That should ring
a bell: presence = 1, absence = 0 => binary integer of width
N where N = # elements. Thus all you have to do is enumerate
the distinct possibilities for an N-bit integer, which can be
done simply by counting, starting at 0. For each value of
the counter, slide a 1-bit past it, ANDing to determine
whether or not the corresponding position denotes the presence
or the absence of that element. If present, output the token;
if absent, do nothing. After the 1-bit has been slid N places,
output a newline and increment the counter. Note that this
will also output the empty string (no element present), which
is a valid combination but which you might want to eliminate
(so start the counter at 1 instead).
.



Relevant Pages

  • Re: Index / match combination
    ... Try this, array entered: ... Microsoft Excel MVP ... "ABC" in Column A to find. ... again an array formula. ...
    (microsoft.public.excel.misc)
  • Re: how to make array of arrays?
    ... you defined an array of ABC, not an array of array of ABC. ... > I created a structure ABC and an array of type ABC ... > Dim str1 As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: File parsing
    ... Build an array or associatiave array... ... I can NOT read until the next occurance ot "abc"- abc 5 ...
    (comp.lang.awk)
  • Re: For Each
    ... For Each will enumerate the collection from lowest to highest index ... A For loop to enumerate an array will be ... Unless you are the controlling process, ...
    (microsoft.public.scripting.vbscript)
  • Re: Permission Denied when using File.Copy or fso.CopyFile
    ... you need to enumerate all your files and copy them one ... otherwise (i.e. if you copy a folder) you cannot say what file ... filename to an array for later retry. ... window while user are browsing). ...
    (microsoft.public.scripting.vbscript)