Skip to content

Commit bb09a41

Browse files
committed
feat(logic): implement Quine-McCluskey algorithm for prime implicants and implicates with minimal DNF/CNF conversion
1 parent e7e75ef commit bb09a41

6 files changed

Lines changed: 891 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@
298298
ce.box(['Or', 'A', ['And', 'A', 'B']]).simplify(); // → A
299299
```
300300

301+
- **Prime Implicants and Minimal Normal Forms**: Added Quine-McCluskey algorithm
302+
for finding prime implicants/implicates and computing minimal CNF/DNF:
303+
- `PrimeImplicants(expr)` - Find all prime implicants (minimal product terms)
304+
- `PrimeImplicates(expr)` - Find all prime implicates (minimal sum clauses)
305+
- `MinimalDNF(expr)` - Convert to minimal DNF using prime implicant cover
306+
- `MinimalCNF(expr)` - Convert to minimal CNF using prime implicate cover
307+
308+
```javascript
309+
// Find prime implicants (terms that can't be further simplified)
310+
ce.box(['PrimeImplicants', ['Or', ['And', 'A', 'B'], ['And', 'A', ['Not', 'B']]]]).evaluate();
311+
// → [A] (AB and A¬B combine to just A)
312+
313+
// Compute minimal DNF
314+
ce.box(['MinimalDNF', ['Or',
315+
['And', 'A', 'B'],
316+
['And', 'A', ['Not', 'B']],
317+
['And', ['Not', 'A'], 'B']
318+
]]).evaluate();
319+
// → A ∨ B (simplified from 3 terms to 2)
320+
```
321+
322+
Limited to 12 variables to prevent exponential blowup; larger expressions
323+
return unevaluated.
324+
301325
#### Linear Algebra
302326

303327
- **Matrix Decompositions**: Added four matrix decomposition functions for

requirements/DONE.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,63 @@ ce.box(['Not', ['Not', 'A']]).simplify(); // → A
480480

481481
---
482482

483+
### L-2. Prime Implicants/Implicates (Quine-McCluskey) ✅
484+
485+
**IMPLEMENTED:** Added functions for finding prime implicants/implicates and
486+
computing minimal normal forms using the Quine-McCluskey algorithm:
487+
488+
1. **`PrimeImplicants(expr)`** - Find all prime implicants (minimal product terms):
489+
```typescript
490+
ce.box(['PrimeImplicants', ['Or', ['And', 'A', 'B'], ['And', 'A', ['Not', 'B']]]]).evaluate();
491+
// → [A] (AB and A¬B combine to just A)
492+
493+
ce.box(['PrimeImplicants', ['Or', 'A', 'B']]).evaluate();
494+
// → [A, B] (two prime implicants)
495+
```
496+
497+
2. **`PrimeImplicates(expr)`** - Find all prime implicates (minimal sum clauses):
498+
```typescript
499+
ce.box(['PrimeImplicates', ['And', 'A', 'B']]).evaluate();
500+
// → [A, B] (the expression implies both A and B)
501+
502+
ce.box(['PrimeImplicates', ['Or', 'A', 'B']]).evaluate();
503+
// → [A ∨ B] (single prime implicate)
504+
```
505+
506+
3. **`MinimalDNF(expr)`** - Convert to minimal Disjunctive Normal Form:
507+
```typescript
508+
ce.box(['MinimalDNF', ['Or',
509+
['And', 'A', 'B'],
510+
['And', 'A', ['Not', 'B']],
511+
['And', ['Not', 'A'], 'B']
512+
]]).evaluate();
513+
// → A ∨ B (simplified from 3 terms to 2 prime implicants)
514+
```
515+
516+
4. **`MinimalCNF(expr)`** - Convert to minimal Conjunctive Normal Form:
517+
```typescript
518+
ce.box(['MinimalCNF', ['And', ['Or', 'A', 'B'], ['Or', 'A', ['Not', 'B']]]]).evaluate();
519+
// → A (simplified to single literal)
520+
```
521+
522+
**Algorithm:** Quine-McCluskey with greedy covering:
523+
1. Generate minterms (for DNF) or maxterms (for CNF) from truth table
524+
2. Iteratively combine terms differing in exactly one variable
525+
3. Identify essential prime implicants
526+
4. Use greedy algorithm to find minimal cover
527+
528+
**Performance:**
529+
- O(3^n) worst case complexity
530+
- Limited to 12 variables to prevent exponential blowup
531+
- Expressions with more variables return unevaluated
532+
533+
**Files modified:**
534+
- `src/compute-engine/library/logic-analysis.ts` - Implemented Quine-McCluskey algorithm
535+
- `src/compute-engine/library/logic.ts` - Added 4 new function definitions
536+
- `test/compute-engine/logic.test.ts` - Added 17 new tests in "Prime Implicants and Minimal Forms" describe block
537+
538+
---
539+
483540
## Medium Priority (Completed)
484541

485542
### 9. Implicit Multiplication Between `\exp` Function Calls ✅

requirements/TODO.md

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,9 @@ ce.box(['IsEquivalent',
123123

124124
---
125125

126-
### 6. Prime Implicants/Implicates
126+
### ~~6. Prime Implicants/Implicates~~ ✅ COMPLETED
127127

128-
**Problem:** For circuit minimization and optimal CNF/DNF, we need minimal
129-
representations.
130-
131-
**Solution:** Add functions:
132-
133-
- `PrimeImplicants(expr)` - Find all prime implicants (minimal terms in DNF)
134-
- `PrimeImplicates(expr)` - Find all prime implicates (minimal clauses in CNF)
135-
- `MinimalDNF(expr)` - DNF with only prime implicants (Quine-McCluskey)
136-
- `MinimalCNF(expr)` - CNF with only prime implicates
137-
138-
```typescript
139-
ce.box(['MinimalDNF', ['Or',
140-
['And', 'A', 'B'],
141-
['And', 'A', ['Not', 'B']],
142-
['And', ['Not', 'A'], 'B']
143-
]]).evaluate()
144-
// → A ∨ B (simplified from 3 terms to 1)
145-
```
146-
147-
**Algorithm:** Quine-McCluskey or Espresso for larger formulas.
148-
149-
**File:** `src/compute-engine/library/logic.ts`
128+
See `requirements/DONE.md` for implementation details.
150129

151130
---
152131

0 commit comments

Comments
 (0)