Proofs: Graph-structured stack
These are the human proofs of Graph-structured stack, at full length.
The graph-structured stack represents a splice-closed set of stacks, and POP returns exactly the paths it must full
This proof shows: correctness.
Setting. The graph has nodes, each with a key and a level , and directed edges with labels. The root is the node of . A path from is a sequence of nodes with an edge for each . Nodes and edges may repeat. A stack of is a path from some node to the root, written in reverse, from the root up; is the set of these. For two sequences, juxtaposition means concatenation. We consider a GSS that starts with the root alone, and that is changed only by PUSH. The abstract set starts as . The call , with the node of , replaces by , the least set of sequences that (a) contains , (b) contains for every whose last cell is , and (c) is closed under splicing: if and are in , so is .
Invariants. (I1) no two nodes have the same key; (I2) for every edge ; (I3) every node has a path to the root; (I4) .
Lemma 1. is closed under splicing for every graph .
Proof. Let and be stacks of . Read from the top down, in reverse is a path from to the root, and in reverse is a path from the top of (or from ) to . Joining them at gives a path to the root, which read from the root up is .
Lemma 2. The invariants hold initially and after each call of PUSH.
Proof. Initially has only the root and no edges, so (I1), (I2) and (I3) hold, and .
Consider on a graph that satisfies the invariants, and let be the graph after it. Line L1 ensures , and otherwise the call fails and changes nothing. NODE (lines L5 and L6) returns the node with key if there is one, and makes one only if there is none, so (I1) holds. ADD-EDGE (lines L7 and L8) adds at most the edge , with , so (I2) holds. After the call, has the edge to , and has a path to the root in by (I3), so has one in ; the other nodes keep their paths, so (I3) holds.
For (I4), if the edge was already in , then as a graph, so . This set already satisfies (a), and (c) by Lemma 1, and (b) because every stack of ending at extends by the edge to the stack . So , as is the least such set. Now let the edge be new.
(.) contains , because has all the edges of . It satisfies (b), because a stack of that ends at extends by the edge . It satisfies (c) by Lemma 1. Since is the least set with (a), (b) and (c), .
(.) Let be a stack of , and let be the number of times it uses the new edge, as a step from up to . We show by induction on . If , is a stack of , so . If , split at its last use of the new edge: , where ends at and , the part above this occurrence of , does not use the new edge. The stack uses the new edge times, so by induction, and by (b). If is empty, we are done. Otherwise the first node of has an edge to in , because does not use the new edge, so is a node of , and by (I3) for it has a path to the root in . Let be this stack, which ends at . Then is a stack of : below it follows , and above it follows , which uses only edges of . So . Splicing and at the cell by (c) gives .
Lemma 3 (POP). terminates and returns, once for each path of edges from that contains the edge as a step (or for each such path, if is none), the pair of its end node and its labels in the order , bottom first.
Proof. Call an entry of the representative of a path from when , lists the labels of its edges from the top down, and is true exactly when is none or the path contains . Line L9 puts on the representative of the path of length 0. Line L14 replaces the representative of a path of length by the representatives of all extensions of by one edge, one for each out-edge of : the new flag is true exactly when used or the new edge is . Each entry is removed once at line L11. So, ignoring line L13, the entries that pass through are the representatives of the paths from of length at most , each once, since two different paths differ at a first step and the two extensions there are different entries. The number of these paths is finite, since every node has finitely many out-edges, so the loop ends.
Line L13 discards the representative of a path only if has not used and . Every extension of moves from along edges, which by (I2) never increase the level, so all nodes after have level less than . None of them is , so no extension takes the step , and no extension of length would be returned. When is none, the floor is 0 and no level is below it, so line L13 discards nothing. Hence the entries with that reach line L12 include the representatives of all paths of length that use (or of all paths of length ), and line L12 returns exactly those with the flag true, each with its labels reversed into bottom-first order.
Theorem. In a GSS changed only by PUSH, after every operation the stacks of the graph are exactly the abstract set . returns, for each stack of with top and at least cells that takes the step in its top steps (or for each such stack, if is none), the cell that becomes the top when its top cells are removed, together with the labels of the removed steps; it returns each such pair once for each different top segment of steps.
Proof. The first part is Lemma 2 (I4); POP and tops do not change the graph. For the second part, the top steps of a stack of with top are, read from the top down, a path of edges from . Conversely, by (I3) every path of edges from to a node extends below to a stack of . So the top segments of steps of the stacks with top are exactly the paths of edges from , and Lemma 3 gives the result.