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 and for bit of the word , with bit 0 the least significant. For the map is one-to-one onto the non-padding positions of a row, so the rows define an 0-1 matrix with . The structure stands for the graph whose adjacency matrix is . Invariant (B) is (B1), all padding bits are 0, and (B2), 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 and a word : has bit set and all other bits as in ; has bit clear and all other bits as in ; and is bit of .
Proof. The word has only bit set, and OR and AND act bit by bit. Shifting right by moves bit to position 0, and AND with 1 keeps only position 0.
Lemma 2 (lowest bits). For a word with lowest set bit , the word equals with bit cleared, and .
Proof. The bits of below are 0. Subtracting 1 turns bit to 0, the bits below to 1, and leaves the bits above unchanged, so the AND keeps the bits above and clears bit and the bits below it, which were 0. In two's complement , which has bit set, the bits below zero, and the bits above inverted, so the AND is .
Theorem. Assume that before an operation (B) holds and 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 , 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 , or , and no other bit. These bits are not padding bits since , so (B1) holds. On the matrix , the lines are exactly lines L2 to L5 of the adjacency matrix, whose proof shows that becomes the matrix of the new graph and that (B2) holds.
ADJACENT (line L6) returns by Lemma 1, which is the right answer by the adjacency matrix proof.
NEIGHBORS (lines L7 to L11). Fix a word . By Lemma 2, each pass of the loop of lines L9 to L11 outputs for the lowest set bit of and then clears that bit. So the passes output the set bits of in increasing order, each once, and the loop stops after as many passes as has set bits. By (B1) the outputs are below , and they are exactly the with and . Over in order, the output is the set in increasing order, which is the set of (out-)neighbours of .
DEGREE and COMMON (lines L12 and L13). POPCOUNT of a word counts its set bits. By (B1) the set bits of row are exactly the positions of its neighbours, so line L12 returns the degree. The AND of word of rows and has bit set exactly when both rows have it, so the sum in line L13 is .
ADD-VERTEX (lines L14 and L15). If is a multiple of , the old rows have no padding and exactly words, and the new column is bit 0 of a new word, which line L14 appends as zero. Otherwise column is a padding bit of the last word, which is 0 by (B1). Either way the new column of is zero, and line L15 adds a zero row of the right length. So the old cells of keep their values, the new vertex has no edges, all padding bits are still 0, and 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 is the matrix of the current graph after each operation, so each query answers for the current edge set.