Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1/22/23, 7:29 PM logic - Simplifying a 9 variable boolean expression - Stack Overflow

Simplifying a 9 variable boolean expression


Asked 9 years ago Modified 5 years, 10 months ago Viewed 3k times

I am trying to create a tic-tac-toe program as a mental exercise and I have the board states stored
as booleans like so:
9
http://i.imgur.com/xBiuoAO.png

I would like to simplify this boolean expression...

(a&b&c) | (d&e&f) | (g&h&i) | (a&d&g) | (b&e&h) | (c&f&i) | (a&e&i) | (g&e&c)

My first thoughts were to use a Karnaugh Map but there were no solvers online that supported 9
variables.

and heres the question:

First of all, how would I know if a boolean condition is already as simple as possible?

and second: What is the above boolean condition simplified?

boolean logic

Share Follow asked Dec 29, 2013 at 18:09


Will Sherwood
1,474 3 14 26

Sorted by:
4 Answers
Highest score (default)

2. Simplified condition:

6 The original expression

a&b&c|d&e&f|g&h&i|a&d&g|b&e&h|c&f&i|a&e&i|g&e&c

+50 can be simplified to the following, knowing that & is more prioritary than |

e&(d&f|b&h|a&i|g&c)|a&(b&c|d&g)|i&(g&h|c&f)

https://stackoverflow.com/questions/20828438/simplifying-a-9-variable-boolean-expression 1/5
1/22/23, 7:29 PM logic - Simplifying a 9 variable boolean expression - Stack Overflow

which is 4 chars shorter, performs in the worst case 18 & and | evaluations (the original one
counted 23) There is no shorter boolean formula (see point below). If you switch to matrices,
maybe you can find another solution.

1. Making sure we got the smallest formula


Normally, it is very hard to find the smallest formula. See this recent paper if you are more
interested. But in our case, there is a simple proof.

We will reason about a formula being the smallest with respect to the formula size, where for a
variable a , size(a)=1 , for a boolean operation size(A&B) = size(A|B) = size(A) + 1 + size(B) ,
and for negation size(!A) = size(A) (thus we can suppose that we have Negation Normal Form
at no cost). With respect to that size, our formula has size 37.

The proof that you cannot do better consists in first remarking that there are 8 rows to check, and
that there is always a pair of letter distinguishing 2 different rows. Since we can regroup these 8
checks in no less than 3 conjuncts with the remaining variable, the number of variables in the final
formula should be at least 8*2+3 = 19 , from which we can deduce the minimal tree size.

Detailed proof

Let us suppose that a given formula F is the smallest and in NNF format.

1. F cannot contain negated variables like !a . For that, remark that F should be monotonic,
that is, if it returns "true" (there is a winning row), then changing one of the variables from
false to true should not change that result. According to Wikipedia, F can be written
without negation. Even better, we can prove that we can remove the negation. Following this
answer, we could convert back and from DNF format, removing negated variables in the
middle or replacing them by true .

2. F cannot contain a sub-tree like a disjunction of two variables a|b . For this formula to
be useful and not exchangeable with either a or b , it would mean that there are
contradicting assignments such that for example F[a|b] = true and F[a] = false , therefore
that a = false and b = true because of monotonicity. Also, in this case, turning b to false
makes the whole formula false because false = F[a] = F[a|false] >= F[a|b](b = false) .
Therefore there is a row passing by b which is the cause of the truth, and it cannot go
through a , hence for example e = true and h = true . And the checking of this row passes
by the expression a|b for testing b . However, it means that with a,e,h being true and all
other set to false, F is still true, which contradicts the purpose of the formula.

3. Every subtree looking like a&b checks a unique row. So the last letter should appear just
above the corresponding disjunction (a&b|...)&{c somewhere for sure here} , or this leaf is
useless and either a or b can be removed safely. Indeed, suppose that c does not appear
above, and the game is where a&b&c is true and all other variables are false . Then the
expression where c is supposed to be above returns false , so a&b will be always useless. So
there is a shorter expression by removing a&b .
https://stackoverflow.com/questions/20828438/simplifying-a-9-variable-boolean-expression 2/5
1/22/23, 7:29 PM logic - Simplifying a 9 variable boolean expression - Stack Overflow

