Python-program-to-convert-pos-to-sop

In this article, we will learn how to convert a Product of Sum (POS) form to its equivalent Sum of Product (SOP) form in Boolean algebra. This conversion is useful in digital logic design and Boolean expression simplification.

Problem statement ? We are given a POS form and need to convert it into its equivalent SOP form.

The conversion involves counting the number of variables in the POS form, extracting maxterms, and then finding the corresponding minterms to generate the SOP expression.

Understanding POS to SOP Conversion

In Boolean algebra:

  • POS form: Product of sums like (A'+B'+C).(A+B+C').(A+B'+C).(A'+B+C)
  • SOP form: Sum of products like A'B'C' + A'BC + AB'C + ABC
POS to SOP Conversion Process POS Expression Extract Maxterms (Convert to Binary) Find Missing Terms (Minterms) SOP Expression (A'+B'+C).(A+B+C')... Maxterms: 0,1,2,4 Minterms: 3,5,6,7 A'BC + AB'C + ABC' + ABC

Example

Let's convert the POS expression (A'+B'+C).(A+B+C').(A+B'+C).(A'+B+C) to SOP form ?

# Python code to convert standard POS form to standard SOP form

def count_no_alphabets(POS):
    """Count number of variables in POS expression"""
    i = 0
    no_var = 0
    # Count alphabets before first '.' character
    while (POS[i] != '.'):
        if (POS[i].isalpha()):
            no_var += 1
        i += 1
    return no_var

def Cal_Max_terms(Max_terms, POS):
    """Extract maxterms from POS expression"""
    a = ""
    i = 0
    while (i < len(POS)):
        if (POS[i] == '.'):
            # Binary to decimal conversion
            b = int(a, 2)
            # Append each maxterm (integer type) into the list
            Max_terms.append(b)
            a = ""
            i += 1
        elif(POS[i].isalpha()):
            # Check if variable has complement
            if(i + 1 != len(POS) and POS[i + 1] == "'"):
                # Complement variable = 1 in maxterm
                a += '1'
                i += 2
            else:
                # Normal variable = 0 in maxterm
                a += '0'
                i += 1
        else:
            i += 1
    # Append last maxterm
    Max_terms.append(int(a, 2))

def Cal_Min_terms(Max_terms, no_var, start_alphabet):
    """Convert maxterms to minterms and generate SOP"""
    Min_terms = []
    max_terms = 2 ** no_var
    
    # Find minterms (terms not in maxterms)
    for i in range(0, max_terms):
        if (Max_terms.count(i) == 0):
            # Convert integer to binary
            b = bin(i)[2:]
            # Pad with zeros to match variable count
            while(len(b) != no_var):
                b = '0' + b
            Min_terms.append(b)
    
    SOP = ""
    # Generate SOP expression from minterms
    for i in Min_terms:
        value = start_alphabet
        for j in i:
            if (j == '0'):
                # Add complement variable
                SOP = SOP + value + "'"
            else:
                # Add normal variable
                SOP = SOP + value
            # Move to next alphabet
            value = chr(ord(value) + 1)
        SOP = SOP + "+"
    
    # Remove extra '+' at the end
    SOP = SOP[:-1]
    return SOP

def main():
    # Input POS expression
    POS_expr = "(A'+B'+C).(A+B+C').(A+B'+C).(A'+B+C)"
    Max_terms = []
    
    # Count variables and extract maxterms
    no_var = count_no_alphabets(POS_expr)
    Cal_Max_terms(Max_terms, POS_expr)
    
    # Generate SOP expression
    SOP_expr = Cal_Min_terms(Max_terms, no_var, POS_expr[1])
    
    print("Standard SOP form of " + POS_expr + " ==> " + SOP_expr)

# Driver code
if __name__ == "__main__":
    main()

The output of the above code is ?

Standard SOP form of (A'+B'+C).(A+B+C').(A+B'+C).(A'+B+C) ==> A'B'C'+A'BC+AB'C+ABC

How the Conversion Works

The algorithm follows these steps:

  1. Count Variables: Determine the number of Boolean variables (A, B, C = 3 variables)
  2. Extract Maxterms: Parse each sum term and convert to binary representation
  3. Find Minterms: Identify missing terms from the complete set (2³ = 8 total terms)
  4. Generate SOP: Convert minterms to product terms and combine with '+' operator

Maxterm to Minterm Mapping

For the given example with 3 variables (A, B, C):

Term Number Binary Type Expression
0 000 Maxterm (A'+B'+C) - excluded
1 001 Maxterm (A+B+C') - excluded
2 010 Maxterm (A+B'+C) - excluded
3 011 Minterm A'BC - included
4 100 Maxterm (A'+B+C) - excluded
5 101 Minterm AB'C - included
6 110 Minterm ABC' - included
7 111 Minterm ABC - included

Conclusion

POS to SOP conversion extracts maxterms from the POS expression, finds the missing terms (minterms), and combines them with OR operators. This conversion is fundamental in Boolean algebra and digital circuit design for expression manipulation and optimization.

Updated on: 2026-03-25T07:04:45+05:30

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements