-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuineMcCluskey.java
638 lines (515 loc) · 21.8 KB
/
QuineMcCluskey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import java.util.LinkedList;
import java.util.Arrays;
import java.util.Collections;
/**
* A class to handle processing the Quine-McCluskey Algorithm
*/
public class QuineMcCluskey {
/**
* An object to hold information about a minterm when using the Quine-McCluskey Algorithm
*/
public class Minterm implements Comparable<Minterm> {
// Instance Fields
private int[] values;
private String value;
private boolean used;
// Constructor
/**
* Creates a new Minterm object
*
* @param values The values this Minterm covers
* @param value The bit value for this Minterm
*/
public Minterm(int[] values, String value) {
this.values = values;
this.value = value;
this.used = false;
// Sort the values in ascending order (bubble sort is okay in this case)
for (int i = 0; i < values.length - 1; i++)
for (int j = i + 1; j < values.length; j++)
if (values[j] < values[i]) {
int temp = values[j];
values[j] = values[i];
values[i] = temp;
}
}
/**
* Returns a String representation of the Minterm.
*/
public String toString() {
String value = "";
for (int i = 0; i < this.values.length; i++) {
value += this.values[i];
if (i < this.values.length - 1)
value += ", ";
}
return String.format(
"m(%s) = %s",
value, this.value
);
}
/**
* Determines if this Minterm object is equal to another object.
*/
public boolean equals(Object object) {
if (! (object instanceof Minterm))
return false;
Minterm minterm = (Minterm) object;
return (
Arrays.equals(minterm.values, this.values) &&
this.value.equals(minterm.value)
);
}
public int compareTo(Minterm minterm) {
// Lengths of values are the same
if (this.values.length == minterm.values.length) {
// Compare by each value
for (int i = 0; i < this.values.length; i++)
if (this.values[i] < minterm.values[i])
return -1;
else if (this.values[i] > minterm.values[i])
return 0;
// Minterm is the same; Compare value
return this.value.compareTo(minterm.value);
}
return this.values.length - minterm.values.length;
}
// Getters
/**
* Returns the values in this Minterm.
*
* @returns int[]
*/
public int[] getValues() {
return values;
}
/**
* Returns the value of this Minterm.
*
* @returns String
*/
public String getValue() {
return value;
}
/**
* Returns whether or not this Minterm has been used.
*
* @returns boolean
*/
public boolean used() {
return used;
}
// Setters
/**
* Labels this Minterm as "used"
*/
public void use() {
used = true;
}
// Other Methods
/**
* Combines 2 Minterms together if they can be combined
*
* @returns Minterm
*/
public Minterm combine(Minterm minterm) {
// Check if the value is the same; If so, do nothing
if (this.value.equals(minterm.value))
return null;
// Check if the values are the same; If so, do nothing
if (Arrays.equals(this.values, minterm.values))
return null;
// Keep track of the difference between the minterms
int diff = 0;
String result = "";
// Iterate through the bits in this Minterm's value
for (int i = 0; i < this.value.length(); i++) {
// Check if the current bit value differs from the minterm's bit value
if (this.value.charAt(i) != minterm.value.charAt(i)) {
diff += 1;
result += "-";
}
// There is not a difference
else
result += this.value.charAt(i);
// The difference has exceeded 1
if (diff > 1)
return null;
}
// Combine the values of this Minterm with the values of minterm
int[] newValues = new int[this.values.length + minterm.values.length];
for (int i = 0; i < this.values.length; i++)
newValues[i] = this.values[i];
for (int i = 0; i < minterm.values.length; i++)
newValues[i + this.values.length] = minterm.values[i];
return new Minterm(newValues, result);
}
}
// Instance Fields
private String[] variables;
private int[] values;
private String function;
// Constructor
/**
* Creates a new QM object to process the Quine-McCluskey Algorithm
*/
public QuineMcCluskey(String[] variables, int[] values) {
this.variables = variables;
this.values = values;
this.function = getFunction(false);
}
// Helper Methods
/**
* Returns the binary representation of the specified value.
*
* @param value The int value to turn into a binary digit.
* @returns String
*/
private String getBits(int value) {
// Convert the value into a binary number
// Then add extra 0's to the beginning to match how many variables there are
String result = Integer.toBinaryString(value);
for (int i = result.length(); i < this.variables.length; i++)
result = "0" + result;
return result;
}
// Grouping Methods
/**
* Creates the initial grouping for the bits from the values
* given to the Quine-McCluskey Algorithm.
*
* @returns Minterm[][]
*/
private Minterm[][] initialGroup() {
// Keep track of groups by an array of linked lists
LinkedList<Minterm>[] groups = new LinkedList[this.variables.length + 1];
for (int i = 0; i < groups.length; i++)
groups[i] = new LinkedList<Minterm>();
// Iterate through values
for (int value: this.values) {
// Count number of 1's in value's bit equivalent
int count = 0;
String bits = getBits(value);
for (int i = 0; i < bits.length(); i++)
if (bits.charAt(i) == '1')
count += 1;
// Add count to proper group
groups[count].add(new Minterm(new int[] {value}, bits));
}
// Turn the groups into 2-dimensional array
Minterm[][] groupsArray = new Minterm[groups.length][];
for (int i = 0; i < groups.length; i++) {
groupsArray[i] = new Minterm[groups[i].size()];
for (int j = 0; j < groups[i].size(); j++)
groupsArray[i][j] = groups[i].get(j);
}
return groupsArray;
}
/**
* Creates a power set of all valid prime implicants that covers the rest of an expression.
* This is used after the essential prime implicants have been found.
*
* @param values An array of int's that the power set must cover.
* @param primeImplicants An array of Minterms for the prime implicants to check for.
* @returns Minterm[]
*/
private Minterm[] powerSet(int[] values, Minterm[] primeImplicants) {
if (primeImplicants.length == 0)
return new Minterm[] {};
// Get the power set of all the prime implicants
LinkedList<LinkedList<Minterm>> powerset = new LinkedList<>();
// Iterate through the decimal values from 1 to 2 ** size - 1
for (int i = 1; i < (int)(Math.pow(2, primeImplicants.length)); i++) {
LinkedList<Minterm> currentset = new LinkedList<>();
// Get the binary value of the decimal value
String binValue = Integer.toBinaryString(i);
for (int j = binValue.length(); j < primeImplicants.length; j++)
binValue = "0" + binValue;
// Find which indexes have 1 in the binValue string
for (int index = 0; index < binValue.length(); index++)
if (binValue.charAt(index) == '1')
currentset.add(primeImplicants[index]);
powerset.add(currentset);
}
// Remove all subsets that do not cover the rest of the implicants
LinkedList<Integer> valuesLinkedList = new LinkedList<>();
for (int i = 0; i < values.length; i++)
valuesLinkedList.add(values[i]);
LinkedList<LinkedList<Minterm>> newPowerset = new LinkedList<>();
for (LinkedList<Minterm> subset: powerset) {
// Get all the values the set covers
LinkedList<Integer> tempValues = new LinkedList<>();
for (Minterm implicant: subset)
for (int value: implicant.getValues())
if (!tempValues.contains(value) && valuesLinkedList.contains(value))
tempValues.add(value);
// Turn the LinkedList into an array
int[] tempValuesArray = new int[tempValues.size()];
for (int i = 0; i < tempValues.size(); i++)
tempValuesArray[i] = tempValues.get(i);
// Sort tempValuesArray
for (int i = 0; i < tempValuesArray.length; i++)
for (int j = i; j < tempValuesArray.length; j++)
if (tempValuesArray[j] < tempValuesArray[i]) {
int temp = tempValuesArray[i];
tempValuesArray[i] = tempValuesArray[j];
tempValuesArray[j] = temp;
}
// Check if this subset covers the rest of the values
if (Arrays.equals(tempValuesArray, values))
newPowerset.add(subset);
}
powerset = newPowerset;
// Find the minimum amount of implicants that can cover the expression
LinkedList<Minterm> minSet = powerset.get(0);
for (LinkedList<Minterm> subset: powerset)
if (subset.size() < minSet.size())
minSet = subset;
// Turn the minSet into an array
Minterm[] minSetArray = new Minterm[minSet.size()];
for (int i = 0; i < minSet.size(); i++)
minSetArray[i] = minSet.get(i);
return minSetArray;
}
// Compare Methods
/**
* Returns an array of all the prime implicants for the expression.
*
* @returns Minterm[]
*/
private Minterm[] getPrimeImplicants() {
return getPrimeImplicants(initialGroup());
}
/**
* Returns an array of all the prime implicants for the expression.
*
* @param groups A 2-dimensional array of minterms separated into groups
* @returns Minterm[]
*/
private Minterm[] getPrimeImplicants(Minterm[][] groups) {
// If there is only 1 group, return all minterms in it
if (groups.length == 1)
return groups[0];
// Try comparing the rest
else {
// Only run this if groups.length - 1 is greater than 0
if (groups.length - 1 <= 0)
return new Minterm[] {};
LinkedList<Minterm> unused = new LinkedList<Minterm>();
int[] comparisons = new int[(groups.length - 1 > 0)? groups.length - 1: 0];
for (int i = 0; i < comparisons.length; i++)
comparisons[i] = i;
LinkedList<Minterm>[] newGroups = new LinkedList[comparisons.length];
for (int i = 0; i < newGroups.length; i++)
newGroups[i] = new LinkedList<Minterm>();
for (int compare: comparisons) {
Minterm[] group1 = groups[compare];
Minterm[] group2 = groups[compare + 1];
// Compare every term in group1 with every term in group2
for (Minterm term1: group1)
for (Minterm term2: group2) {
// Try combining it
Minterm term3 = term1.combine(term2);
// Only add it to the new group if term3 is not null
// term3 will only be null if term1 and term2 could not
// be combined
if (term3 != null) {
term1.use();
term2.use();
if (! newGroups[compare].contains(term3))
newGroups[compare].add(term3);
}
}
}
// Turn the newGroups into a 2-dimensional array
Minterm[][] newGroupsArray = new Minterm[newGroups.length][];
for (int i = 0; i < newGroups.length; i++) {
newGroupsArray[i] = new Minterm[newGroups[i].size()];
for (int j = 0; j < newGroups[i].size(); j++)
newGroupsArray[i][j] = newGroups[i].get(j);
}
// Add unused minterms
for (Minterm[] group: groups)
for (Minterm term: group)
if (!term.used() && !unused.contains(term))
unused.add(term);
// Add recursive call
for (Minterm term: getPrimeImplicants(newGroupsArray))
if (!term.used() && !unused.contains(term))
unused.add(term);
// Turn the unused into an array
Minterm[] unusedArray = new Minterm[unused.size()];
for (int i = 0; i < unused.size(); i++)
unusedArray[i] = unused.get(i);
return unusedArray;
}
}
// Solving Methods
/**
* Solves for the expression returning the minimal amount of prime implicants needed
* to cover the expression.
*
* @returns Minterm[]
*/
private Minterm[] solve() {
// Get the prime implicants
Minterm[] primeImplicants = getPrimeImplicants();
// Keep track of values with only 1 implicant
// These are the essential prime implicants
LinkedList<Minterm> essentialPrimeImplicants = new LinkedList<Minterm>();
boolean[] valuesUsed = new boolean[this.values.length];
for (int i = 0; i < this.values.length; i++)
valuesUsed[i] = false;
for (int i = 0; i < values.length; i++) {
int value = values[i];
// Count how many times the current minterm value is used
int uses = 0;
Minterm last = null;
for (Minterm minterm: primeImplicants) {
boolean found = false;
for (int j = 0; j < minterm.getValues().length; j++)
if (value == minterm.getValues()[j]) {
found = true;
break;
}
if (found) {
uses += 1;
last = minterm;
}
}
// If there is only 1 use, this is an essential prime implicant
if (uses == 1 && !essentialPrimeImplicants.contains(last)) {
for (int lv = 0; lv < last.getValues().length; lv++)
for (int v = 0; v < values.length; v++)
if (last.getValues()[lv] == values[v]) {
valuesUsed[v] = true;
break;
}
essentialPrimeImplicants.add(last);
}
}
// Turn the essentialPrimeImplicants into an array
Minterm[] essentialPrimeImplicantsArray = new Minterm[essentialPrimeImplicants.size()];
for (int i = 0; i < essentialPrimeImplicants.size(); i++) {
essentialPrimeImplicantsArray[i] = essentialPrimeImplicants.get(i);
}
// Check if all values were used
boolean found = false;
for (int i = 0; i < valuesUsed.length; i++)
if (valuesUsed[i]) {
found = true;
break;
}
// If all values were used, return the essential prime implicants
if (!found) {
return essentialPrimeImplicantsArray;
}
// Keep track of prime implicants that cover as many values as possible
LinkedList<Minterm> newPrimeImplicants = new LinkedList<>();
for (int i = 0; i < primeImplicants.length; i++)
if (!essentialPrimeImplicants.contains(primeImplicants[i]))
newPrimeImplicants.add(primeImplicants[i]);
// Turn the new prime implicants into an array
primeImplicants = new Minterm[newPrimeImplicants.size()];
for (int i = 0; i < newPrimeImplicants.size(); i++) {
primeImplicants[i] = newPrimeImplicants.get(i);
}
// Check if there is only 1 implicant left (very rare but just in case)
if (primeImplicants.length == 1) {
Minterm[] finalResult = new Minterm[
essentialPrimeImplicantsArray.length + primeImplicants.length
];
for (int i = 0; i < essentialPrimeImplicantsArray.length; i++)
finalResult[i] = essentialPrimeImplicantsArray[i];
for (int i = 0; i < primeImplicants.length; i++)
finalResult[essentialPrimeImplicantsArray.length + i] = primeImplicants[i];
return finalResult;
}
// Create a power set from the remaining prime implicants and check which
// combination of prime implicants gets the simplest form
LinkedList<Integer> valuesLeftLinkedList = new LinkedList<>();
for (int i = 0; i < valuesUsed.length; i++)
if (!valuesUsed[i])
valuesLeftLinkedList.add(values[i]);
// Turn the values left into an array
int[] valuesLeft = new int[valuesLeftLinkedList.size()];
for (int i = 0; i < valuesLeftLinkedList.size(); i++) {
valuesLeft[i] = valuesLeftLinkedList.get(i);
}
// Get the power set
Minterm[] powerset = powerSet(valuesLeft, primeImplicants);
// Get the final result
Minterm[] finalResult = new Minterm[
essentialPrimeImplicantsArray.length + powerset.length
];
for (int i = 0; i < essentialPrimeImplicantsArray.length; i++)
finalResult[i] = essentialPrimeImplicantsArray[i];
for (int i = 0; i < powerset.length; i++)
finalResult[essentialPrimeImplicantsArray.length + i] = powerset[i];
return finalResult;
}
/**
* Returns the expression in a readable form.
*/
public String getFunction() {
return function;
}
/**
* Returns the expression in a readable form.
*/
private String getFunction(boolean saveVariable) {
// Get the prime implicants and variables
Minterm[] primeImplicants = solve();
// Check if there are no prime implicants; Always False
if (primeImplicants.length == 0)
return "0";
// Check if there is only 1 prime implicant
else if (primeImplicants.length == 1) {
// Now check if there are just as many hyphens (-) as there are variables
int hyphens = 0;
for (int i = 0; i < primeImplicants[0].getValue().length(); i++)
if (primeImplicants[0].getValue().charAt(i) == '-')
hyphens += 1;
if (hyphens == variables.length)
return "1";
}
String result = "";
// Iterate through the prime implicants
for (int i = 0; i < primeImplicants.length; i++) {
Minterm implicant = primeImplicants[i];
// Determine if parentheses should be added to each minterm's expression
int hyphens = 0;
boolean addParenthesis = false;
for (int j = 0; j < implicant.getValue().length(); j++)
if (implicant.getValue().charAt(j) == '-')
hyphens += 1;
if (hyphens < this.variables.length - 1)
addParenthesis = true;
// Add parenthesis if necessary
if (addParenthesis)
result += "(";
// Iterate through all bits in the implicants value
for (int j = 0; j < implicant.getValue().length(); j++) {
String character = String.valueOf(implicant.getValue().charAt(j));
if (character.equals("0"))
result += "NOT ";
if (!character.equals("-"))
result += variables[j];
// Make sure there are no more hyphens
hyphens = 0;
for (int k = j + 1; k < implicant.getValue().length(); k++)
if (implicant.getValue().charAt(k) == '-')
hyphens += 1;
if ((hyphens < implicant.getValue().length() - j - 1) && !character.equals("-"))
result += " AND ";
}
// Add parenthesis if necessary
if (addParenthesis)
result += ")";
// Combine minterm expressions with an OR statement
if (i < primeImplicants.length - 1)
result += " OR ";
}
return result;
}
}