-6

I have a list which has always even item count.

I need an algorithm to find all two member combinations.

For instance if item count is 4, output will be;

Items:

{1, 2, 3, 4 }

ResulSet:

{12}, {34}

{13}, {24}

{14}, {23}

Order makes no difference, {12} covers {21}.

For item count 6, output will be;

Items:

{1, 2, 3, 4, 5, 6}

ResulSet:

12 34 56

12 35 46

12 36 45

13 24 56

13 25 46

13 26 45

14 23 56

14 25 36

14 26 35

15 23 46

15 24 36

15 26 34

16 23 45

16 24 35

16 25 34

Could you show me a way please?

Thank you.

Edit:

Question is really short to read, and if you take 1 minute to read question you can see it is not duplicate like most of people think (probably they are semi-illiterate)

Combination of {1, 2, 3, 4, 5, 6} is

123456 and as you see at top, this is not what I'm looking for. Read question if you want to help or just get off.

Have a nice day.

fartking
  • 11
  • 1
  • 7
  • 1
    It would be really nice to see your attempt at this problem before answering. Getting this algorithm as a beginner can be hard (even though its simple, it can be hard to generate by yourself) so I'll try and help you out (see my answer). Please make sure you understand what the code does, and ask questions in the comments if I can help you learn how it works. – BradleyDotNET Apr 25 '14 at 23:07

2 Answers2

0

Since you always have two members in your combination, a simple nested for loop should work:

for (int i = 0; i < data.Length - 1; i++)
   for (int j = i + 1; j < data.Length; j++
      Console.WriteLine({0}{1}, i, j);

We iterate over each item in the list, up to the next to last one (since we can't have 1 number combos). In each of these iterations, we iterate from the outer iteration variable plus 1 (no duplicating elements) up to the end of the list.

This will generate all unique combinations. To do more than two or three member outputs though, you'll want to look into recursion. I'll leave formatting the output to match your question as an exercise to the reader :).

For 6 element combinations, we will have to delve into the wild world of recursion. Recursion can really mess with your head, so please ask if you don't understand something. The general principle of recursion is: "Do something with the first element, call yourself and pass the rest". In this situation, the code would look something like:

public List<List<int>> GetAllCombos (int[] values)
{
    //Kick it off with the 0 index
    return GetCombos(values, 0);
}

private List<List<int>> GetCombos(int[] values, int myIndex)
{
    //A holder for combinations from this index onward
    List<List<int>> combos = new List<List<int>>();

    for (int i = myIndex; i < values.Length; i++)
    {
        if (myIndex + 1 < values.Length)
        {
            foreach (List<int> combo in GetCombos(values, myIndex + 1))
            {
               combo.Add(values[myIndex][i]);
               combos.Add(combo);
            }
        }
        else
        {
           List<int> newCombination = new List<int>() { values[myIndex][i] };
           combos.Add(newCombination);
        }
    }
    return combos;
}

Again, please make sure you ask if you don't understand something. Recursion can be a really tough concept to understand!

BradleyDotNET
  • 57,599
  • 10
  • 90
  • 109
  • Thank you for your reply :) But for 6 items; I need a List with 15 items each item length of 6 like; 12 34 56 12 35 46 I think I defined my problem wrong, sorry about it. – fartking Apr 25 '14 at 23:14
  • @EmreAtaseven, Recursive solution added. Let me know if you have questions! – BradleyDotNET Apr 25 '14 at 23:31
  • Thanks for code, I nearly understand it but it creates duplicate values in set, I will try to modify it. Thank you. – fartking Apr 25 '14 at 23:45
0

Have you done your research? 5 minutes (not even that) with google and I get:

All of which would show you how to do this.

Searching SO, you pretty immediately find this question, Algorithm to return all combinations of k elements from n, with even more resources (not to mention actual solutions).

And if you're a member of IEEE or the ACM, a few seconds searching their online libraries will get you pretty much everything you need.

Community
  • 1
  • 1
Nicholas Carey
  • 60,260
  • 12
  • 84
  • 126
  • Have you read question? 5 minutes (not even that) to read and understand that they are completely different things. – fartking Apr 26 '14 at 14:00