private static void Rec(List <StateInfo> As, List <StateInfo> Bs, BidirectionalDictionary <StateInfo, StateInfo> Correspondences) { if (As.Count > 0) { var a = As[0]; var b = a.GetFirst(Bs); if (b != null) { //Console.WriteLine("{0} - {1}", a, b); Correspondences.AddAssociation(a, b); Rec(new List <StateInfo>(As.Skip(1)), new List <StateInfo>(Bs.Where(s => s.State != b.State)), Correspondences); a.Rejected.Add(b.State); Correspondences.RemoveAssociationFirst(a); foreach (var aa in As.Skip(1)) { aa.Rejected.Clear(); } Rec(As, Bs, Correspondences); } } else { Console.WriteLine("--------"); foreach (var pair in Correspondences) { Console.WriteLine("{0} - {1}", pair.Key.State, pair.Value.State); } Console.WriteLine("--------"); } }