1

I have a few boolean expression in RPN-Format like this:

{0} {1} {2} AND OR    // equals: {0} or {1} and {2}

Computing the boolean variables {x} is very expensive. And obviously there is no need to compute {1} and {2} if {0} is already true, since the expression will alway evaluation to true in this case.

How can I detect beforehand which boolean variables I have to evaluate first to abort the evaluation of the expression with as few variables evaluated as possible?

I want to know which variables with a definite value will evaluate the whole expression to be true or false.

user2033412
  • 1,950
  • 2
  • 27
  • 47

3 Answers3

1

You should prefer to evaluate expressions where the expected number of sub-expression evaluations is lower.

  • a and (b or c or d)

    You should first evaluate a - since if it false - you're done

  • a or (b and c and d)

    Again, you should first evaluate a - since if it is true - you're done

  • (a or b) and (c or d or e)

    First, evaluate (a or b) - since if it is false - you're done

etc.

To implement:

  • Build a tree where the root is an or if your expression has the form "expr or expr or expr or ...". Alternatively if your expression has the form "expr and expr and expr and ..." - the root should be an and.

  • Build the sub-trees recursively. The levels or the trees are and / or alternatively. The number of children or each node varies (2 or more except leafs).

  • Evaluate recursively by selecting the sub-tree with the minimal number of children and evaluate it first.

Lior Kogan
  • 19,919
  • 6
  • 53
  • 85
  • But is there a way to detect the abort-criterion not while evaluating but finding variables beforehand which _must_ be true or false for the expression to evaluation to true? – user2033412 Jul 28 '17 at 19:43
  • 1
    First, construct the tree. Don't evaluate. Sub-trees with depth 1 represent "abort-criterion". – Lior Kogan Jul 28 '17 at 19:48
0

Algorithm

One approach is to first expand the boolean expression, and then factorize.

The common factors will tell you which variables can force the expression to be false.

Then you can invert the expression and repeat to find the variables that will force the expression to be true.

Example 1

This should become clearer with an example, I'll write + for OR and . for AND:

0 + 1.2

this is already expanded. There is no common factor so there is no way a single variable can force this to be true.

Next we invert and expand using De Morgan's laws:

!(0 + 1.2)
= !0.!(1.2)
= !0.(!1 + !2)

This has a factor of !0, i.e. if 0 is false, we can conclude that the whole expression is false.

Example 2

1.2 + 1.3
= 1.(2 + 3)

so if 1 is false, the whole expression is false.

!(1.2 + 1.3)
=!(1.2).!(1.3)
=(!1 + !2).(!1 + !3)
=!1 + !2.!1 + !2.!3

This has no common factor so there is no one variable that can force the expression to be true.

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
0

Convert your expression into a minimized sum-of-product form. This can be done by using a Karnaugh-Veitch map for small expressions. More involved methods are described here.

enter image description here

In your example, you get one term a (your {0}) with a single literal and a second term cb ({1}{2} in your notation) with two literals. For every input variable, count the terms affected by this variable.
A simple count would not take into account that terms may be large (many literals) or small (few literals). Therefore, you might weight the terms with the number of Karnaugh cells covered by the respective cell.
The counting result (a:1x4, b:1x2, c:1x2) tells you which variable should be evaluated first. The evaluation ends, once a single term becomes true or all terms are evaluated to false.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54