Reduce if/else-if on a bunch of partially overlapping conditions

79 Views Asked by At

I was trying to solve a coding problem which ended up with these conditionals:

x_A2 <= x_B1 OR x_A1 >= X_B2  --> 0
x_A1 <= x_B1 <= x_A2  --> x_A2 - X_B1
x_B1 <= x_A1 <= x_B2  --> x_B2 - X_A1
x_B1 <= x_A1 <= x_B2 AND x_B1 <= x_A2 <= x_B2  --> x_A2 - X_A1
x_A1 <= x_B1 <= x_A2 AND x_A1 <= x_B2 <= x_A2  --> x_B2 - X_B1

As you can see if I were to build 5 if/else-if statements, there are some computations that are repeated.

I'd like to know if there is a mathematical theory or procedure/algorithm to "reduce" or optimize any problem of the same kind.

I'm undecided if this was more of a math question.

Just to be more clear: I can start by any one of those "atomic conditions" and have two branches, one for True and one for False. But the starting point can be indeed any of those conditions and every branch contains the same structure just described.

Does this problem need an algorithm of its own involving graphs?

1

There are 1 best solutions below

0
Lajos Arpad On

Let's suppose that you have n statements, each i'th statement consists of f(i) sub-statements. Then, the statements of

s1
s2
.
.
.
sn

in a sense of if-else look like this:

s1
not (s1) and s2
not (s1 or s2) and s3
.
.
.
not (s1 or s2 ... or sn-1) and sn

So, if you are at si, you know that all the earlier statements were false.

Hence, you will know that some earlier statements have some truth-value, such as

T(A1), ... T(Am)

if any of the statements whose truth-value was determined in earlier ifs is a part of a later else if, then you can actually reuse it.

Example:

if A > 5
else if B > 2 and A <= 5

can be simplified to

if A > 5
else if B > 2

because by the time we have reached the else branch we already know that A > 5 is false. Furthermore, even

if A > 5
else if B > 2 and A <= 6

can be simplified to

if A > 5
else if B > 2

Because (not (A > 5)) => (A <= 6) and if we ever reach the else if, we already know that the premise, that is, that (not (A > 5)) is true.

So, as a result, we have the formula of whenever we know the truth values of

T(A1), T(A2), ..., T(Ai), ...

if we have a predicate P inside an else if where T(Ai) => P, then via modus ponens, we already know that P is true. Also, if we know that not T(Ai) => P, then via reductio ad absurdum we know that P is false.

Of course, you can use logic to make a composite statement of your knowledge so far, by applying logical relations or operations and combine new information.

For example, if we know that T(A1) is true and T(A2) is false, then we also know that (T(A1) and T(A2)) is false.

Also, you can apply other mathematical knowledge, such as transitivity of <= or so on. Let's take a look at your first statement:

x_A2 <= x_B1 OR x_A1 >= X_B2

It's a statement of X OR Y. We also know that X OR Y is false if and only if X OR Y is false. Therefore, if you ever reach an else if, then you know that X is false and Y is false. Therefore, if you are at the second statement, then you know that the first was evaluated as false, therefore x_A2 <= x_B1 is false. So its opposite is true, which is x_B1 < x_A2 therefore x_B1 <= x_A2 is also true.