4

I'm trying to write the Quine-McCluskey algorithm in python, but I wanted to see if there were any versions out there that I might use instead. A google search showed few useful results. I'm looking for 4x4 map reduction, not 2x2 or 3x3. Any ideas or references?

2 Answers 2

6
def combine(m, n):
    a = len(m)
    c = ''
    count = 0
    for i in range(a): 
        if(m[i] == n[i]):
            c += m[i]
        elif(m[i] != n[i]):
            c += '-'
            count += 1

    if(count > 1): 
        return None
    else:            
        return c


def find_prime_implicants(data):
    newList = list(data)
    size = len(newList)
    IM = []
    im = []
    im2 = []
    mark = [0]*size
    m = 0
    for i in range(size):
        for j in range(i+1, size):
            c = combine( str(newList[i]), str(newList[j]) )
            if c != None:
                im.append(str(c))
                mark[i] = 1
                mark[j] = 1
            else:
                continue

    mark2 = [0]*len(im)
    for p in range(len(im)):
        for n in range(p+1, len(im)):
            if( p != n and mark2[n] == 0):
                if( im[p] == im[n]):
                    mark2[n] = 1


    for r in range(len(im)):
        if(mark2[r] == 0):
            im2.append(im[r])

    for q in range(size):
        if( mark[q] == 0 ):
            IM.append( str(newList[q]) )
            m = m+1

    if(m == size or size == 1):
        return IM
    else:
        return IM + find_prime_implicants(im2)


minterms = set(['1101', '1100', '1110', '1111', '1010', '0011', '0111', '0110'])

minterms2 = set(['0000', '0100', '1000', '0101', '1100', '0111', '1011', '1111'])

minterms3 = set(['0001', '0011', '0100', '0110', '1011', '0000', '1000', '1010', '1100', '1101'])

print 'PI(s):', find_prime_implicants(minterms)

print 'PI2(s):', find_prime_implicants(minterms2)

print 'PI3(s):', find_prime_implicants(minterms3)
Sign up to request clarification or add additional context in comments.

2 Comments

Does this algorithm only calculates the prime implicates or also the essential prime implicates?
@HighwayJohn: Whatever comes out, it's certainly not minimal; I have a 10-bit example with 149 ones, where the algorithm here boils it down to 137 summands, but where I know the upper limit for a minimized sum is 119.
4

In the Wikipedia of which you gave the link, there are some "External links" at the bottom, among which are these, interesting relatively to your project:

  • " Python Implementation by Robert Dick "

    Wouldn't this fulfil your need ?

  • " A series of two articles describing the algorithm(s) implemented in R: first article and second article. The R implementation is exhaustive and it offers complete and exact solutions. It processes up to 20 input variables. "

    You could use the rpy Python interface to R language to run the R code of the Quine-McCluskey algorithm. Note that there is a rewrite of rpy : rpy2

    Also, why not, write yourself a new Python script, using the enhancement of the algorithm done by Adrian Duşa in 2007 , lying in the second article ?

1 Comment

vi is up, and I'm coding my heart out, thanks to the reference to that second article. Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.