I am trying to solve the boolean expression optimization problem for a simple rule engine.
Here is what I am trying to do, lets say if I have following 5 conditions (where a,b,c,d,e,f are complex boolean expressions)
if (a AND b AND c AND d AND e AND f) then do-1
if (a AND b AND c AND d AND e) then do-2
if (a AND b AND c AND d) then do-3
if (a AND b AND c) then do-4
if (a AND b) then do-5
In case when I execute these conditions in a linear order I will
- evaluate "a" for 5 times
- evaluate "b" for 5 times
- evaluate "c" for 4 times
- evaluate "d" for 3 times
- evaluate "e" for 2 times
- evaluate "f" for 1 time
I would like to make a expression execution tree from this so that each expression (a,b,c,d,e,f) is evaluated minimum number of times. The perfect solution will be that each expression is evaluated only once. I think this can be achieved with a tree creation where all these conditions are part of a tree.
The tree may look like this
if(a AND b) then do-5 {
if(c) then do-4 {
if(d) then do-3 {
if(e) then do-2 {
if(f) then do-1
}
}
}
}
My questions is - How can I make this tree from the set of boolean expressions listed above?
Related Questions:
Algorithm for evaluating nested logical expression
Converting an expression to conjunctive normal form with a twist