Proofs: Adjacency matrix
These are the human proofs of Adjacency matrix, at full length.
The operations of an adjacency matrix keep the invariant and store the edge set of the graph type full
This proof shows: correctness, termination.
Write for the edge set that the graph type prescribes after a sequence of operations: it starts empty, add-edge adds the edge of and , and remove-edge removes it if it is there. For a directed graph the edge of and is the pair ; for an undirected graph it is the set with . The array stands for the edge set of the page: the pairs with when the graph is directed, and the sets with and when it is undirected. Invariant (M) is empty in the directed case and is (M1) symmetry and (M2) a zero diagonal in the undirected case.
Lemma 1. If (M1) holds, then for , if and only if , in either order of and .
Proof. If this is the definition. If , then with , so it is in if and only if , which is by (M1).
Theorem. Assume that before an operation (M) holds and . Then after the operation (M) holds, equals the new , the queries return the answers of the graph type for , and the operation stops.
Proof. We take the operations in turn.
NEW (line L1). All cells are 0, so . A zero array is symmetric with a zero diagonal, so (M) holds.
ADD-EDGE (lines L2 and L3). In the directed case line L2 sets exactly the cell , so becomes . In the undirected case is required, and lines L2 and L3 set the two cells and to 1. These two cells are mirror images, so symmetry still holds; neither is on the diagonal, so (M2) holds. By Lemma 1 the set gains and no other set, since no other cell changed. If the edge was already present the cells were already 1, and nothing changes, which matches .
REMOVE-EDGE (lines L4 and L5). The same argument with 0 in place of 1 gives . In the undirected case with , line L4 writes 0 on the diagonal, where (M2) already has 0, so nothing changes, and has no edge to remove.
ADJACENT (line L6). In the directed case if and only if . In the undirected case with this is Lemma 1, and with the cell is 0 by (M2), which is the right answer, as has no loops.
NEIGHBORS and DEGREE (lines L7 to L9). The loop of line L7 visits each from to once, in increasing order, and line L8 outputs exactly when . By the case for ADJACENT, these are exactly the that are out-neighbours of (directed) or neighbours of (undirected). Line L9 counts the same set, so it returns the out-degree or the degree. The loop has steps, so it stops.
ADD-VERTEX (lines L10 to L13). Line L11 copies each cell with to the same place of , and the cells in row or column stay 0. So after line L12 the edges among the old vertices are unchanged, and the new vertex has no edges, which is what the graph type prescribes. restricted to the old cells is symmetric with a zero diagonal when was, and the new row and column are zero, so (M) holds. Line L13 returns the number of the new vertex. Lines L10 and L11 run over finitely many cells, so they stop.
Each operation keeps (M) and the equality , so by induction on the number of operations, these hold after each sequence of operations that starts with NEW, and each query answers for the current edge set.