|
ADS
Algorithms and Data Structures
|
The headers of include/ADS/stack realize the STACK of section 4.3.1 of the book, i.e., with a vector of a given capacity (section 4.3.2), on the operations of any SEQUENCE, and, as several stacks in one vector, following exercise 4.7; beside them are the algorithms that use a stack through its operations alone, among which the evaluation of the arithmetic expressions in postfix notation and the translation of the infix ones. This page collects the definitions and the notation that the headers share (see Definitions and notation), the realizations with the costs of their operations (see Realizations and their costs), and, for the algorithms, the property on which each is correct and the argument behind its bound (see Arithmetic expressions and Algorithms on a stack). The pseudocode of each operation and of each algorithm is on the page of its header, in the comment of its function, together with the section of the book, or the paper, that it follows.
A stack is a sequence of items to which items are added, and from which they are removed, at one end only, the top: the last item added is the first removed (LIFO, last in first out). The STACK of the book has push(), which adds an item on top, pop(), which removes the item on top and returns it, top(), which reads it, and empty(); its axioms are \( \mathrm{top}( \mathrm{push}( S , v ) ) = v \) and \( \mathrm{pop}( \mathrm{push}( S , v ) ) = S \), with top() and pop() undefined on the empty stack (see Abstract data types and their axioms, and the concept Stack of ADT.h). A stack is what a computation needs when it has to come back to a point in the reverse order in which it left it: the calls of a program, whose frames make the stack of the calls, the parentheses of an expression, the moves of a backtracking.
| Symbol | Meaning |
|---|---|
| \( n \) | the number of items of a stack, or of tokens of an expression |
| \( c \) | the capacity of a stack on a vector |
| \( k \) | the number of stacks that share one vector, or a key of removeGreater() |
| \( S \) | a stack, or the stack of the operators of a translation |
| \( s \mathbin{\mathrm{op}} t \) | a binary operation, with \( s \) its left operand and \( t \) its right one |
| Realization | Representation | Space | push(), pop(), top() | Capacity |
|---|---|---|---|---|
| ArrayStack | a vector, the top in the place \( n - 1 \) | \( \Theta( c ) \) | \( O( 1 ) \) | \( c \), fixed |
| SequenceStack | a SEQUENCE, the top its first item | that of the sequence | those of insert(), remove() and read() at the head, \( O( 1 ) \) on a list | none |
| MultiStack | \( k \) stacks of linked cells in one vector, with a free list | \( \Theta( n + k ) \) | \( O( 1 ) \) amortized | none, the vector grows |
| MinStack | two vectors, the items and the record minima | \( \Theta( n + m ) \), \( m \le n \) minima | \( O( 1 ) \) amortized, min() too | none |
On a vector the \( n \) items are in the places \( 0 , \ldots , n - 1 \) and the top in the place \( n - 1 \), so that push() writes the place \( n \) and pop() reads the place \( n - 1 \), each in \( O( 1 ) \), and the precondition of push() is that the stack is not full. A SEQUENCE simulates the stack with its top as the first item:
\[ \mathrm{top}() = \mathrm{read}( \mathrm{head}() ) , \qquad \mathrm{pop}() = \mathrm{remove}( \mathrm{head}() ) , \qquad \mathrm{push}( v ) = \mathrm{insert}( \mathrm{head}() , v ) , \tag{1} \]
which costs \( O( 1 ) \) on any list with pointers, even a singly linked one, while on a vector the top would rather be the last item, since an insertion at the head moves all the items.
Two stacks in one vector of \( c \) places (exercise 4.7) grow from its two ends toward each other, and neither is full while the vector has a free place. MultiStack extends the idea to \( k \) stacks: each item is a cell of a single vector linked to the cell below it in its stack, and the free cells form one more list. A push() takes the first free cell, a pop() gives its cell back, each in \( O( 1 ) \); when no cell is free the vector grows by a constant factor, which costs \( O( 1 ) \) amortized per operation (see Vectors that change their size). The space is that of the \( n \) items of all the stacks, plus the \( k \) tops, against the \( k c \) places of \( k \) vectors each large enough for the largest stack.
An expression in postfix notation (the reverse Polish notation of Łukasiewicz) writes each operator after its two operands, e.g., 5 9 8 + 4 6 * * 7 + * for \( 5 \cdot ( ( 9 + 8 ) \cdot ( 4 \cdot 6 ) + 7 ) \); it needs no parentheses and no precedences, since the order of the tokens is that of the evaluation. Formally, an operand is a postfix expression, and so is \( E_1 \, E_2 \, \mathrm{op} \) when \( E_1 \) and \( E_2 \) are.
Lemma 1 (the evaluation of a postfix expression). Let a scan of the tokens push each operand, and pop, for each operator, the right operand \( t \) and then the left one \( s \), and push \( s \mathbin{\mathrm{op}} t \). Then the scan of a postfix expression \( E \) that starts with a stack \( S \) ends with \( S \) and the value of \( E \) on top of it; hence the value of a whole expression is the one item left, after \( n \) steps of \( O( 1 ) \).
Indeed, by induction on the structure of \( E \): an operand is pushed; for \( E = E_1 \, E_2 \, \mathrm{op} \), the scan of \( E_1 \) leaves its value \( s \) on \( S \), that of \( E_2 \) leaves its value \( t \) on \( S \) and \( s \), and the operator replaces the two with \( s \mathbin{\mathrm{op}} t \). A token sequence that is not well formed shows as an operator that finds fewer than two items, or as more than one item left at the end, which are the preconditions of evalPostfix().
A fully parenthesized infix expression has a pair of parentheses around each operation, e.g., ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ); its postfix form is obtained by moving each operator to the closed parenthesis of its pair, i.e., by writing the operands as they come, pushing the operators and popping one at each closed parenthesis, since the innermost pair open is the last one opened. With the usual precedences instead (\( \cdot \) and \( / \) before \( + \) and \( - \)) and the associativity to the left, the shunting-yard algorithm of Dijkstra (toPostfix()) keeps on the stack the operators whose right operand is not complete yet: an operator \( \mathrm{op} \) first pops to the output, down to an open parenthesis, every operator of precedence not lower than its own, whose right operand ends where \( \mathrm{op} \) begins; a closed parenthesis pops them down to the open one. The operators on the stack have therefore precedences that increase strictly from the bottom up between two parentheses, each token is pushed and popped at most once, and the translation costs \( O( n ) \) (Samelson and Bauer, Dijkstra).
The functions of StackAlgorithms.h act on a stack through push(), pop(), top() and empty() alone, so that they run on every realization. A string is a palindrome if and only if the characters pushed in order and popped come out as the string read from its start, since the stack reverses the order; hence \( O( n ) \) time. Removing the items larger than a key \( k \) while keeping the order of the others takes an auxiliary stack, which reverses them once when they are moved there and once more when they come back, i.e., \( O( n ) \) time and space.
The recursion supplies a second stack, that of the calls. To visit the items from the bottom to the top and leave the stack as it was, forEachBottomUp() pops the top, visits the rest by a recursive call, visits the top and pushes it back: the call stack holds the \( n \) items popped, in \( O( n ) \) time and space. In the same way sortStack() sorts a stack with no other structure: a pass pops two items, pushes back the larger, makes a recursive pass on the rest and pushes the smaller on top.
Lemma 2 (the sort of a stack by passes). A pass on the items \( x_1 , \ldots , x_k \) from the top leaves \( \min( c_j , x_{j+1} ) \) at the place \( j \) from the top and \( c_k \) at the bottom, where \( c_j = \max\{ x_1 , \ldots , x_j \} \); hence it carries a largest item to the bottom, after the pass \( i \) the \( i \) largest items are at the bottom in order, and \( n - 1 \) passes sort the stack, the least item on top. The \( n - 1 \) passes of sortStack() take \( \Theta( n^2 ) \) operations on every input.
Indeed, by induction on the levels of the recursion, the level \( j \) finds \( c_j \) on top, pushes back \( \max( c_j , x_{j+1} ) = c_{j+1} \) for the level below and pushes \( \min( c_j , x_{j+1} ) \) when that level returns. If the bottom items \( x_{p+1} \le \cdots \le x_k \) are the largest, then \( c_j = x_j \) for \( j > p \): the pass leaves them in place and puts the largest of the others just above them. Each pass goes down to the bottom with \( O( 1 ) \) operations per level, whatever the order, and the small items rise by at most one place per pass, as in the bubble sort, of which this is the version on a stack (see Searching, merging and sorting a sequence).
The exercises of chapter 1 of Morin's Open Data Structures on the STACK are three. A sequence \( w \) of \( +1 \) and \( -1 \) is a Dyck word when no prefix of it has a negative sum; it is so if and only if the pushes for \( +1 \) and the pops for \( -1 \), run on an empty stack, never pop the empty stack (exercise 1.2), since after a prefix the stack holds as many items as the sum of the prefix, and a pop of the empty stack is the first prefix of sum \( -1 \). A word whose sum is 0 is moreover one after which the stack is empty: the Dyck words of sum 0 are the sequences of operations that leave a stack as it was. A string of brackets is matched (exercise 1.3) when it is a Dyck word of sum 0 in which each pop removes the bracket that opens the one read: isMatched() pushes the open brackets and checks each closed one against the top, in \( O( n ) \) time. A queue reverses a stack (exercise 1.4), reverseWithQueue(): the items popped come out of the queue in the order in which they went in, the old top first, and pushed in that order they end with the old top at the bottom.
Lemma 3 (the minimum of a stack, exercise 3.14). Let MinStack push an item \( x \) also on a second stack \( M \) when \( M \) is empty or \( x \le \mathrm{top}( M ) \), and pop \( M \) when the item popped is not larger than \( \mathrm{top}( M ) \). Then \( \mathrm{top}( M ) \) is the least item of the stack, and \( M \) holds the items that were not larger than all those below them when they were pushed.
Indeed, by induction on the operations, \( M \) does not increase from its bottom and its top is the least item: a push of \( x \) not larger than the least goes on top of \( M \), and one larger leaves both the minimum and \( M \) as they were; a pop removes the top of \( M \) exactly when the item popped equals it, which happens only if that item is the one that put it there or an equal one pushed on top of it, also in \( M \) (an item equal to the minimum goes to \( M \) too, for this reason). The minima are one per record low from the bottom: \( n \) on a decreasing input, \( O( \log n ) \) expected on a random one (the records of a random permutation), where the pairs of an item and the minimum below it would hold \( 2 n \) items. The queue and the double ended queue with the minimum (exercises 3.15 and 3.16) are two such stacks back to back (see The queues of Morin's book).