We can implement the K-Map algorithm in python
for 4 variables, as shown below. The function accepts the Boolean function in SOP (sum of products) form and the names of the variables and returns a simplified reduced representation. Basically you need to create rectangular groups containing total terms in power of two like 8, 4, 2 and try to cover as many elements as you can in one group (we need to cover all the ones).
For example, the function can be represented F(w,x,y,z) = xy’ + x’z’ + wxz + wx’y in SOP form as f(w,x,y,z)=∑(0,2,4,5,8,10,11,12,13,15), as can be seen from the below table:

As can be seen from the output of the next code snippet, the program outputs the simplified form x¬y + ¬x¬z + wyz
, where negation of a boolean variable x
is represented as ¬x
in the code.
from collections import defaultdict
from itertools import permutations, product
def kv_map(sop, vars):
sop = set(sop)
not_covered = sop.copy()
sop_covered = set([])
mts = [] # minterms
# check for minterms with 1 variable
all_3 = [''.join(x) for x in product('01', repeat=3)]
for i in range(4):
for v_i in [0,1]:
if len(not_covered) == 0: continue
mt = ('' if v_i else '¬') + vars[i]
s = [x[:i]+str(v_i)+x[i:] for x in all_3]
sop1 = set(map(lambda x: int(x,2), s))
if len(sop1 & sop) == 8 and len(sop_covered & sop1) < 8: # if not already covered
mts.append(mt)
sop_covered |= sop1
not_covered = not_covered - sop1
if len(not_covered) == 0:
return mts
# check for minterms with 2 variables
all_2 = [''.join(x) for x in product('01', repeat=2)]
for i in range(4):
for j in range(i+1, 4):
for v_i in [0,1]:
for v_j in [0,1]:
if len(not_covered) == 0: continue
mt = ('' if v_i else '¬') + vars[i] + ('' if v_j else '¬') + vars[j]
s = [x[:i]+str(v_i)+x[i:] for x in all_2]
s = [x[:j]+str(v_j)+x[j:] for x in s]
sop1 = set(map(lambda x: int(x,2), s))
if len(sop1 & sop) == 4 and len(sop_covered & sop1) < 4: # if not already covered
mts.append(mt)
sop_covered |= sop1
not_covered = not_covered - sop1
if len(not_covered) == 0:
return mts
# check for minterms with 3 variables similarly (code omitted)
# ... ... ...
return mts
mts = kv_map([0,2,4,5,8,10,11,12,13,15], ['w', 'x', 'y', 'z'])
mts
# ['x¬y', '¬x¬z', 'wyz']
The following animation shows how the above code (greedily) simplifies the Boolean function given in SOP form (the basic goal is to cover all the 1s with minimum number of power-2 blocks). Since the algorithm is greedy it may get stuck to some local minimum, that we need to be careful about.
