Structural Induction
“Balanced String of Parentheses”
• Base case:
– The empty string εis balanced
• Constructor rules:
– C1: If x is balanced then so is (x), that is, the
result of writing a “(“, then x, then “)”
– C2: If x and y are balanced then so is xy
A String of Parentheses is Balanced iff
it Satisfies the Counting Rule (SCR)
• Say that a string s∈{ ), ( }* SCR iff starting
from 0 at the left end of the string, adding
1 for each (, subtracting 1 for each ), gives
0 at the end without ever going negative.
• Theorem: A string of parentheses is
balanced iff it SCR.
If a string of parentheses x is
balanced, then x SCR.
• Structural Induction Base case: x=ε. Then
count begins and ends 0 so x SCR.
• Induction case 1: x = (y) where y is balanced.
Then y SCR (why?). Then the count for x is
+1 after first (, +1 again after the last
character of y, stays positive in between, and
ends at 0 after the final ).
• Induction case 2: x = yz where y and z are
balanced. Then y and z SCR and the count
goes from 0, to 0 after y, to 0 after z without
ever going negative.
If x SCR then x is balanced.
• Proof by strong induction on |x|. Suppose x SCR.
• Base case: |x|=0. Then x=εand x is balanced.
• Induction step. Fix n, suppose |x| = n+1>0 and for all
m≤n and any y of length m, if y SCR then y is
• Case 1. The count never reaches 0 except at the
end. Then x=(y) where y SCR (why?). But |y|=|x|-2
and by IH y is balanced. By construction rule 1 so is
• Case 2. x=yz where the y and z are nonempty and
the count goes to 0 after y. Then y and z are shorter
than x and each SCR, so each is balanced by IH.
Then x is balanced by construction rule 2.

similar documents