//************************* FUNCTION GetEssentialPrimes ************************ BoolExpr GetEssentialPrimes(PrimeImplChart &chart, BoolExpr &minterms, BoolExpr &prime_implicants) { /* * Receives: the inputted minterms for the expression to minimze * Task: Get the minimized expression of minterms * Returns: the essential primes as a BoolExpr */ int num_primes = prime_implicants.size(); int num_minterms = minterms.size(); BoolExpr essentials; // Check each column for a lone X; this means that this prime implicant // is essential; the expression cannot be built without it for (int minterm = 0; minterm < num_minterms; ++minterm) { int lone_position = -1; for (int prime = 0; prime < num_primes; ++prime) { if (chart[prime][minterm] == 1) { // Check if there's been an X found already if (lone_position == -1) { // Circle the X to mark the row as an essential PI chart[prime][minterm] = 2; // Save the location of the circled X lone_position = prime; } else { // Another X was found; uncircle the previous X chart[lone_position][minterm] = 1; // Reset lone_position to signify that there are multiple Xs lone_position = -1; // End search through this column break; } } } if (lone_position != -1) { // This prime is essential; add it to essentials if it is not // already added auto essential_term = prime_implicants[lone_position]; bool not_added = true; for (auto term = essentials.begin(); term != essentials.end(); ++term) { if (*term == essential_term) { not_added = false; break; } } if (not_added) essentials.push_back(essential_term); } } return essentials; }
//************************* FUNCTION MakePrimeImplChart ************************ PrimeImplChart MakePrimeImplChart(BoolExpr minterms, BoolExpr prime_implicants) { /* * Receives: the inputted minterms for the expression to minimze * Task: Get the minimized expression of minterms * Returns: the essential primes as a BoolExpr */ PrimeImplChart prime_impl_chart(prime_implicants.size(), PrimeImplCol(minterms.size(), 0)); // Mark the cells where the prime implicant is a subset of the minterm MarkSubsets(prime_impl_chart, minterms, prime_implicants); return prime_impl_chart; }
//************************* FUNCTION FindPrimeImplicants *********************** BoolExpr FindPrimeImplicants(BoolExpr bool_expr) { /* * Receives: the inputted minterms for the expression to minimze * Task: Get the minimized expression of minterms * Returns: the essential primes as a BoolExpr */ BoolExpr prime_implicants; do { // Sort the terms into groups based on their number of complements auto grouped_terms = BucketSortTerms(bool_expr); //std::cout << PrintTermBuckets(grouped_terms) << std::endl; // Compare the groups with each other to reduce the terms auto terms_pair = CompareTermBuckets(grouped_terms); // bool_expr will contain the terms that will be sorted into buckets // next iteration (if there are still terms to compare) bool_expr = terms_pair.first; // Concatenate the unused terms to the list of prime implicants to return prime_implicants = CombineBoolExprs(prime_implicants, terms_pair.second); // Continue while there are still terms to compare } while (bool_expr.size()); // Return the list of prime implicants return prime_implicants; }
//************************* FUNCTION MarkSubsets ******************************* void MarkSubsets(PrimeImplChart &chart, BoolExpr &minterms, BoolExpr &prime_implicants) { /* * Receives: the inputted minterms for the expression to minimze * Task: Get the minimized expression of minterms * Returns: the essential primes as a BoolExpr */ int num_primes = prime_implicants.size(); int num_minterms = minterms.size(); // Compare each prime implicant to each minterm to see if it's a subset for (int i = 0; i < num_primes; ++i) { for (int j = 0; j < num_minterms; ++j) { if (IsSubset(minterms[j], prime_implicants[i])) { chart[i][j] = 1; } } } }