Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is C# code which solves the question.</p> <p>I've assumed that you really care about maximizing the uniqueness in student pairing, not the set of possible unique groups of student pairings.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.IO; namespace Pairing { class Program { static void Main(string[] args) { //switch these lines if you'd prefer a command line interface to a text file. var rgs = File.ReadAllLines("Roster.txt"); //var rgs = args; var setPairs = new HashSet&lt;HashSet&lt;string&gt;&gt;(); for (var ixrgs = 0; ixrgs &lt; rgs.Length - 1; ixrgs++) for (var ixrgsSubset = ixrgs + 1; ixrgsSubset &lt; rgs.Length; ixrgsSubset++) setPairs.Add(new HashSet&lt;string&gt;(new string[] { rgs[ixrgs], rgs[ixrgsSubset] })); var setGroups = new HashSet&lt;HashSet&lt;HashSet&lt;string&gt;&gt;&gt;(); var setUsedPairs = new HashSet&lt;HashSet&lt;string&gt;&gt;(); while (setPairs.Count &gt; 0) { var setPairsTmp = new HashSet&lt;HashSet&lt;string&gt;&gt;(setPairs); var setTmp = new HashSet&lt;HashSet&lt;string&gt;&gt;(); var setUsedVariables = new HashSet&lt;string&gt;(); while (setPairsTmp.Count &gt; 0) { //give me the first element var pair = setPairsTmp.First&lt;HashSet&lt;string&gt;&gt;(); //store it setTmp.Add(pair); //add it to our set of used variables setUsedVariables.UnionWith(pair); //remove it from our set of available pairs. setPairsTmp.RemoveWhere(set =&gt; set.Intersect&lt;string&gt; (setUsedVariables).Count&lt;string&gt;() != 0); //remove its implicated deadlocks from our set of available pairs //(this step is both gross, and key. Without it, failure potential arises.) var s1 = new HashSet&lt;string&gt;(); var s2 = new HashSet&lt;string&gt;(); //get the set of variables paired with the first: var rgPair = pair.ToArray&lt;string&gt;(); foreach (var set in setUsedPairs) { if (set.Contains(rgPair[0])) s1.UnionWith(set); if(set.Contains(rgPair[1])) s2.UnionWith(set); } s1.IntersectWith(s2); //enumerate the pairs created by the deadlocking pairs, remove them from our available selections. var rgsTmp = s1.ToArray&lt;string&gt;(); for (var ixrgs = 0; ixrgs &lt; rgsTmp.Length - 1; ixrgs++) for (var ixrgsSubset = ixrgs + 1; ixrgsSubset &lt; rgsTmp.Length; ixrgsSubset++) setPairsTmp.RemoveWhere(set =&gt; set.Contains(rgsTmp[ixrgs]) &amp;&amp; set.Contains(rgsTmp[ixrgsSubset])); } setPairs.ExceptWith(setTmp); setGroups.Add(setTmp); setUsedPairs.UnionWith(setTmp); } //at this point, setGroups contains the set of unique group combinations. //the non-maximally sized groups indicate unique sets that could form provided that //all other students are in redundant sets. var enumerableGroups = setGroups.OrderByDescending&lt;HashSet&lt;HashSet&lt;string&gt;&gt;, int&gt;(set =&gt; set.Count); //Sanity Check: foreach (var group in enumerableGroups) { Console.Write("{"); foreach (var pair in group) Console.Write(string.Format(@"[{0},{1}]", pair.ToArray&lt;string&gt;())); Console.WriteLine("}"); } } } } </code></pre>
 

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