/// <summary> /// Petrick's method (https://en.wikipedia.org/wiki/Petrick%27s_method) is too complicated for lab work programming. /// Thats why lets implement primitive method based on teacher's explanation. /// </summary> private static List <Implicant> GetMinimalDisjunctionNormalForm(List <Implicant> constituents, List <Implicant> implicants) { var coreImplicants = new HashSet <Implicant>(); foreach (Implicant implicant in implicants) { foreach (Implicant constituent in constituents) { List <Implicant> implicantsInConstituent = implicants.Where(i => i.IsSubsetOf(constituent)).ToList(); if (implicantsInConstituent.Count == 1 && implicantsInConstituent.Contains(implicant)) { coreImplicants.Add(implicant); continue; } } } List <Implicant> noncoreImplicants = implicants.Except(coreImplicants).ToList(); List <Implicant> notCoveredConstituents = constituents .Where(c => !coreImplicants.Any(coreImplicant => coreImplicant.IsSubsetOf(c))).ToList(); List <Implicant> resultImplicants = coreImplicants.ToList(); while (notCoveredConstituents.Count > 0) { noncoreImplicants = noncoreImplicants .OrderBy(i => notCoveredConstituents.Where(c => i.IsSubsetOf(c)).Count()).ToList(); Implicant noncoreTopRatedImplicant = noncoreImplicants[noncoreImplicants.Count - 1]; noncoreImplicants.RemoveAt(noncoreImplicants.Count - 1); bool covers = false; for (int i = notCoveredConstituents.Count - 1; i >= 0; i--) { if (noncoreTopRatedImplicant.IsSubsetOf(notCoveredConstituents[i])) { notCoveredConstituents.RemoveAt(i); covers = true; } } if (covers) { resultImplicants.Add(noncoreTopRatedImplicant); } } return(resultImplicants); }
/// <param name="implicants">Implicants ordered from short to long.</param> private static List <Implicant> AbsorpImplicantsToShortDisjunctionNormalForm(List <Implicant> implicants) { List <Implicant> resultList = implicants.ToList(); for (int absorperIndex = 0; absorperIndex < resultList.Count - 1; absorperIndex++) { Implicant absorper = resultList[absorperIndex]; for (int absorpedIndex = resultList.Count - 1; absorpedIndex > absorperIndex; absorpedIndex--) { Implicant absorped = resultList[absorpedIndex]; if (absorper.IsSubsetOf(absorped)) { resultList.RemoveAt(absorpedIndex); } } } return(resultList); }