Algorithm Search⌘K

Proofs: Bitset adjacency matrix

These are the human proofs of Bitset adjacency matrix, at full length.

The operations of a bitset adjacency matrix keep the invariant and store the edge set of the graph type full

This proof shows: correctness, termination.

Write W=⌈n/w⌉W = \lceil n/w \rceil and β(u,v)\beta(u, v) for bit v mod wv \bmod w of the word R[u][⌊v/w⌋]R[u][\lfloor v/w \rfloor], with bit 0 the least significant. For 0≤v<n0 \le v < n the map v↦(⌊v/w⌋,v mod w)v \mapsto (\lfloor v/w \rfloor, v \bmod w) is one-to-one onto the non-padding positions of a row, so the rows define an n×nn \times n 0-1 matrix AA with A[u][v]=β(u,v)A[u][v] = \beta(u, v). The structure stands for the graph whose adjacency matrix is AA. Invariant (B) is (B1), all padding bits are 0, and (B2), AA is symmetric with a zero diagonal when the graph is undirected; (B2) is the invariant (M) of the adjacency matrix.

Lemma 1 (single bits). For 0≤b<w0 \le b < w and a word xx: x OR 2bx \text{ OR } 2^b has bit bb set and all other bits as in xx; x AND NOT 2bx \text{ AND NOT } 2^b has bit bb clear and all other bits as in xx; and (x≫b) AND 1(x \gg b) \text{ AND } 1 is bit bb of xx.

Proof. The word 2b2^b has only bit bb set, and OR and AND act bit by bit. Shifting right by bb moves bit bb to position 0, and AND with 1 keeps only position 0. □\square

Lemma 2 (lowest bits). For a word x≠0x \neq 0 with lowest set bit bb, the word x AND (x−1)x \text{ AND } (x - 1) equals xx with bit bb cleared, and x AND (−x)=2bx \text{ AND } (-x) = 2^b.

Proof. The bits of xx below bb are 0. Subtracting 1 turns bit bb to 0, the bits below bb to 1, and leaves the bits above bb unchanged, so the AND keeps the bits above bb and clears bit bb and the bits below it, which were 0. In two's complement −x=NOT x+1-x = \text{NOT } x + 1, which has bit bb set, the bits below bb zero, and the bits above bb inverted, so the AND is 2b2^b. □\square

Theorem. Assume that before an operation (B) holds and AA is the adjacency matrix of the graph that the graph type prescribes. Then after the operation the same holds for the new graph, each query returns the answer of the graph type, and each operation stops.

Proof. NEW (line L1) makes all words zero, so A=0A = 0, which is the empty graph, and (B) holds.

ADD-EDGE and REMOVE-EDGE (lines L2 to L5). By Lemma 1 each line changes exactly the bit β(u,v)\beta(u, v), or β(v,u)\beta(v, u), and no other bit. These bits are not padding bits since u,v<nu, v < n, so (B1) holds. On the matrix AA, the lines are exactly lines L2 to L5 of the adjacency matrix, whose proof shows that AA becomes the matrix of the new graph and that (B2) holds.

ADJACENT (line L6) returns β(u,v)=A[u][v]\beta(u, v) = A[u][v] by Lemma 1, which is the right answer by the adjacency matrix proof.

NEIGHBORS (lines L7 to L11). Fix a word jj. By Lemma 2, each pass of the loop of lines L9 to L11 outputs jw+bjw + b for the lowest set bit bb of xx and then clears that bit. So the passes output the set bits of R[u][j]R[u][j] in increasing order, each once, and the loop stops after as many passes as R[u][j]R[u][j] has set bits. By (B1) the outputs are below nn, and they are exactly the vv with ⌊v/w⌋=j\lfloor v/w \rfloor = j and A[u][v]=1A[u][v] = 1. Over j=0,…,W−1j = 0, \ldots, W - 1 in order, the output is the set {v:A[u][v]=1}\{v : A[u][v] = 1\} in increasing order, which is the set of (out-)neighbours of uu.

DEGREE and COMMON (lines L12 and L13). POPCOUNT of a word counts its set bits. By (B1) the set bits of row uu are exactly the positions of its neighbours, so line L12 returns the degree. The AND of word jj of rows uu and vv has bit bb set exactly when both rows have it, so the sum in line L13 is ∣{x:A[u][x]=A[v][x]=1}∣=∣N(u)∩N(v)∣|\{x : A[u][x] = A[v][x] = 1\}| = |N(u) \cap N(v)|.

ADD-VERTEX (lines L14 and L15). If nn is a multiple of ww, the old rows have no padding and exactly WW words, and the new column nn is bit 0 of a new word, which line L14 appends as zero. Otherwise column nn is a padding bit of the last word, which is 0 by (B1). Either way the new column of AA is zero, and line L15 adds a zero row of the right length. So the old cells of AA keep their values, the new vertex has no edges, all padding bits are still 0, and AA stays symmetric with zero diagonal when it was. The loops of lines L1, L14 and L15 run over finitely many words, so they stop.

By induction on the number of operations, starting with NEW, (B) holds and AA is the matrix of the current graph after each operation, so each query answers for the current edge set. ■\blacksquare

Back to Bitset adjacency matrix