Sample Application: Pattern Recognition Problem

J.E.D.I The number of elements in the stack private int numElements = 0; Implementation of size public int size{ return numElements; } Implementation of isEmpty public boolean isEmpty{ return top == null; } Implementation of top public Object top{ if isEmpty throw new StackExceptionStack empty.; return top.info; } Implementation of pop public Object pop{ Node temp; if isEmpty throw new StackExceptionStack underflow.; temp = top; top = top.link; return temp.info; } Implementation of push public void pushObject item{ Node newNode = new Node; newNode.info = item; newNode.link = top; top = newNode; } }

2.6 Sample Application: Pattern Recognition Problem

Given is the set L = { wcw R | w ⊂ { a, b }+ }, where w R is the reverse of w, defines a language which contains an infinite set of palindrome strings. w may not be the empty string. Examples are aca, abacaba, bacab, bcb and aacaa. The following is the algorithm that can be used to solve the problem: 1. Get next character a or b from input string and push onto the stack; repeat until the symbol c is encountered. 2. Get next character a or b from input string, pop stack and compare. If the two symbols match, continue; otherwise, stop – the string is not in L. The following are the additional states in which the input string is said to be not in L: Data Structures 26 J.E.D.I 1. The end of the string is reached but no c is encountered. 2. The end of the string is reached but the stack is not empty. 3. The stack is empty but the end of the string is not yet reached. The following examples illustrate how the algorithm works: Input Action Stack abbabcbabba ------ bottom -- top abbabcbabba Push a a bbabcbabba Push b ab babcbabba Push b abb abcbabba Push a abba bcbabba Push b abbab cbabba Discard c abbab babba Pop, compare b and b -- ok abba abba Pop, compare a and a -- ok abb bba Pop, compare b and b -- ok ab ba Pop, compare b and b -- ok a a Pop, compare a and a -- ok - - Success Input Action Stack abacbab ------ bottom -- top abacbab Push a a bacbab Push b ab acbab Push a aba cbab Discard c aba bab Pop, compare a and b -- no match, not in the string ba In the first example, the string is accepted while in the second it is not. The following is the Java program used to implement the pattern recognizer: public class PatternRecognizer{ ArrayStack S = new ArrayStack100; public static void mainString[] args{ PatternRecognizer pr = new PatternRecognizer; if args.length 1 System.out.println Usage: PatternRecognizer input string; Data Structures 27 J.E.D.I else { boolean inL = pr.recognizeargs[0]; if inL System.out.printlnargs[0] + is in the language.; else System.out.printlnargs[0] + is not in the language.; } } boolean recognizeString input{ int i=0; Current character indicator While c is not encountered, push the character onto the stack while i input.length input.charAti = c{ S.pushinput.substringi, i+1; i++; } The end of the string is reached but no c is encountered if i == input.length return false; Discard c, move to the next character i++; The last character is c if i == input.length return false; while S.isEmpty{ If the input character and the one on top of the stack do not match if input.substringi,i+1.equalsS.pop return false; i++; } The stack is empty but the end of the string is not yet reached if i input.length return false; The end of the string is reached but the stack is not empty else if i == input.length S.isEmpty return false; else return true; } } Application: Infix to Postfix An expression is in infix form if every subexpression to be evaluated is of the form operand-operator-operand. On the other hand, it is in postfix form if every subexpression to be evaluated is of the form operand-operand-operator. We are accustomed to evaluating infix expression but it is more appropriate for computers to evaluate expressions in postfix form. Data Structures 28 J.E.D.I There are some properties that we need to note in this problem: • The degree of an operator is the number of operands it has. • The rank of an operand is 1. the rank of an operator is 1 minus its degree. the rank of an arbitrary sequence of operands and operators is the sum of the ranks of the individual operands and operators. • if z = x | y is a string, then x is the head of z. x is a proper head if y is not the null string. Theorem: A postfix expression is well-formed iff the rank of every proper head is greater than or equal to 1 and the rank of the expression is 1. The following table shows the order of precedence of operators: Operator Priority Property Example 3 right associative abc = abc 2 left associative abc = abc + - 1 left associative a+b+c = a+b+c Examples: Infix Expression Postfix Expression a b + c d a b c d - a b c - d a b c d - a b + c + d e - f a b c d + e + f - a b c + f g + d f – h i a b c f g d + f h – i + In converting from infix to postfix, the following are the rules: 1. The order of the operands in both forms is the same whether or not parentheses are present in the infix expression. 2. If the infix expression contains no parentheses, then the order of the operators in the postfix expression is according to their priority . 3. If the infix expression contains parenthesized subexpressions, rule 2 applies for such subexpression. And the following are the priority numbers: • icpx - priority number when token x is an incoming symbol incoming priority • ispx - priority number when token x is in the stack in-stack priority Token, x icpx ispx Rank Operand - +1 + - 1 2 -1 3 4 -1 6 5 -1 Data Structures 29 J.E.D.I Token, x icpx ispx Rank 7 - Now the algorithm:

1. Get the next token x. 2. If x is an operand, then output x

3. If x is the , then push x onto the stack. 4. If x is the , then output stack elements until an is encountered. Pop stack once more to delete the . If top = 0, the algorithm terminates. 5. If x is an operator, then while icpx ispstacktop, output stack elements; else, if icpx ispstacktop, then push x onto the stack. 6. Go to step 1. As an example, lets do the conversion of a + b c + d - f g h into its postfix form: Incoming Symbol Stack Output Remarks a a Output a + + a Push + + a Push b + ab Output b + ab icp isp c + abc Output c + ++ abc icp+ isp, pop icp+ isp, push + d ++ abcd Output d + abcd+ Pop +, pop - - abcd++ icp- isp+, pop +, push - f - abcd++f Output f - abcd++f icpisp-, push g - abcd++fg Output g - abcd++fg icpisp, push h - abcd++fgh Output h - abcd++fgh- Pop , pop , pop - Data Structures 30 J.E.D.I

2.7 Advanced Topics on Stacks