Bit-Parallel Algorithms
Bitsets, word-level parallelism, Shift-And matching and the Four Russians.
A 64-bit AND is 64 Boolean ANDs for the price of one instruction. Arrange an algorithm so that its innermost loop combines whole rows of bits, and it runs dozens of times faster on the same machine. This word-level parallelism is behind the bitset subset-sum DP, the agrep text searcher, Myers's edit-distance algorithm, and the "Four Russians" trick for Boolean matrices, the start of a line of work that reached a research milestone in 2024.
What you’ll learn
- State the word-RAM model and explain why a bitset turns Boolean operations into word operations.
- Speed up subset sum to and graph reachability by a factor with bitsets, with correctness invariants.
- Derive the Shift-And recurrence for exact matching, and explain how Wu–Manber and Myers extend bit-parallelism to errors and edit distance.
- Analyze the Four Russians algorithm for Boolean matrix multiplication, , and place it in the history of combinatorial algorithms up to 2024.
The word RAM and the bitset
The word RAM stores data in words of bits, with so that a word can hold an index, and charges one step for each AND, OR, XOR, NOT, shift, addition or comparison of whole words. A bitset of bits occupies words, so a bulk AND, OR, XOR or shift costs , and so does counting its ones, since modern processors count a word's set bits in one instruction. That is word-level parallelism: a speedup of with ordinary registers, and more with 256- or 512-bit vector registers.
The classic example is subset sum: can some subset of positive integers reach every target up to ? Keep the set of reachable sums as one bitset , bit meaning "sum is reachable".
Let be the set of sums of subsets of the first items, as a bitset over . Initially only the empty subset exists:
A subset of the first items either skips item or uses it, which adds to every sum. Shifting left by adds to every element of the set at once:
Each item costs word operations instead of bit operations, so all items cost . It is still pseudo-polynomial: the running time grows with the value , not with its number of bits.
#include <bitset>
#include <cstdio>
#include <vector>
int main() {
const int MAXT = 100000; // largest target, fixed at compile time
std::vector<int> items = {3, 5, 7};
std::bitset<MAXT + 1> reach;
reach[0] = 1; // the empty subset
for (int a : items) reach |= reach << a; // about MAXT / 64 word operations per item
for (int s = 0; s <= 15; s++)
if (reach[s]) std::printf("%d ", s); // 0 3 5 7 8 10 12 15
}std::bitset needs its size at compile time. In Python, a plain integer works as a bitset: reach |= (reach << a) & full, with full = (1 << (t + 1)) - 1, runs its shifts and ORs in C over machine words.
| Problem | Plain | With bitsets | Inner operation |
|---|---|---|---|
| Subset sum up to | R |= R << a | ||
| Transitive closure (Warshall) | if reaches : | ||
| Reachability from every vertex of a DAG | |||
| Counting triangles | popcount of for each edge | ||
| Gaussian elimination over | XOR of rows |
Shift-And: one shift per text character
Bit-parallelism also simulates automata. To find a pattern of length in a text , keep a bit vector with one bit per pattern prefix: after reading , bit of is 1 exactly when occurs in the text ending at position . Precompute a mask for each character , with bit set exactly when .
The prefix ends at exactly when ends at (for : always) and :
All bits at once: shift to move every partial match one position along, OR in 1 to start a new match, and AND with the character's mask:
A full occurrence ends at when bit is set. Each character costs word operations, so the scan takes time after preprocessing: linear in when the pattern fits in a word.
def shift_and(text, pat):
m = len(pat)
B = {}
for j, ch in enumerate(pat): # B[c]: bit j set iff pat[j] == c
B[ch] = B.get(ch, 0) | 1 << j
D, hits = 0, []
for i, ch in enumerate(text):
D = ((D << 1) | 1) & B.get(ch, 0)
if (D >> (m - 1)) & 1: # pat ends at text position i
hits.append(i - m + 1)
return hits
print(shift_and("abaababaab", "abab")) # [3]For the pattern abab, and . The state reads over the first seven characters; bit 3 is set after position 6, so the match starts at 3.
Errors. Wu and Manber (1992), in the agrep tool, keep vectors , where marks the prefixes that match with at most edits. Each edit type is one more shift or OR of the previous vectors:
where the terms are a match, an inserted text character, a substitution and a deleted pattern character (the final 1: a one-character prefix is always within one edit). Initially . That is time.
Edit distance. Myers (1999) went further: adjacent cells of the edit-distance table differ by , or , so a whole column fits in two bit vectors, and a constant number of word operations advances it by one text character. Approximate matching then takes time for any number of errors (see Edit Distance & Its Limits).
In Shift-And with pattern , what does it mean when bit of is 1 after reading text position ?
- The prefix occurs in the text ending at position
- The pattern occurs starting at position
- At least characters of the pattern have matched somewhere in the text so far
Show the answer
B. The prefix occurs in the text ending at position
tracks every partial match at once: bit says the last text characters equal . The mask checks only the newest character; the shift carries the older ones.
The Four Russians: precomputed tables of rows
Boolean matrix multiplication (BMM) computes . With bitset rows, row of is the OR of the rows of for which : row-ORs of words for each of rows, .
Arlazarov, Dinic, Kronrod and Faradzev (1970), the "Four Russians", saved one more logarithmic factor with a table. Cut the index range of into blocks of . Within a block, the row of selects one of only subsets of the block's rows of , so precompute the OR of every such subset once, and replace up to row-ORs by one lookup.
def bmm_four_russians(A, Brows, n, t): # A: 0/1 lists; Brows[k]: row k of B as an int bitset
C = [0] * n
for start in range(0, n, t): # a block of t columns of A (rows of B)
blk = Brows[start:start + t]
table = [0] * (1 << len(blk)) # table[s] = OR of the block rows selected by s
for s in range(1, 1 << len(blk)):
low = (s & -s).bit_length() - 1
table[s] = table[s & (s - 1)] | blk[low] # one OR per entry, from a smaller entry
for i in range(n):
key = 0
for b in range(len(blk)):
if A[i][start + b]:
key |= 1 << b
C[i] |= table[key] # one lookup replaces up to t row ORs
return CEach table entry costs one OR of words, built from the entry without its lowest bit. In practice the key is read directly from 's bit-packed row, at no extra cost.
There are blocks. Each builds a table of rows of words, then performs one lookup-and-OR for each of the rows of :
Choosing makes , so the two terms balance:
With this is the familiar . Transitive closure has the same complexity as BMM up to constant factors (Fischer & Meyer, 1971; Munro, 1971), so it inherits the speedup; fittingly, the 1970 paper was about transitive closure.
Combinatorial algorithms and the frontier
Algebraic algorithms multiply Boolean matrices as integer matrices, in time with as of 2025, but they rely on subtraction and huge hidden constants. Combinatorial algorithms, loosely those that avoid such algebra, like the Four Russians, have a story of their own:
| Year | Combinatorial BMM | Time |
|---|---|---|
| 1970 | Four Russians (Arlazarov, Dinic, Kronrod & Faradzev) | |
| 2009 | Bansal & Williams, via regularity lemmas | |
| 2015 | Chan | |
| 2015 | Yu | |
| 2024 | Abboud, Fischer, Kelley, Lovett & Meka |
Key takeaways
- On the word RAM, a bitset performs Boolean operations per instruction: subset sum becomes in , and closure, reachability, triangles and elimination all gain a factor .
- Shift-And keeps one bit per pattern prefix and updates all of them with , so exact matching costs ; Wu–Manber adds errors and Myers computes edit distance the same way.
- The Four Russians precompute the OR of every subset of rows, so Boolean matrix multiplication and transitive closure take .
- Combinatorial BMM improved by polylogarithmic factors until 2024, when it first beat every polylogarithm; a truly subcubic combinatorial algorithm remains an open question that much of fine-grained complexity leans on.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A bitset subset-sum DP over items and targets up to , with 64-bit words, performs about how many word operations?
- About
- About
- About
- About
Show the answer
A. About
word operations, a fraction of a second. The plain DP needs bit operations.
What is the core idea of the Four Russians algorithm for Boolean matrix multiplication?
- Split each matrix into four quadrants and recurse, as in Strassen's algorithm
- For each block of rows of , precompute the OR of all subsets of those rows, so one table lookup replaces up to row operations
- Multiply the matrices over the integers and round the result
- Sort the rows of so that equal rows are computed once
Show the answer
B. For each block of rows of , precompute the OR of all subsets of those rows, so one table lookup replaces up to row operations
A row of restricted to a block can only select one of subsets of the block's rows. Precomputing all of them costs per block, which with balances the lookups and gives overall.
Which statement about combinatorial Boolean matrix multiplication is correct as of 2025?
- No algorithm beats by more than a constant factor
- An combinatorial algorithm is known
- The best known algorithm, from 2024, beats by a factor , more than any polylogarithm, but no combinatorial algorithm is known
- It is proved that no combinatorial algorithm can beat
Show the answer
C. The best known algorithm, from 2024, beats by a factor , more than any polylogarithm, but no combinatorial algorithm is known
Abboud, Fischer, Kelley, Lovett and Meka (2024) broke the polylogarithmic barrier, yet is still smaller than any . Whether a truly subcubic combinatorial algorithm exists is open.
Further reading
- V. L. Arlazarov, E. A. Dinic, M. A. Kronrod & I. A. Faradzev, "On economical construction of the transitive closure of an oriented graph," Soviet Mathematics Doklady 11 (1970), 1209–1210.
- R. Baeza-Yates & G. H. Gonnet, "A new approach to text searching," Communications of the ACM 35(10) (1992), 74–82.
- S. Wu & U. Manber, "Fast text searching: allowing errors," Communications of the ACM 35(10) (1992), 83–91.
- G. Myers, "A fast bit-vector algorithm for approximate string matching based on dynamic programming," Journal of the ACM 46(3) (1999), 395–415.
- A. Abboud, N. Fischer, Z. Kelley, S. Lovett & R. Meka, "New graph decompositions and combinatorial Boolean matrix multiplication algorithms," Proceedings of STOC (2024), 935–943.