Proofs: Edge list
These are the human proofs of Edge list, at full length.
The operations of an edge 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. Write for the edge that an entry stands for, and . The invariant (E) is (E1), the entries are pairs of vertices with when undirected, and (E2), no two entries match.
Lemma 1 (MATCH). MATCH is true if and only if .
Proof. For a directed graph, means , which is the first test of line L6. For an undirected graph, holds exactly when or , which are the two tests of line L6.
So (E2) says that distinct entries stand for distinct edges, and then .
Theorem. Assume that before an operation (E) holds and , and that ADD-EDGE is called only on an edge not in . Then after the operation (E) 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). is empty, so , and (E) holds trivially.
ADD-EDGE (line L2). The entry is valid, with when undirected, so (E1) holds. Its edge is not in , so by Lemma 1 it matches no entry, and (E2) holds. The new set is .
REMOVE-EDGE (lines L3 to L5). The scan of line L3 visits the finitely many entries, so it stops. If no entry matches, then by Lemma 1 the edge is not in , and nothing needs to change. Otherwise, by (E2) the matching entry at index is the only entry for the edge . Lines L4 and L5 replace it by the last entry and drop the last index; as in the deletion from an unordered array, the multiset of entries loses exactly the entry at . So loses and no other edge, and the remaining entries still satisfy (E1) and (E2).
ADJACENT (line L7). By Lemma 1, some entry matches if and only if .
NEIGHBORS and DEGREE (lines L8 to L11). An entry stands for an edge at with other end exactly when and , or, for an undirected graph, and . Lines L9 and L10 output in these cases, and at most once per entry, since L10 runs only when L9 does not; for an undirected graph both cannot hold, as by (E1). By (E2) distinct entries at stand for distinct edges, and in a graph without parallel edges distinct edges at have distinct other ends. So the output lists each neighbour of exactly once, and line L11 returns the degree. The loop is over a finite list, so it stops.
ADD-VERTEX (line L12). is unchanged, so the new vertex has no edges, and (E1) still holds since only grew.
By induction on the number of operations, starting with NEW, (E) and hold after each operation, and each query answers for the current edge set.