Note that there are some explanatory texts on larger screens.

plurals
  1. POall possible combinations from a set of sets (n choose k)
    primarykey
    data
    text
    <p>Given several sets such as <code>{1,2,3,4}</code>,<code>{1,2}</code>,<code>{1}</code>,<code>{1,2,3,}</code>,..., and a number <code>k</code> such that the combination that ought to be formed is exactly of length <code>k</code>, <em>how would one go about generating a unique combination of length <code>k</code> if I can only choose one number from each set?</em></p> <p>There are exactly k number of sets to choose from. I'm looking for the most efficient way to do this in c#. </p> <p>Intuitively i tried(though i'm not sure it is correct) generating all possible combinations for each set then concatenating each i'th combination from each set with the corresponding i'th combination from subsequent sets to form a <code>k</code> long unique combination but I'm sure there is a general case for this type of question. </p> <p>Can anyone please offer some tips or advise? Specifically which math or computer science topic this falls under and how these questions are typically solved? </p> <p>in the snippet below a string in the array theSets would be for example "31" this means that k = 2 (length of a string in theSets) and there are two sets one of which is {1,2,3} and {1}. with these sets, generate all the unique combinations and provide a count</p> <pre><code>private int combicount(string[] theSets) { int size = theSets[0].Length; int count = 0; List&lt;HashSet&lt;int&gt;&gt; l = new List&lt;HashSet&lt;int&gt;&gt;(); foreach (string s in theSets) { foreach (char c in s) { HashSet&lt;int&gt; h = new HashSet&lt;int&gt;(); for (int n = 1; n &lt;= int.Parse(c.ToString()); n++) { h.Add(n); } l.Add(h); } //toDO - generate all unique combinations by picking exactly 1 from each set in l //toDO - count the number of unique combinations generated } return count; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload