Proofs: Hash-based adjacency
These are the human proofs of Hash-based adjacency, at full length.
The operations of hash-based adjacency keep the invariant and store the graph of the graph type full
This proof shows: correctness, termination.
We use only the set semantics of a hash table: a hash set stores a finite set, insertion adds an element (and changes nothing if it is present), deletion removes it (and changes nothing if it is absent), membership answers correctly, and iteration lists each element once and stops. A hash map stores a finite set of keys with one value each. Time bounds are a separate matter and are not part of this proof.
The structure stands for the graph whose vertex set is the key set of and whose edges are the pairs with for a directed graph, or the sets with for an undirected graph. The invariant (H) is (H1) all elements of the sets are keys, (H2) symmetry without loops when undirected, and (H3) has the keys of and mirrors it when directed. Write and for the vertex and edge sets that the graph type prescribes after a sequence of operations: add-vertex adds a vertex, add-edge adds both ends and the edge, remove-edge removes the edge, and remove-vertex removes the vertex and all edges at it.
Theorem. Assume that before an operation (H) holds and . Then after the operation (H) holds, is the new , the queries return the answers of the graph type, and the operation stops.
Proof. NEW (line L1) gives empty maps, so is the empty graph, and (H) holds trivially.
ADD-VERTEX (line L2). If nothing changes, and already. Otherwise becomes a key with an empty set, and with an empty in-set when directed. No set gains an element, so (H1) to (H3) hold, and gains the isolated vertex .
ADD-EDGE (lines L3 to L5). After line L3, and are keys. In the undirected case , line L4 adds to and line L5 adds to ; both are keys, so (H1) holds, the new elements form a matching pair, so (H2) holds, and no loop is added. In the directed case lines L4 and L5 add to and to , which keeps (H3). In both cases the edge set of gains exactly the edge of and , or is unchanged if it was there, which matches .
REMOVE-EDGE (lines L6 and L7). The two deletions remove a matching pair, or nothing if the edge was absent, by (H2) or (H3). So (H) holds and the edge set becomes .
ADJACENT (line L8) returns true exactly when and , which is the definition of the edge being in ; for an undirected graph the order of and does not matter, by (H2).
NEIGHBORS and DEGREE (lines L9 and L10). is by definition the set of (out-)neighbours of in , so iterating it lists each once, and its size is the degree.
REMOVE-VERTEX (lines L11 to L13). Let ; otherwise nothing changes. Undirected: by (H2) the keys whose sets contain are exactly the elements of , and line L11 deletes from each . After line L13 deletes the key , no set contains , so (H1) holds, and the remaining pairs are still matching, so (H2) holds. Directed: by (H3) the sets that contain are those with , and the sets that contain are those with . Lines L11 and L12 delete from all of them, and line L13 deletes the key from both maps, so (H1) and (H3) hold. In both cases the deleted elements are exactly those that stand for edges at , so loses and the edges at and nothing else. The loops of lines L11 and L12 iterate over copies of finite sets, so they stop even though the sets they delete from may include the set being read, as with a self-loop .
By induction on the number of operations, starting with NEW, (H) holds and is the graph of the graph type after each operation.