Solved

Algorithm for combination generator

Posted on 2008-06-12
5
7,436 Views
Last Modified: 2008-08-28
Hi

I'm wondering if you could give me a hand with an algorithm.

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

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.

I've attached code for generating combinations from a fixed number of sets.

Will appreciate anyones help or guidance.

Thanks
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;

                    }

                }

            }

        }

Open in new window

0
Comment
Question by:tejs1dhu
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 21767339
If n isn't too big, you could make the algorithm recursive ...
0
 
LVL 1

Author Comment

by:tejs1dhu
ID: 21768015
Hi Infinity08

Making this recursive is the aim, however I am not sure how to implement this using C#.  Any ideas?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 21768291
In pseudo code it could look something like this :

        function(n, sets, combination)
            if n is max then print combination
            else
                for every element in the n-th set from sets
                    set the n-th value in the combination to that element
                    call function(n + 1, sets, combination)
                end for
            end else
        end function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now