Solved

Algorithm for combination generator

Posted on 2008-06-12
5
7,462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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