Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalence of boolean expressions

I have a problem that consist in comparing boolean expressions ( OR is +, AND is * ). To be more precise here is an example:

I have the following expression: "A+B+C" and I want to compare it with "B+A+C". Comparing it like string is not a solution - it will tell me that the expressions don't match which is of course false. Any ideas on how to compare those expressions?

Any ideas about how can I tackle this problem? I accept any kind of suggestions but (as a note) the final code in my application will be written in C++ (C accepted of course).

An normal expression could contain also parenthesis:

(A * B * C) + D or A+B*(C+D)+X*Y

Thanks in advance,

Iulian

like image 942
INS Avatar asked Jun 03 '10 11:06

INS


2 Answers

I think the competing approach to exhaustive (and possibly exhausting) creation of truth tables would be to reduce all your expressions to a canonical form and compare those. For example, rewrite everything into conjunctive normal form with some rule about the ordering of symbols (eg alphabetical order within terms) and terms (eg alphabetical by first symbol in term). This of course, requires that symbol A in one expression is the same as symbol A in another.

How easy it is to write (or grab from the net) a C or C++ function for rewriting your expressions into CNF I don't know. However, there's been a lot of AI work done in C and C++ so you'll probably find something when you Google.

I'm also a little unsure about the comparative computational complexity of this approach and the truth-table approach. I strongly suspect that it's the same.

Whether you use truth tables or a canonical representation you can of course keep down the work to be done by splitting your input forms into groups based on the number of different symbols that they contain.

EDIT: On reading the other answers, in particular the suggestion to generate all truth tables and compare them, I think that @Iulian has severely underestimated the number of possible truth tables.

Suppose that we settle on RPN to write the expressions, this will avoid having to deal with brackets, and that there are 10 symbols, which means 9 (binary) operators. There will be 10! different orderings of the symbols, and 2^9 different orderings of the operators. There will therefore be 10! x 2^9 == 1,857,945,600 rows in the truth table for this expression. This does include some duplicates, any expression containing only 'and' and 'or' for instance will be the same regardless of the order of symbols. But I'm not sure I can figure this any further ...

Or am I making a big mistake ?

like image 181
High Performance Mark Avatar answered Sep 27 '22 21:09

High Performance Mark


You can calculate the truth table for each expression over all possible inputs then compare the truth tables.

like image 37
Mark Byers Avatar answered Sep 27 '22 21:09

Mark Byers