Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 16th, 2003, 12:06 AM
Shisho Shisho is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 76 Shisho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 20 sec
Reputation Power: 10
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.

Reply With Quote
  #2  
Old July 16th, 2003, 06:01 AM
SlankenOgen SlankenOgen is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: The Netherlands
Posts: 122 SlankenOgen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Re: Combinations and Permutations question

Quote:

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

Reply With Quote
  #3  
Old July 16th, 2003, 06:04 AM
Shisho Shisho is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 76 Shisho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 20 sec
Reputation Power: 10
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.

Reply With Quote
  #4  
Old July 16th, 2003, 06:37 AM
SlankenOgen SlankenOgen is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: The Netherlands
Posts: 122 SlankenOgen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Can you explain more? How many elements does each combo. have? 2Cn or 3Cn or all possible subsets?

Reply With Quote
  #5  
Old July 16th, 2003, 10:45 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
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;)
       {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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Combinations and Permutations question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap