Algorithm Search⌘K

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 GG has nodes, each with a key (s,j)(s, j) and a level level(v)=j\mathrm{level}(v) = j, and directed edges v→xv \to x with labels. The root is the node of (q0,0)(q_0, 0). A path from vv is a sequence of nodes v=y0,y1,…,ydv = y_0, y_1, \ldots, y_d with an edge yi−1→yiy_{i-1} \to y_i for each ii. Nodes and edges may repeat. A stack of GG is a path from some node to the root, written in reverse, from the root up; Paths(G)\mathrm{Paths}(G) 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 S\mathcal{S} starts as {(root)}\{(\mathrm{root})\}. The call PUSH(s,j,x)\mathrm{PUSH}(s, j, x), with vv the node of (s,j)(s, j), replaces S\mathcal{S} by S′\mathcal{S}', the least set of sequences that (a) contains S\mathcal{S}, (b) contains σv\sigma v for every σ∈S′\sigma \in \mathcal{S}' whose last cell is xx, and (c) is closed under splicing: if σ1cσ2\sigma_1 c \sigma_2 and τ1cτ2\tau_1 c \tau_2 are in S′\mathcal{S}', so is τ1cσ2\tau_1 c \sigma_2.

Invariants. (I1) no two nodes have the same key; (I2) level(x)≤level(v)\mathrm{level}(x) \le \mathrm{level}(v) for every edge v→xv \to x; (I3) every node has a path to the root; (I4) Paths(G)=S\mathrm{Paths}(G) = \mathcal{S}.

Lemma 1. Paths(G)\mathrm{Paths}(G) is closed under splicing for every graph GG.

Proof. Let σ1cσ2\sigma_1 c \sigma_2 and τ1cτ2\tau_1 c \tau_2 be stacks of GG. Read from the top down, τ1c\tau_1 c in reverse is a path from cc to the root, and cσ2c \sigma_2 in reverse is a path from the top of σ2\sigma_2 (or from cc) to cc. Joining them at cc gives a path to the root, which read from the root up is τ1cσ2\tau_1 c \sigma_2. □\square

Lemma 2. The invariants hold initially and after each call of PUSH.

Proof. Initially GG has only the root and no edges, so (I1), (I2) and (I3) hold, and Paths(G)={(root)}=S\mathrm{Paths}(G) = \{(\mathrm{root})\} = \mathcal{S}.

Consider PUSH(s,j,x)\mathrm{PUSH}(s, j, x) on a graph GG that satisfies the invariants, and let G′G' be the graph after it. Line L1 ensures level(x)≤j\mathrm{level}(x) \le j, and otherwise the call fails and changes nothing. NODE (lines L5 and L6) returns the node with key (s,j)(s, j) 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 v→xv \to x, with level(x)≤j=level(v)\mathrm{level}(x) \le j = \mathrm{level}(v), so (I2) holds. After the call, vv has the edge to xx, and xx has a path to the root in GG by (I3), so vv has one in G′G'; the other nodes keep their paths, so (I3) holds.

For (I4), if the edge v→xv \to x was already in GG, then G′=GG' = G as a graph, so Paths(G′)=Paths(G)=S\mathrm{Paths}(G') = \mathrm{Paths}(G) = \mathcal{S}. This set already satisfies (a), and (c) by Lemma 1, and (b) because every stack σ\sigma of GG ending at xx extends by the edge v→xv \to x to the stack σv\sigma v. So S′=S\mathcal{S}' = \mathcal{S}, as S′\mathcal{S}' is the least such set. Now let the edge be new.

(S′⊆Paths(G′)\mathcal{S}' \subseteq \mathrm{Paths}(G').) Paths(G′)\mathrm{Paths}(G') contains Paths(G)=S\mathrm{Paths}(G) = \mathcal{S}, because G′G' has all the edges of GG. It satisfies (b), because a stack σ\sigma of G′G' that ends at xx extends by the edge v→xv \to x. It satisfies (c) by Lemma 1. Since S′\mathcal{S}' is the least set with (a), (b) and (c), S′⊆Paths(G′)\mathcal{S}' \subseteq \mathrm{Paths}(G').

