跳转至

极小布尔表达式求解记:如何用对称性与定向搜索征服组合爆炸

文章背景与核心概要

表达任意 \(n\) 个变量的布尔函数,最少需要多少个 AND 和 OR 逻辑运算符?这是理论计算机科学与离散数学领域中一个极富魅力的开放命题。当变量数由 4 个(搜索空间为 \(2^{16} = 65,536\) 种函数)提升到 5 个时,函数空间呈指数级爆炸至 \(2^{32} \approx 42.9\) 亿种,若采用原始的暴力搜索需要逾 \(30 \cdot 2^{64}\) 次循环迭代,哪怕倾尽全球超算也无法完成。

计算机科学家 Alex Healy 与 Russ Cox 展开攻坚,将图论中最短路径 Floyd-Warshall 算法引入公式空间,并通过德摩根定律、输入变量排列对称性(缩减 120 倍)以及输入取反不变性(缩减 32 倍),结合精妙的位运算真值表表示,一举将搜索空间缩减了 7,680 倍;在搜索后半程(\(k > 22\)),他们进一步创新地切换为自顶向下的定向搜索策略,最终仅用半天 CPU 算力便形式化证实了 5 变量布尔函数所需的极大极小操作符数目为 \(a(5) = 28\)。


概要

Summary

本文探讨了如何通过计算搜索,确定表达任意包含 \(n\) 个变量的布尔函数所需的 AND 与 OR 运算符的最小数目,重点聚焦于求解 5 个变量时的理论极值(即 Alex Healy 与作者于 2010 年 4 月最终算出的 \(a(5) = 28\))。文章详尽复盘了从最初朴素的暴力穷举算法,到利用数学对称性、有序状态探索以及自顶向下定向搜索征服组合爆炸的极致优化历程。最后,文章还探讨了引入 XOR 异或运算后对公式复杂度的简化效果,以及如何利用真值表的高效位运算来实现输入置换与取反。

This post explores the computational search for the minimum number of AND and OR operators required to write any Boolean function of \(n\) variables, focusing on finding the value for five variables (\(a(5) = 28\)), computed in April 2010 by Alex Healy and the author. It details the journey from a naive brute-force algorithm to heavily optimized approaches using symmetry, methodical exploration, and a top-down directed search to conquer the combinatorial explosion. Finally, it touches upon how the addition of XOR simplifies formulas and how truth-table manipulation enables efficient generation of input permutations and inversions.


1. 朴素暴力搜索法的局限

1. A Naive Brute Force Approach

包含两个输入变量的任意布尔函数,最多只需 3 个 AND 或 OR 运算符即可表达。例如,两个变量的奇偶校验函数 (\(X \text{ XOR } Y\)) 可展开为:

Any Boolean function of two variables can be written with at most 3 AND or OR operators. For example, the parity function on two variables (\(X \text{ XOR } Y\)) is expressed as:

\(X \text{ XOR } Y = X \cdot Y' + X' \cdot Y\)

