public static string QuineMcCluskeyAlg(WerteTabelle wt, AlgSourceMode algSourceMode = AlgSourceMode.K1608) { var minTerms = new List <QuineMcCluskeyRow>(); // generate minterm table for (int i = 0; i < wt.Array.GetLength(0); i++) { if (wt.Array[i, wt.Array.GetLength(1) - 1]) { minTerms.Add(new QuineMcCluskeyRow(wt.Array, i, i.ToString())); } } int initialMintermCount = minTerms.Count; minTerms.Sort(); var minTermArray = minTerms.ToArray(); while (QuineMcCluskeyRow.Step2(ref minTermArray)) { ; } return(QuineMcCluskeyRow.WesentlichePrimimplikanten(minTermArray, algSourceMode)); }
public CFGrammer ToChomskyNF(AlgSourceMode mode = AlgSourceMode.EAFK) { var finalVars = new List<char>(Variables); var newRS = Rules; Utils.DebugMessage($"0.before: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); if (mode == AlgSourceMode.EAFK) { newRS = newRS.RemoveUnusedSymbols(finalVars, Terminals); Utils.DebugMessage($"0.remove: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); } newRS = Chomskey1_StartSymbol(newRS, finalVars); Utils.DebugMessage($"1.starts: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); newRS = Chomskey2_Epsilon(newRS); Utils.DebugMessage($"2.epsilo: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); newRS = Chomskey3_UnitRules(newRS, finalVars); Utils.DebugMessage($"3.unitru: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); newRS = Chomskey4_Length(newRS, finalVars); Utils.DebugMessage($"4.length: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); newRS = Chomskey5_Terminals(newRS, finalVars); Utils.DebugMessage($"5.termin: {newRS}", this, Uni.Utils.eDebugLogLevel.Verbose); return new CFGrammer($"CCFG{mode}_({Name})", finalVars.ToArray(), Terminals, newRS, StartSymbol) { IsChomskey = true, org = this }; }
internal static string WesentlichePrimimplikanten(QuineMcCluskeyRow[] qmcRows, AlgSourceMode algSourceMode) { //generated index columns sorted var colIndexs = qmcRows .SelectMany(r => r.Index.Split(',')) .Distinct() .Select(a => System.Convert.ToInt32(a)) .OrderBy(a => a); var table = new int[qmcRows.Length + 1, colIndexs.Count() + 1]; //data with border desc +1 var matchColumIndex = new Dictionary <int, int>(); int mcIndexInc = 1; foreach (var ind in colIndexs) { matchColumIndex.Add(ind, mcIndexInc++); } // generate primeimplicanttable for (int r = 0; r < qmcRows.Length; r++) { foreach (var ind in (from i in qmcRows[r].Index.Split(',') select matchColumIndex[System.Convert.ToInt32(i)])) { table[r + 1, ind] = 1; } } // border description table[0, 0] = -1; foreach (var item in matchColumIndex) { table[0, item.Value] = item.Key; } for (int i = 1; i < table.GetLength(0); i++) { table[i, 0] = i - 1; } Utils.DebugMessage("Primimplikanttable:\n" + Utils.FormatArray(table), Utils.eDebugLogLevel.Normal); var processedIndicies = new List <int>(); if (algSourceMode == AlgSourceMode.K1608) { const int ROW_NOT_UNIQUE = -1; var Kernimplikanten = new List <int>(); // rows // find Kernimplikanten, column has only one unique true row for (int c = 1; c < table.GetLength(1); c++) { int pindex = ROW_NOT_UNIQUE; for (int r = 1; r < table.GetLength(0); r++) { if (table[r, c] == 1 && !processedIndicies.Contains(c)) { if (pindex == ROW_NOT_UNIQUE) { pindex = r; } else { pindex = ROW_NOT_UNIQUE; break; } } } // next r if (pindex != ROW_NOT_UNIQUE) { if (!Kernimplikanten.Contains(pindex)) { Kernimplikanten.Add(pindex); processedIndicies.AddRange( // matching indexes of qmcRows Indexes from ind in qmcRows[pindex - 1].Index.Split(',') select matchColumIndex[System.Convert.ToInt32(ind)] ); } } } // next c Utils.DebugMessage($"found Kernimplikanten: {string.Join(',', Kernimplikanten)}", Utils.eDebugLogLevel.Verbose); // add remaining unmatched rows for (int c = 1; c < table.GetLength(1); c++) { if (!processedIndicies.Contains(c)) { for (int r = 1; r < table.GetLength(0); r++) { if (table[r, c] == 1) { Kernimplikanten.Add(r); } } } } Utils.DebugMessage($"union implikanten: {string.Join(',', Kernimplikanten)}", Utils.eDebugLogLevel.Verbose); var sb = new System.Text.StringBuilder(); int i; for (i = 0; i < Kernimplikanten.Count - 1; i++) { sb.Append(qmcRowToString(qmcRows[Kernimplikanten[i] - 1]) + " | "); } sb.Append(qmcRowToString(qmcRows[Kernimplikanten[i] - 1])); return(sb.ToString()); } else if (algSourceMode == AlgSourceMode.Wiki) { Dominanz(ref table); var sb = new System.Text.StringBuilder(); int i; for (i = 0; i < table.GetLength(0) - 2; i++) { sb.Append(qmcRowToString(qmcRows[table[i + 1, 0]]) + " | "); } sb.Append(qmcRowToString(qmcRows[table[i + 1, 0]])); return(sb.ToString()); } else // invalid alg source { throw new System.NotSupportedException(); } }