public static List <ThresholdSubset> findThreshold(IEnumerable <QualifiedSubset> candidateSets, int k, int allparties, int NRSFT, bool findIntersection = true) { int allsentences = candidateSets.Count(); //add trace attemptTrace.Add(string.Format("Set:{0}, Attempted threshold:({1},{2})", QualifiedSubset.DumpElementsIntoSingleString(candidateSets), k, allparties)); // normal case List <ThresholdSubset> thresholds = new List <ThresholdSubset>(); var ncr = nCr(allparties, k); if (ncr == allsentences) { ThresholdSubset th = new ThresholdSubset(allparties, k, new List <Trustee>(), QualifiedSubset.GetAllInvolvedParties(candidateSets).Parties); thresholds.Add(th); } /// if the candidate set is larger than nCr then probably some sets don't belong to the threshold /// Here we recusrively rotate parties and select all possible combinations to see which ones are /// building threshold for e.g. p1p2p3 p2p3p4 p1p3p4 p1p2p4 p2p4p5 else if (allsentences < ncr) { if (NRSFT > allsentences) { NRSFT = GetNumberOfRequiredSetsForThreshold(allparties, k, allsentences); } if (NRSFT == 1) { return(thresholds); } //all possible candidate sets must be generated and recursively called var smallersetcombinations = candidateSets.Combinations(NRSFT); var subsetsAlreadyHaveThreshold = new List <QualifiedSubset>(); foreach (var smallerset in smallersetcombinations) { var newallparties = QualifiedSubset.GetAllInvolvedParties(smallerset).Parties.Count; List <ThresholdSubset> foundthresholds = new List <ThresholdSubset>(); //add trace attemptTrace.Add(string.Format("Set:{0}, Attempted threshold:({1},{2})", QualifiedSubset.DumpElementsIntoSingleString(smallerset), k, newallparties)); if (nCr(newallparties, k) == smallerset.Count() && k < newallparties)//not interested in k=n thresholds { foundthresholds = findThreshold(smallerset, k, newallparties, NRSFT, findIntersection); if (foundthresholds.Count > 0) { subsetsAlreadyHaveThreshold.AddRange(smallerset); } } ///try filtering the fixed sets maybe find a threshold if (foundthresholds.Count == 0 && findIntersection) { foundthresholds = findFixedConcatThreshold(smallerset, k, newallparties, NRSFT); } if (foundthresholds != null) { thresholds.AddRange(foundthresholds); } } if (--NRSFT > 1) { #if filterThresholds var minusAlreadyFoundSets = candidateSets.Except(subsetsAlreadyHaveThreshold); var minusSentences = minusAlreadyFoundSets.Count(); if (minusSentences < allsentences) { var minusInvolvedParties = QualifiedSubset.GetAllInvolvedParties(minusAlreadyFoundSets).Parties.Count; //calculate new NRSFT //NRSFT = GetNumberOfRequiredSetsForThreshold(minusInvolvedParties, k, minusAlreadyFoundSets.Count()); //if (NRSFT == 1) return thresholds; } thresholds.AddRange(findThreshold(minusAlreadyFoundSets, k, allparties, NRSFT, findIntersection)); #else thresholds.AddRange(findThreshold(candidateSets, k, allparties, NRSFT, findIntersection)); #endif } } return(thresholds); }
public static List <ThresholdSubset> findFixedConcatThreshold(IEnumerable <QualifiedSubset> candidateSets, int k, int allparties, int NRSFT) { int allsentences = candidateSets.Count(); if (allsentences < 2) { return(null); } //fixed element normal case : just a fixed element in front of threshold candidates var fixedIntersection = candidateSets.Select(po => po.Parties).Aggregate((first, second) => first.Intersect(second).ToList()); //add trace fixedAttemptTrace.Add(string.Format("Set:{0}, Attempted fixed item:{1} + threshold:({2},{3})", QualifiedSubset.DumpElementsIntoSingleString(candidateSets) , new QualifiedSubset(fixedIntersection).ToString(), k, allparties)); if (fixedIntersection.Count > 0) { var smallerQS = new List <QualifiedSubset>(); //remove fixed part from parties and try to find normal threshold foreach (var qs in candidateSets) { var withoutFixedParties = qs.Parties.Except(fixedIntersection); if (withoutFixedParties.Count() > 0) { var tempqs = new QualifiedSubset(withoutFixedParties); smallerQS.Add(tempqs); } } var smallerQSinvolvedParties = QualifiedSubset.GetAllInvolvedParties(smallerQS).Parties.Count; var smallerK = k - fixedIntersection.Count; if (nCr(smallerQSinvolvedParties, smallerK) == allsentences) { var threshold = findThreshold(smallerQS, smallerK, smallerQSinvolvedParties, NRSFT, false); if (threshold != null) //now add the fixed part to the threshold { foreach (var th in threshold) { th.fixedParties = fixedIntersection; } return(threshold); } } } return(new List <ThresholdSubset>()); }