Boolean logic calculation for dependent variables

33 Views Asked by At

Given the following problem:

Which statements are equivalent:

if x == y and y == z or z == x:
    do something
if x == y and (y == z or z == x):
   do something
if (x == y and y == z) or z == x:
   do Something

How do you create an exhaustive list of test cases to check all possibilities?

Here is my approach: We have three relationships:

x==y x==z y==z

each relationship can either be true or false. I understand that there are 2^3 possibilities but since these conditions are not independent how many valid logical relationships do I have? Is there a formula?

I count 5

x    y    z          x=y    x=z    y=z
1    1    1          true   true   true
1    1    2          true   false  false
1    2    1          false  true   false
1    2    2          false  false  true
1    2    3          false  false  false

essentially, any truth table row where there are only 2 true's is logically inconsistent.

x=y  & x=z  -> y=z
x=y  & x!=z -> y!=z
x!=y & x=z  -> y!=z

for a small problem like this, it's easy to find the solution by inspection. Suppose though we have 20 variables, seems like we should be able to calculate the number of valid rows in the truth table.