4. There are 8 independent branches, so there is at least 8 subtrees of type a&b . We cannot
regroup them using a disjunction of 2 conjunctions since a , f and h never share the same
rows, so there must be 3 outer variables. 8*2+3 makes 19 variables appear in the final
formula. A tree with 19 variables cannot have less than 18 operators, so in total the size have
to be at least 19+18 = 37.

You can have variants of the above formula.

QED.

Share Follow edited Apr 13, 2017 at 12:48 answered Jan 11, 2014 at 15:32
Community Bot Mikaël Mayer
1 1 10.2k 5 61 101

One option is doing the Karnaugh map manually. Since you have 9 variables, that makes for a
2^4 by 2^5 grid, which is rather large, and by the looks of the equation, probably not very
0 interesting either.

By inspection, it doesn't look like a Karnaugh map will give you any useful information (Karnaugh
maps basically reduce expressions such as ((!a)&b) | (a&b) into b ), so in that sense of
simplification, your expression is already as simple as it can get. But if you want to reduce the
number of computations, you can factor out a few variables using the distributivity of the AND
operators over ORs.

Share Follow answered Dec 29, 2013 at 18:22


howard
644 2 8 15

1 He can't even factor any of them out to any effect; a given AND pair can only be in one winning line.
– Bandrami Dec 29, 2013 at 18:26

1 I'm talking about extracting single variables, like so. (a&((b&c) | (d&g) | (e&i))) | (c&((f&i) |
(g&e))) | (e&((d&f) | (b&h))) | (g&h&i) Though, I would recommend against doing this, as it is far
less readable. I don't know what language is going to be used, but it's possible that the compiler already
optimizes for these situations as well. – howard Dec 29, 2013 at 18:34

I may be misremembering symbolics, but I'm pretty sure that's always a martingale from a theoretical
standpoint... – Bandrami Dec 29, 2013 at 18:39

The best way to think of this is how a person would think of it. No person would say to
themselves, "a and b and c, or if d and e and f," etc. They would say "Any three in a row,
0 horizontally, vertically, or diagonally."

Also, instead of doing eight checks (3 rows, 3 columns, and 2 diagonals), you can do just four
checks (three rows and one diagonal), then rotate the board 90 degrees, then do the same checks
https://stackoverflow.com/questions/20828438/simplifying-a-9-variable-boolean-expression 3/5
1/22/23, 7:29 PM logic - Simplifying a 9 variable boolean expression - Stack Overflow

again.

Here's what you end up with. These functions all assume that the board is a three-by-three matrix
of booleans, where true represents a winning symbol, and false represents a not-winning symbol.

def win?(board)
winning_row_or_diagonal?(board) ||
winning_row_or_diagonal?(rotate_90(board))
end

def winning_row_or_diagonal?(board)
winning_row?(board) || winning_diagonal?(board)
end

def winning_row?(board)
3.times.any? do |row_number|
three_in_a_row?(board, row_number, 0, 1, 0)
end
end

def winning_diagonal?(board)
three_in_a_row?(board, 0, 0, 1, 1)
end

def three_in_a_row?(board, x, y, delta_x, delta_y)


3.times.all? do |i|
board[x + i * delta_x][y + i * deltay]
end
end

def rotate_90(board)
board.transpose.map(&:reverse)
end

The matrix rotate is from here: https://stackoverflow.com/a/3571501/238886

Although this code is quite a bit more verbose, each function is clear in its intent. Rather than a
long boolean expresion, the code now expresses the rules of tic-tac-toe.

Share Follow edited May 23, 2017 at 12:02 answered Dec 29, 2013 at 18:36
Community Bot Wayne Conrad
1 1 100k 26 155 190

You know it's a simple as possible when there are no common sub-terms to extract (e.g. if you
had "a&b" in two different trios).
0
You know your tic tac toe solution must already be as simple as possible because any pair of
boxes can belong to at most only one winning line (only one straight line can pass through two
given points), so (a & b) can't be reused in any other win you're checking for.

https://stackoverflow.com/questions/20828438/simplifying-a-9-variable-boolean-expression 4/5
1/22/23, 7:29 PM logic - Simplifying a 9 variable boolean expression - Stack Overflow

(Also, "simple" can mean a lot of things; specifying what you mean may help you answer your
own question. )

Share Follow edited Dec 29, 2013 at 18:44 answered Dec 29, 2013 at 18:25
Bandrami
4,064 1 13 12

https://stackoverflow.com/questions/20828438/simplifying-a-9-variable-boolean-expression 5/5

You might also like