Project: Optimal Circuit Synthesis with SAT · Searching Small Circuits

Lesson 2

Nikolai Chukhin · Alexander S. Kulikov

There are only \(2^{2^n}\) Boolean functions on \(n\) variables, yet this number grows extremely fast: it is \(16\) for \(n=2\), \(256\) for \(n=3\), and \(65\ 536\) for \(n=4\). Exhaustive methods are therefore useful for discovering patterns on tiny instances, but they do not scale smoothly.

For up to three variables, every function has an optimal circuit that is also a formula: no intermediate result needs to be used twice. This permits a particularly simple dynamic program over truth tables.

Programming problem. Find the minimum number of gates needed to compute a Boolean function of at most three variables. All sixteen fan-in-two operations are allowed, and an input itself has size zero.

  • Input format.  The first line contains \(n\) (\(1\le n\le3\)). The second line contains a bit string of length \(2^{n}\), the target truth table in lexicographic order.

  • Output format.  Print the minimum number of gates.

5 points
Public samples
Public sample 1
Input
2
0110
Expected output
1
Public sample 2
Input
3
00010111
Expected output
2
Public sample 3
Input
3
01101001
Expected output
2