9

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:

https://i.stack.imgur.com/rENbC.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?

Will Sherwood
  • 1,484
  • 3
  • 14
  • 27

4 Answers4

6

2. Simplified condition:

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

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)

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.

  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.

Community
  • 1
  • 1
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
0

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 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.

howard
  • 644
  • 2
  • 7
  • 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 '13 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 '13 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 '13 at 18:39
0

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).

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.

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

Bandrami
  • 4,242
  • 1
  • 13
  • 12
0

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, 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 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.

Community
  • 1
  • 1
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191