K-map solver: Karnaugh maps for 2 to 5 variables
Fill in a Karnaugh map by clicking cells, typing minterms and don’t-cares, or entering an expression. The solver draws the groups on the map, including the ones that wrap round the edges, and gives the minimal sum of products, the minimal product of sums and the essential prime implicants, for 2 to 5 variables.
On this page
How to use the K-map solver
- Pick the number of variables (2 to 5) and, if you like, rename them. Changing the count clears the map.
- Fill the map. Click a cell to cycle it through 0, 1 and X. Each cell shows its minterm number in the corner. Or type a list such as
Σm(1, 3, 7, 11, 15) + d(0, 2, 5)and press “Set minterms”; theΣ, themand the brackets are optional, so1 3 7 11 15works too. Or type an expression such asAB' + CDand the solver fills the map from its truth table. - Read the groups. Each coloured outline is one product term of the answer. A dashed edge means the group continues on the other side of the map. Hover over or tab to a term in the list below the map to highlight just that group.
- Switch to POS to group the 0s instead. The groups then show the sum terms of the minimal product of sums.
The address bar records the map, so you can bookmark a problem or send it to someone.
How a Karnaugh map works
A Karnaugh map is a truth table folded into a grid so that neighbouring cells differ in exactly one variable. For four variables, the rows are labelled with and the columns with , both in Gray-code order: 00, 01, 11, 10. Because 10 and 00 also differ in one bit, the left edge is adjacent to the right edge and the top to the bottom. The map is really drawn on a torus.
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 1 | 3 | 2 |
| 01 | 4 | 5 | 7 | 6 |
| 11 | 12 | 13 | 15 | 14 |
| 10 | 8 | 9 | 11 | 10 |
Two adjacent 1s differ in one variable, so by the combining law they merge into one product without that variable. Four 1s in a 2×2 square or a line lose two variables, eight lose three. Simplifying a function becomes covering every 1 with as few rectangles as possible, each as large as possible, with sides that are powers of two.
The rules for grouping
- Groups are rectangles of 1, 2, 4, 8 or 16 cells. L-shapes and diagonals are not allowed.
- Every 1 must be in at least one group. Groups may overlap.
- Make each group as large as possible: a group that cannot grow is a prime implicant.
- Use as few groups as possible. Start with essential prime implicants, the groups that are the only way to cover some 1, then cover what is left.
- Don’t-cares (X) may be included in a group when they make it bigger, but need not be covered.
- Groups wrap round the edges, and the four corners of a four-variable map are one group of four.
To read a group as a product, keep the variables that are constant across it: write if it is 1 throughout and if it is 0 throughout. Variables that change inside the group drop out.
Worked example: the four corners
Take . The 1s at minterms 0, 2, 8 and 10 sit in the four corners. Across all four, and , while and change, so the group is . The 1s at 5, 7, 13 and 15 form a 2×2 square in the middle, where : the group is .
The solver gives F = BD + B′D′, with 2 essential prime implicants. This is the function “ equals ”, an XNOR of two variables, though it started as eight minterms over four.
Worked example: don’t-cares
A circuit only ever sees some inputs, so the output for the others does not matter. Here :
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | X | 1 | 1 | X |
| 01 | 0 | X | 1 | 0 |
| 11 | 0 | 0 | 1 | 0 |
| 10 | 0 | 0 | 1 | 0 |
The column holds 3, 7, 15 and 11: the group . That leaves minterm 1. On its own it would need a long product, but with the don’t-cares 0 and 2 it fills the whole top row (minterms 0, 1, 3 and 2), . The answer is F = A′B′ + CD. Without the don’t-cares the best is A′B′D + CD, which has 5 literals instead of 4.
Load this example into the solver with “Example with don’t-cares”. There is a second minimal answer, , which groups minterm 1 with 3, 5 and 7 instead; both have two terms and four literals.
Product of sums from the 0s
Grouping the 0s instead of the 1s gives the complement as a sum of products. Applying De Morgan turns each product into a sum with every literal complemented, and the whole into a product of sums. In the solver, switch “Show groups for” to POS: a group of 0s where and becomes the sum term , because it must be 0 exactly on that group.
Five variables
A five-variable map is drawn as two four-variable maps, one for and one for , placed side by side. Cells in the same position on the two maps are adjacent, as if one map were stacked on top of the other. A group that appears in the same place on both maps drops . Beyond five variables, maps stop being practical by hand, and the Boolean algebra calculator or the Quine–McCluskey method is the better tool.
Common mistakes
- Binary order instead of Gray order. Labelling columns 00, 01, 10, 11 puts 01 next to 10, which differ in two bits, and groups across them are wrong. Always use 00, 01, 11, 10.
- Groups of three or six. Only powers of two give a single product. Three 1s in a row need two overlapping groups of two.
- Missing the wrap-around. The leftmost and rightmost columns are neighbours, and so are the top and bottom rows. The four corners make one group.
- Covering the don’t-cares. An X never has to be in a group. Include it only if it makes a group larger.
- Too many groups. A group whose 1s are all covered by other groups is redundant and should be dropped, even if it is a prime implicant.
For the algebra behind the map, step through the same simplification in the Boolean algebra calculator. To check a result row by row, use the truth table generator, and to build the circuit, the logic gate simulator.
References
- M. Karnaugh, “The map method for synthesis of combinational logic circuits”, Transactions of the AIEE, Part I 72(5), 1953. doi:10.1109/TCE.1953.6371932
- E. W. Veitch, “A chart method for simplifying truth functions”, Proceedings of the ACM National Meeting, 1952.
- M. M. Mano and M. D. Ciletti, Digital Design, 6th ed., Pearson, 2018. Chapter 3.
- “Karnaugh map”, Wikipedia.