Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 June 11th, 2008, 11:16 AM
tejsidhu tejsidhu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 1 tejsidhu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 52 sec
Reputation Power: 0
Algorithm Design

Hi

My maths is really rusty. I'm wondering if you guys could give me a hand with an algorithm.

I need to write a algorithm for generating combinations from n number of sets.

I can write code that will take my fixed number of sets and produce the combinations but Im lost on how I would be able to do this for n number of sets.

Here is my existing fixed number of sets combination generator:

int[] firstSet = new int[] { 1, 2, 3, 4, 5 };
int[] secondSet = new int[] { 6, 7, 8, 9, 10, 11 };
int[] thirdSet = new int[] { 12, 13, 14 };
int[] fourthSet = new int[] { 15, 16, 17, 18 };
int counter = 0;

foreach (int firstSetVariable in firstSet)
{
foreach (int secondSetVariable in secondSet)
{
foreach (int thirdSetVariable in thirdSet)
{
foreach (int fourthSetVariable in fourthSet)
{
Response.Write(counter + ": " + firstSetVariable + "," + secondSetVariable + "," + thirdSetVariable + "," + fourthSetVariable + "<br />");
counter += 1;
}
}
}
}

Will appreciate anyones help or guidance.

Thanks

Reply With Quote
  #2  
Old June 17th, 2008, 06:17 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,459 Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 16 h 48 m 37 sec
Reputation Power: 336
This naturally suggests a recursive solution.

Here's one way in Python using generators:
python Code:
Original - python Code
  1. def get_combinations(L):
  2.     if len(L) == 0:
  3.         yield []   
  4.     else:
  5.         for c in get_combinations(L[1:]):
  6.             for x in L[0]:
  7.                 yield [x] + c
In words: recursively get the combinations ignoring the first set, and prepend each element of the first set in turn to each of those combinations.

(There is no math involved, by the way.)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Algorithm Design


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT