Proofs: Adjacency list
These are the human proofs of Adjacency list, at full length.
The operations of an adjacency list 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. The edge of and is for a directed graph and with for an undirected graph. The lists stand for the set of the edges , or , with . The invariant (L) is (L1), no list holds a vertex twice, and (L2), for an undirected graph and no list holds .
Lemma 1 (DELETE). Let be a list with no repeated entry. DELETE stops, and afterwards holds exactly the entries it held before, less , with no repeated entry.
Proof. Line L6 scans the finitely many indices of , so it stops. If no entry is , the list is unchanged. Otherwise let be the index found and the last index. Line L7 writes the entry at index , and line L8 removes index . If , the net effect is to remove . If , the entry at index , which was , is now , and index is gone, so the entries of are the old ones less one copy of . By (L1) that was the only copy of , so is no longer in , and since no new value entered , no entry is repeated.
Theorem. Assume that before an operation (L) holds and , and that ADD-EDGE is called only on an edge not in . Then after the operation (L) 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 lists are empty, so , and (L) holds trivially.
ADD-EDGE (lines L2 and L3). The edge is not in , so , and, for an undirected graph, by (L2). Line L2 appends to and, for an undirected graph, line L3 appends to . No list gets a repeated entry, so (L1) holds. In the undirected case the two new entries are a matching pair, and , so (L2) holds. The new entries stand for the new edge only, so .
REMOVE-EDGE (lines L4 and L5). By Lemma 1, line L4 removes from and nothing else. In the undirected case line L5 removes from ; the pair of entries goes away together, so (L2) holds. If the edge was absent, by (L2) neither entry is there and nothing changes. In each case , and (L1) holds by Lemma 1.
ADJACENT (line L9). It returns true exactly when , that is, when the edge of and is in . For an undirected graph this does not depend on the order of and , by (L2).
NEIGHBORS and DEGREE (lines L10 and L11). The neighbours of in , or its out-neighbours, are exactly the entries of . Line L10 outputs each entry once, so by (L1) each neighbour once. Line L11 returns the length of the list, which by (L1) is the number of distinct neighbours, the degree. The loop of line L10 runs over a finite list, so it stops.
ADD-VERTEX (lines L12 and L13). The new list is empty, so the new vertex has no edges, the old lists are unchanged, and (L) holds.
By induction on the number of operations, starting with NEW, (L) and hold after each operation, and each query answers for the current edge set.