Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ combinatorics: All Combinations of a Set without the Empty Set

I have a set of strings, and I want to find all possible combinations of the strings and add them to a list. I want to end up with a list of a list of each combination of the strings, minus the empty set.

I have created a solution that does this exactly with a nested for loop. However I want to do this more elegantly, preferably with LINQ, and I am not so proficient with it because I'm still pretty new to it.

The solution should have 2^n - 1 lists of combinations where n is the cardinality of the original set. Here is a correct example of what I am looking for:

set = {a, b, c}

completedListOfCombinations = 
{
    {a},
    {b},
    {a, b},
    {c},
    {a, c},
    {b, c},
    {a, b, c}
}

Here is my working, basic but ugly solution that I crafted with help from: https://stackoverflow.com/a/3319652/3371287

List<string> myStrings =  new List<string> { "a", "b", "c" };

var allCombos = new List<List<string>>();

for (int i = 0; i < myStrings.Count; i++)
{
    int subsetCount = allCombos.Count;
    var m = new List<string>();
    m.Add(myStrings[i]);
    allCombos.Add(m);

    for (int j = 0; j < subsetCount; j++)
    {
        string[] subset = new string[allCombos.ElementAt(j).Count + 1];
        allCombos[j].CopyTo(subset, 0);
        subset[subset.Length - 1] = myStrings[i];
        allCombos.Add(subset.ToList());
    }

}

Can someone show me a more elegant solution for this? I have seen similar LINQ solutions that create Cartesian Pairs and lists with a threshold, but I have not been able to tweak them to what I need.

like image 268
user3371287 Avatar asked May 06 '15 16:05

user3371287


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

Providing that all the values in the list are unique:

  List <String> list = new List<String> { "a", "b", "c" };

  var result = Enumerable
    .Range(1, (1 << list.Count) - 1)
    .Select(index => list.Where((item, idx) => ((1 << idx) & index) != 0).ToList());

To print out:

Console.WriteLine(String
  .Join(Environment.NewLine, result
     .Select(line => String.Join(", ", line))));

The outcome is

a
b
a, b
c
a, c
b, c
a, b, c
like image 125
Dmitry Bychenko Avatar answered Sep 19 '22 14:09

Dmitry Bychenko