Parsing Expressions and Induction
- algebraic expressions
- prefix and postfix expressions
- recognizing prefix expressions
- infix expressions
- towers of Hanoi problem and solution
- proofs about programs using mathematical induction:
- correctness of factorial
- cost of towers of Hanoi
Grammars for Algebraic Expressions
Prefix and Postfix Expression
- fully parenthesized infix expression:
(((2 + 7) - ((5 * 13) / 9)) - 19)
- prefix needs neither precedence nor associativity:
- - + 2 7 / * 5 13 9 19
- postfix also needs neither precedence nor associativity:
2 7 + 5 13 * 9 / - 19 -
- converting infix to prefix or postfix:
- start with a fully-parenthesized infix expression
- in place of each left (right) parenthesis, write the
corresponding operator
- in-class exercise: write the prefix and postfix forms of
a * b - c * d + e * f * g / h - 1
Recognizing Prefix Expressions
= |
= + | - | * | /
= a | b | c | ... | z
- if a string (first
to last) has length 1, then it must be a lowercase letter
()
- otherwise, it must start with an operator
- observation: any characters after an expression make it not
an expression
- so if we are trying to read , the first
sub-expression (which starts at first + 1
can only end at one place (no ambiguity)
- (1) find the end of the first expression (2) recursively
check the two subexpressions
Finding the end of a Prefix Expressions
- return position of end of prefix expression, or -1 if it is not
a valid prefix expression
- pseudocode:
endPre (first, last) {
if (first > last) return -1;
ch = character at first;
if (ch is an id) return first
if (ch is not an operator)
return -1;
firstEnd = endPre (first + 1, last);
if (firstSub >= 0) {
return endPre (firstEnd + 1, last);
}
return -1;
}
Notes:
If time allows, box trace this pseudocode on blackboard, see p. 227-228 of
book (but use different parameters on blackboard)
Evaluating a Prefix Expressions
Towers of Hanoi
- three "towers" holding n disks, each disk a different size
- can only put a disk on top of a bigger disk
- all disks start on tower 1, must be moved one at a time so they
are on tower 2
- to move lowest disk from 1 to 2, move all but the lowest to tower
3, then move the lowest to tower 2, then move all but the lowest from 3 to 2:
recursive solution, top disk is base case (can be moved without recursion)
Correctness of Factorial
fact (n)
if (n is 0) return 1;
return n * fact (n - 1);
- easy to prove that fact(0) = 0! = 1
- prove (by induction on n) that
fact(n) = n! = n * (n - 1) * ... * 1
- basis: show that it is true for n=0 (trivial)
- induction: show that if the code is correct for k,
it is correct for k+1
- so we can assume that fact(k) = k!
- but fact(k + 1) simply returns (k + 1) * fact(k),
which by the assumption is (k + 1) * k!, or
(k + 1) * k * ... * 1
Cost of Towers of Hanoi
towers (n, src, dest, spare)
if (count is 1) move 1 from src to dest
else
towers (n - 1, src, spare, dest);
move 1 from src to dest
towers (n - 1, spare, dest, src);
- how many moves does this take?
- with one disk, it takes one move
- if we know it takes m(n-1) moves
for n-1 disks, we can compute the
moves for n disks: m(n) = 2 * m(n - 1) + 1
- I wish to prove that m(n) = 2n - 1
for n >= 1
Cost of Towers of Hanoi -- Math Induction
- I wish to prove that m(n) = 2n - 1
for n >= 1
- basis: if n = 1, then m(n) = 1 = 21 - 1
- inductive case:
- assume that for a given n = k > 1, the formula is correct,
so that m(k) = 2k - 1
- now show that, using this assumption, we can prove
the formula holds for n = k + 1
- m(k+1) = 2 * m(k) + 1 = 2 * (2k - 1) + 1
= 2 * 2k - 2 + 1 = 2 k + 1 - 1
- this proves that m(n) = 2n - 1 for all n >= 1