#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    76
    Rep Power
    12

    Combinations and Permutations question


    I'm seeking advice for a combinations.

    I'm familiar with a small amount of statistical math, and I wanted to create a combination listing. I understand that you use nCk() function to get the number of unique combinations...

    n!
    --------- = number of combinations
    k!(n-k)!

    but I want to get a list of all unique combinations.

    I.E. - If I used CAT and wanted 2 letter combinations of it's letters
    (CAT) 3 C 2
    List -
    CA
    AT
    CT

    I want to create an array that contains all the unique combinations that can be derived from the components that will be combined.

    Any suggestions / solutions?
    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    The Netherlands
    Posts
    122
    Rep Power
    12

    Re: Combinations and Permutations question



    I want to create an array that contains all the unique combinations that can be derived from the components that will be combined.
    Just call the Comb. function and get all combinations to the length of the word.

    nC0, nC1, nC2, ... nCword.length.
    ~mgb
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    76
    Rep Power
    12
    Not really using a word. That was just an example. It's going to be a database of components that can be matched in combinations.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    The Netherlands
    Posts
    122
    Rep Power
    12
    Can you explain more? How many elements does each combo. have? 2Cn or 3Cn or all possible subsets?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    this (c#) function does what you're looking for (hopefully)
    Code:
        public static String[] getCombinations(String[] sourceArray,int size)
         {int i,j;
          int sourceLength=sourceArray.Length;
          if (sourceLength<size) {size=sourceLength;}
          int[] indices=new int[size];
          int count=1;
          for (i=0;i<size&#59;&#41;
           {count*=(sourceLength-i);
            count/=++i;
           }
          String[] items=new String[count];
          int indexCap=1+sourceLength-size;
          for (i=0;i<count;++i)
           {for (j=0;j<size;++j) {items[i]+=sourceArray[j+indices[j]]+" ";} 
                                  items[i]=items[i].Substring(0,items[i].Length-1);
            while (0<j) 
             {indices[--j]++;
              if (indices[j]<indexCap) {break;}
             }
            if (indices[0]<indexCap)
             {for (j=0;j<size;++j) {if (indices[j]==indexCap) {indices[j]=indices[j-1];}}}
           } 
          return items;
         }
    It's set up to return an array of strings - each item being a list of the constituent strings separated by spaces. It's easy enough to change it to accept an array of object references as an input and return a 2 dimensional array of object references instead.

    Let me know if you need it explained...

    What's the comb() function?
    Last edited by epl; July 16th, 2003 at 10:54 AM.

IMN logo majestic logo threadwatch logo seochat tools logo