(Paths(G′)⊆S′\mathrm{Paths}(G') \subseteq \mathcal{S}'.) Let π\pi be a stack of G′G', and let kk be the number of times it uses the new edge, as a step from xx up to vv. We show π∈S′\pi \in \mathcal{S}' by induction on kk. If k=0k = 0, π\pi is a stack of GG, so π∈S⊆S′\pi \in \mathcal{S} \subseteq \mathcal{S}'. If k>0k > 0, split π\pi at its last use of the new edge: π=αvβ\pi = \alpha v \beta, where α\alpha ends at xx and β\beta, the part above this occurrence of vv, does not use the new edge. The stack α\alpha uses the new edge k−1k - 1 times, so α∈S′\alpha \in \mathcal{S}' by induction, and αv∈S′\alpha v \in \mathcal{S}' by (b). If β\beta is empty, we are done. Otherwise the first node of β\beta has an edge to vv in GG, because β\beta does not use the new edge, so vv is a node of GG, and by (I3) for GG it has a path to the root in GG. Let ρ\rho be this stack, which ends at vv. Then ρβ\rho \beta is a stack of GG: below vv it follows ρ\rho, and above vv it follows β\beta, which uses only edges of GG. So ρβ∈S⊆S′\rho \beta \in \mathcal{S} \subseteq \mathcal{S}'. Splicing ρβ\rho \beta and αv\alpha v at the cell vv by (c) gives αvβ=π∈S′\alpha v \beta = \pi \in \mathcal{S}'. □\square

Lemma 3 (POP). POP(u,m,e)\mathrm{POP}(u, m, e) terminates and returns, once for each path u=y0,y1,…,ymu = y_0, y_1, \ldots, y_m of mm edges from uu that contains the edge ee as a step (or for each such path, if ee is none), the pair of its end node ymy_m and its labels in the order ℓ(ym−1→ym),…,ℓ(y0→y1)\ell(y_{m-1} \to y_m), \ldots, \ell(y_0 \to y_1), bottom first.

Proof. Call an entry (y,d,used,labels)(y, d, \mathit{used}, \mathit{labels}) of TT the representative of a path y0,…,ydy_0, \ldots, y_d from uu when y=ydy = y_d, labels\mathit{labels} lists the labels of its edges from the top down, and used\mathit{used} is true exactly when ee is none or the path contains ee. Line L9 puts on TT the representative of the path of length 0. Line L14 replaces the representative of a path PP of length d<md < m by the representatives of all extensions of PP by one edge, one for each out-edge of ydy_d: the new flag is true exactly when PP used ee or the new edge is ee. Each entry is removed once at line L11. So, ignoring line L13, the entries that pass through TT are the representatives of the paths from uu of length at most mm, 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 PP only if PP has not used e=y0′→x0′e = y_0' \to x_0' and level(yd)<level(y0′)\mathrm{level}(y_d) < \mathrm{level}(y_0'). Every extension of PP moves from ydy_d along edges, which by (I2) never increase the level, so all nodes after ydy_d have level less than level(y0′)\mathrm{level}(y_0'). None of them is y0′y_0', so no extension takes the step ee, and no extension of length mm would be returned. When ee is none, the floor is 0 and no level is below it, so line L13 discards nothing. Hence the entries with d=md = m that reach line L12 include the representatives of all paths of length mm that use ee (or of all paths of length mm), and line L12 returns exactly those with the flag true, each with its labels reversed into bottom-first order. □\square

Theorem. In a GSS changed only by PUSH, after every operation the stacks of the graph are exactly the abstract set S\mathcal{S}. POP(u,m,e)\mathrm{POP}(u, m, e) returns, for each stack of S\mathcal{S} with top uu and at least m+1m + 1 cells that takes the step ee in its top mm steps (or for each such stack, if ee is none), the cell that becomes the top when its top mm cells are removed, together with the labels of the removed steps; it returns each such pair once for each different top segment of mm steps.

Proof. The first part is Lemma 2 (I4); POP and tops do not change the graph. For the second part, the top mm steps of a stack of S\mathcal{S} with top uu are, read from the top down, a path of mm edges from uu. Conversely, by (I3) every path of mm edges from uu to a node yy extends below yy to a stack of S\mathcal{S}. So the top segments of mm steps of the stacks with top uu are exactly the paths of mm edges from uu, and Lemma 3 gives the result. ■\blacksquare

Back to Graph-structured stack