(其中 \(X'\) 代表“非 \(X\)”,乘法表示 AND,加法表示 OR)。

(where \(X'\) denotes "not \(X\)," and we use multiplication for AND and addition for OR).

随着输入变量数量的增加,奇偶校验函数依然是最难表达、需要最多运算符的极端函数: - 3 个变量:需要 9 个运算符 - 4 个变量:需要 15 个运算符

As we scale up the number of variables, parity remains a hardest function requiring more operators: * 3 variables: 9 operators * 4 variables: 15 operators

这一序列自然引发了直觉猜测:奇偶校验函数是否永远是表达代价最高的函数?最小运算符数量是否始终在 \(2^n - 1\) 与 \(2^n + 1\) 之间交替波动?

This sequence naturally prompts the questions: Is parity always a hardest function? Does the minimum number of operators alternate between \(2^n - 1\) and \(2^n + 1\)?

初版暴力穷举程序

The Initial Brute Force Program

为了计算 \(a(4)\),作者最初编写了一个程序,用于穷举找出包含 \(n\) 个变量的每一个布尔函数所需的最小运算符数量。因为有 \(2^4 = 16\) 种可能的输入状态组合,所以总共存在 \(2^{16} = 65,536\) 种互不相同的布尔函数。两两组合这些函数会产生 \(2^{16} \cdot 2^{16} = 2^{32}\) 对函数组合。

To compute \(a(4)\), a program was written to find the minimum number of operators for every Boolean function of \(n\) variables. Because there are \(2^4 = 16\) input settings, there are \(2^{16} = 65,536\) different functions. Combining them yields \(2^{16} \cdot 2^{16} = 2^{32}\) pairs of functions.

该算法借鉴了图论中著名的 Floyd-Warshall 全源最短路径算法,将两点间的“距离”替换为“公式尺寸”:

The algorithm is an adaptation of the Floyd-Warshall all-pairs shortest paths algorithm, substituting formula sizes for distance:

// Algorithm 1
func compute()
    for each function f
        size[f] = ∞

    for each single variable function f = v
        size[f] = 0

    loop
        changed = false
        for each function f
            for each function g
                d = size[f] + 1 + size[g]
                if d < size[f OR g]
                    size[f OR g] = d
                    changed = true
                if d < size[f AND g]
                    size[f AND g] = d
                    changed = true
        if not changed
            return

真值表二进制表示

Truth Table Representation

使用紧凑的二进制真值表可以将所有布尔函数直接映射为简单的数组索引,并且 AND / OR 逻辑运算可以直接借助极快的底层位运算指令完成。

Using a dense binary truth table allows functions to be mapped via simple arrays, and AND/OR operations to be performed using fast bitwise operators.

尽管这种方法在 \(n = 4\) 时尚可运行(最终算得 \(a(4) = 15\)),但一旦扩展到 \(n = 5\),暴力穷举将需要进行大约 \(30 \cdot 2^{64}\) 次循环迭代 —— 对于任何常规计算而言,这都是天文数字。

While this worked up to \(n = 4\) (where \(a(4) = 15\)), scaling to \(n = 5\) would require roughly \(30 \cdot 2^{64}\) iterations—well out of reach for a naive approach.


2. 深度挖掘数学对称性

2. Exploiting Symmetry

五年之后,Alex Healy 与作者利用三条核心的数学结构对称性,彻底突破了这道算力天花板:

Five years later, Alex Healy and the author broke through these limits by exploiting structural symmetries:

  1. 德摩根定律 (De Morgan’s Laws):函数 \(f\) 与其取反形式 \(\neg f\) 拥有完全相同的运算复杂度;
  2. 输入置换对称性 (Input Permutations):任意重新排列函数的输入变量(例如 \(f(V, W, X, Y, Z)\) 与 \(f(Z, Y, X, W, V)\)),绝不会改变其最低复杂度,由此直接斩获 \(5! = 120\) 倍的搜索空间缩减;
  3. 输入取反不变性 (Input Inversions):对任何输入变量取反(例如用 \(\neg X\) 代替 \(X\)),同样不会改变整体公式的最优复杂度(带来 \(2^5 = 32\) 倍的缩减空间)。
  1. De Morgan’s Laws: \(f\) and \(\neg f\) share the exact same complexity.
  2. Input Permutations: Permuting inputs to a function (e.g., \(f(V, W, X, Y, Z)\) vs. \(f(Z, Y, X, W, V)\)) preserves complexity, saving a factor of \(5! = 120\).
  3. Input Inversions: Negating any input variable does not change its complexity (factor of \(2^5 = 32\)).

综合上述对称性优化,算法预期的迭代总量惊人地缩减了 \(7,680\) 倍,使原本不可逾越的问题空间大幅收敛:

Combining these optimizations reduced the total number of expected iterations by a factor of \(7,680\), shrinking the problem space dramatically:

// Algorithm 2
func compute():
    for each function f
        size[f] = ∞

    for each single variable function f = v
        size[f] = 0

    loop
        changed = false
        for each canonical function f
            for each function ff equivalent to f
                for each canonical function g
                    d = size[ff] + 1 + size[g]
                    changed |= visit(d, ff OR g)
                    changed |= visit(d, ff AND g)
                    changed |= visit(d, ff AND ¬g)
                    changed |= visit(d, ¬ff AND g)
        if not changed
            return

func visit(d, fg):
    if size[fg] != ∞
        return false

    record fg as canonical

    for each function ffgg equivalent to fg
        size[ffgg] = d
    return true

3. 严格有序的状态空间探索

3. Methodical Exploration

与其盲目地一轮轮循环直到状态不再变更,不如按照函数公式的复杂度顺序严格递增进行考察(依次遍历复杂度为 \(1\)、再到 \(2\) 的所有函数)。这确保了每对函数最多只会被考察一次:

Instead of looping blindly until nothing changes, we can consider functions in strict order of complexity (complexity \(1\), then \(2\), etc.). This ensures each function pair is considered at most once:

// Algorithm 4 (Canonicalized & Ordered)
func compute():
    for each function f
        size[f] = ∞

    for each single variable function f = v
        size[f] = 0

    for k = 1 to ∞
        for each canonical function f
            for each function ff equivalent to f
                for each canonical function g of size k − size(f) − 1
                    visit(k, ff OR g)
                    visit(k, ff AND g)
                    visit(k, ff AND ¬g)
                    visit(k, ¬ff AND g)
        if nvisited == 2^(2^n)
            return

func visit(d, fg):
    if size[fg] != ∞
        return

    record fg as canonical

    for each function ffgg equivalent to fg
        if size[ffgg] != ∞
            size[ffgg] = d
            nvisited += 2  // counts ffgg and ¬ffgg
    return

4. 残局攻坚:自顶向下的定向搜索

在算法运行接近尾声时(当 \(k > 22\) 时),盲目遍历庞大的函数对空间变得极度低效。此时,我们可以逆向思维,切换为自顶向下的 定向搜索 (Directed Search),专门针对尚未被覆盖的特定函数进行构建:

Near the end of the execution (when \(k > 22\)), exploring large spaces of function pairs becomes inefficient. Instead, we can flip to a top-down directed search to construct specific missing functions:

// Algorithm 5 (Final Algorithm with Directed Search)
func compute():
    for each function f
        size[f] = ∞

    for each single variable function f = v
        size[f] = 0

    // 生成阶段 (Generation phase)
    for k = 1 to max_generate
        for each canonical function f
            for each function ff equivalent to f
                for each canonical function g of size k − size(f) − 1
                    visit(k, ff OR g)
                    visit(k, ff AND g)
                    visit(k, ff AND ¬g)
                    visit(k, ¬ff AND g)

    // 定向搜索阶段 (Directed Search phase)
    for k = max_generate+1 to ∞
        for each missing function fg
            for each canonical function g
                fsize = k − size(g) − 1
                if fg OR g == fg
                    if f = find(fg AND ¬g, g, fsize) succeeds
                        visit(k, fg)
                        next fg
                if fg OR ¬g == fg
                    if f = find(fg AND g, ¬g, fsize) succeeds
                        visit(k, fg)
                        next fg
                if ¬fg OR g == ¬fg
                    if f = find(¬fg AND ¬g, g, fsize) succeeds
                        visit(k, fg)
                        next fg
                if ¬fg OR ¬g == ¬fg
                    if f = find(¬fg AND g, ¬g, fsize) succeeds
                        visit(k, fg)
                        next fg
        if nvisited == 2^(2^n)
            return

通过在尺寸达到 23 左右时果断从穷举生成模式切入定向搜索模式,整个 5 变量布尔函数的运算在 稍多于半天的单机 CPU 时间内 便全部圆满收敛,无可争议地证明了 \(a(5) = 28\)。

By switching from generation to directed search around size 23, the entire computation for 5 variables finishes in just over half a day of CPU time, successfully proving that \(a(5) = 28\).


5. 引入 XOR 运算符的降维打击

5. Adding XOR

如果在 AND 和 OR 之外允许引入 XOR 异或运算符,布尔函数往往能用数量少得多的运算符写出,奇偶校验函数的复杂度更是直接暴降为线性。

If XOR is permitted alongside AND and OR, functions can be written with significantly fewer operators, making the parity function trivial.

高德纳 (Donald Knuth) 在其巨著《计算机程序设计艺术》(The Art of Computer Programming) 第 4A 卷中专门探讨了允许使用 XOR 时的 5 变量函数。将算法 4 稍作改造以支持 XOR 后,验证结果与高德纳的结论完全吻合,且整个运行速度奇快无比,因为此时的最大公式尺寸直接腰斩并降至 12。

Knuth’s The Art of Computer Programming, Volume 4A examines 5-input functions with XOR allowed. Adapting Algorithm 4 to include XOR confirms matching results, running exceptionally fast since the maximum formula size drops to 12.


6. 尾声:位运算实现置换与取反

6. Postscript: Generating Permutations and Inversions

为了高效遍历 for each function ff equivalent to f 中的所有等价函数,作者巧妙地运用了一套高度并行的真值表位掩码技巧。

To cycle through all equivalent functions via for each function ff equivalent to f, bitwise truth-table manipulations are used in parallel.

借助位掩码实现输入变量取反

Inverting Inputs via Bit Masks

变换函数 真值表计算 (f = f(V, W, X, Y, Z))
f(¬V, W, X, Y, Z) (f&0x55555555)<< 1 \| (f>> 1)&0x55555555
f(V, ¬W, X, Y, Z) (f&0x33333333)<< 2 \| (f>> 2)&0x33333333
f(V, W, ¬X, Y, Z) (f&0x0f0f0f0f)<< 4 \| (f>> 4)&0x0f0f0f0f
f(V, W, X, ¬Y, Z) (f&0x00ff00ff)<< 8 \| (f>> 8)&0x00ff00ff
f(V, W, X, Y, ¬Z) (f&0x0000ffff)<<16 \| (f>>16)&0x0000ffff

相邻输入变量的快速交换

Swapping Adjacent Inputs

变换函数 真值表计算 (f = f(V, W, X, Y, Z))
f(W, V, X, Y, Z) f&0x99999999 \| (f&0x22222222)<<1 \| (f>>1)&0x22222222
f(V, X, W, Y, Z) f&0xc3c3c3c3 \| (f&0x0c0c0c0c)<<1 \| (f>>1)&0x0c0c0c0c
f(V, W, Y, X, Z) f&0xf00ff00f \| (f&0x00f000f0)<<1 \| (f>>1)&0x00f000f0
f(V, W, X, Z, Y) f&0xff0000ff \| (f&0x0000ff00)<<8 \| (f>>8)&0x0000ff00

通过将 格雷码 (Gray Code)(用于以最小位变动实现变量取反)与高德纳提出的 算法 P (Algorithm P)(起源于 17 世纪教堂敲钟换位术的极简全排列生成法)相结合,程序以极其微弱的系统开销,高效地遍历了所有的输入置换与取反状态。

By combining Gray codes (for minimal-change input inversions) and Knuth's Algorithm P ("Plain changes," originating from 17th-century bell ringing), the program efficiently iterates over all permutations and inversions with minimal overhead.