-
Notifications
You must be signed in to change notification settings - Fork 0
/
karnaugh.js
220 lines (207 loc) · 7.31 KB
/
karnaugh.js
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
class karnaugh {
MaxLength = 4;
MaxNumber = 16; // 2 ^ MaxLength
chars = ["A", "B", "C", "D", "E", "F"]; // chars.length = MaxLength
// Input: Binary Code (String)
// Output: Number of 1's in binary code (Number)
bitNum(bits) {
let n = 0;
for (let i = 0; i < bits.length; i ++) {
if (bits[i] == "1") n += 1;
}
return n;
}
// Input: Decimal(Number)
// Output: binary code(String)
binaryify(m) {
let i = this.MaxNumber;
let str = "";
while (i >>= 1) {
if (m & i) str += "1";
else str += "0";
}
return str;
}
// Input: binary code(String)
// Output: Decimal(Number)
decimalify(bits) {
let n = this.MaxNumber;
let i = 0;
let ans = 0;
while (n >>= 1) {
if (bits[i] == 1)
ans += n;
i += 1;
}
return ans;
}
// Input: Binary code with "-" (String)
// Output: Boolean monomal Text Formula (String)
termify(bits) {
let index = 0;
let str = "";
for (let i = 0; i < bits.length; i ++) {
if (bits[i] == "0") str += "\\bar ";
if (bits[i] != "-") str += this.chars[index];
index += 1;
}
return str.length ? str : "1";
}
termify_POS(bits) {
let index = 0;
let str = "";
for (let i = 0; i < bits.length; i ++) {
if (str.length && bits[i] != "-") str += "+";
if (bits[i] == "1") str += "\\bar ";
if (bits[i] != "-") str += this.chars[index];
index += 1;
}
return str.length ? `(${str})` : "0";
}
// Input: A Decimal array containing all the smallest (String)
// Output: Terms contained (Number[])
minimumTermify(bits) {
if (bits.length == 0) return [];
let terms = [""];
let termsTemp = new Array();
for (let i = 0; i < bits.length; i ++) {
if (bits[i] == "-") terms.forEach(u => {
termsTemp.push(u + "1");
termsTemp.push(u + "0");
});
else terms.forEach(u => {
termsTemp.push(u + bits[i]);
});
terms = termsTemp;
termsTemp = new Array();
}
return terms.map(u => this.decimalify(u));
}
// Attempt to merge u and v, return an empty string indicating that the merge failed
// Input: Contains "-" binary code (String)
// Return: Contains "-" binary code (String)
merge(u, v) {
let flag = false;
let str = "";
for (let i = 0; i < u.length; i ++) {
if (u[i] != v[i])
if (flag) return false;
else {
flag = true;
str += "-";
}
else str += u[i];
}
return str;
}
// 用 Quine-McCluskey Algorithm Simplifies Several Minimal Terms
// Input: Decimal array (Number[])
// Return: Array of binary codes with "-" (String[])
simplify(ones, dontcares) {
let group = new Array();
let groupTemp = new Array();
for (let i = 0; i <= this.MaxLength; i ++) {
group[i] = new Map();
groupTemp[i] = new Map();
}
let result = new Set();
ones.forEach(num => {
let bits = this.binaryify(num);
group[this.bitNum(bits)].set(bits, false);
});
dontcares.forEach(num => { // Irrelevant items also participate in merging, but do not participate in subsequent prime item table simplification
let bits = this.binaryify(num);
group[this.bitNum(bits)].set(bits, false);
});
while (true) {
let hasMerged = false;
// Attempt to merge two items of adjacent groups
for (let i = 0; i < this.MaxLength; i ++)
for (let u of group[i])
for (let v of group[i + 1]) {
let merged = this.merge(u[0], v[0]);
if (merged) {
hasMerged = true;
groupTemp[this.bitNum(merged)].set(merged, false);
group[i].set(u[0], true);
group[i + 1].set(v[0], true);
}
}
// Add unmarked "prime implicants" to the result list
for (let i = 0; i <= this.MaxLength; i ++)
for (let u of group[i])
if (u[1] == false)
result.add(u[0]);
// If the merge is unsuccessful, the loop ends
if (!hasMerged) break;
group = groupTemp;
groupTemp = new Array();
for (let i = 0; i <= this.MaxLength; i ++)
groupTemp[i] = new Map();
}
return Array.from(result);
}
// Further simplification using prime table
simplify_prime(ones, dontcares) {
let result = this.simplify(ones, dontcares);
let simplifiedResult = new Array();
let gcols = new Set(); // set of columns, g for "global"
let rowsOfCol = new Array();
let colsOfRow = new Map();
ones.forEach(col => {
gcols.add(col);
rowsOfCol[col] = new Array();
});
result.forEach(row => {
let cols = this.minimumTermify(row);
let reducedCols = new Array(); // Minimum item only on one line
cols.forEach(col => {
if (!dontcares.has(col)) { //Irrelevant items do not participate in prime item table simplification
rowsOfCol[col].push(row);
reducedCols.push(col);
}
});
if (reducedCols.length)
colsOfRow.set(row, reducedCols);
});
// Preliminary determination of prime items
rowsOfCol.forEach(row => {
if (row.length == 1 && colsOfRow.get(row[0])) { //prime term
simplifiedResult.push(row[0]); // join final result
colsOfRow.get(row[0]).forEach(col => {
if (gcols.has(col)) {
gcols.delete(col);
rowsOfCol[col] = new Array(); //Remove all the smallest items contained in this item
}
});
colsOfRow.delete(row[0]);
}
});
while (gcols.size > 0) {
let max = 0;
let rowOfMax = 0;
// Find the implicant with the most minimal terms
colsOfRow.forEach((cols, row) => {
let length = 0;
cols.forEach(col => {
if (gcols.has(col))
length += 1;
});
if (length > max) {
max = length;
rowOfMax = row;
}
});
if (max == 0) break;
simplifiedResult.push(rowOfMax);
colsOfRow.get(rowOfMax).forEach(col => {
if (gcols.has(col)) {
gcols.delete(col);
rowsOfCol[col] = new Array();
}
});
colsOfRow.delete(rowOfMax);
}
return simplifiedResult.length ? simplifiedResult : result;
}
}