Module QM
In: truthtable/qm.rb

Quine-McCluskey algorithm

Methods

qm  

Public Instance methods

implements Quine-McCluskey algorithm. It minimize a boolean function given by tbl.

For example, the 3-inputs majority function is given as follows.

 tbl = {
 #  P      Q      R
   [false, false, false] => false,
   [false, false, true ] => false,
   [false, true,  false] => false,
   [false, true,  true ] => true,
   [true,  false, false] => false,
   [true,  false, true ] => true,
   [true,  true,  false] => true,
   [true,  true,  true ] => true,
 }
 TruthTable::QM.qm(tbl)
 #=>
 [[true, true, :x], [true, :x, true], [:x, true, true]]  # P&Q | P&R | Q&R
 # P     Q           P         R           Q     R

For another example, the implication function is given as follows.

 tbl = {
 #  P      Q
   [false, false] => true,
   [false, true ] => true,
   [true,  false] => false,
   [true,  true ] => true,
 }
 TruthTable::QM.qm(tbl)
 #=>
 [[false, :x], [:x, true]]  # ~P | Q
 # ~P               Q

tbl is a hash to represent a boolean function. If the function has N variables, all key of tbl must be an array of N elements.

A element of the key array and a value of the hash should be one of follows:

  • false, 0
  • true, 1
  • :x

0 is same as false.

1 is same as false.

:x means "don‘t care".

For example, 3-inputs AND function can be given as follows.

 tbl = {
   [false, :x,    :x   ] => false,
   [:x,    false, :x   ] => false,
   [:x,    :x,    false] => false,
   [true,  true,  true ] => true,
 }

:x can be used for a value of tbl too. It means that the evaluated result of minimized boolean function is not specified: it may be evaluated to true or false.

 tbl = {
   [false, false] => false,
   [false, true ] => true,
   [true,  false] => false,
   [true,  true ] => :x
 }

If tbl doesn‘t specify some combination of input variables, it assumes :x for such combination. The above function can be specified as follows.

 tbl = {
   [false, false] => false,
   [false, true ] => true,
   [true,  false] => false,
 }

QM.qm returns an array of arrays which represents the minimized boolean function.

The minimized boolean function is a disjunction of terms such as "term1 | term2 | term3 | …".

The inner array represents a term. A term is a conjunction of input variables and negated input variables: "P & ~Q & ~R & S & …".

[Validate]