Practice Problem Themes Recursion Recurrence Definitions Recursive Relations
Practice Problem • Themes – – Recursion Recurrence Definitions Recursive Relations Induction (prove properties of recursive programs and objects defined recursively)
Induction Examples Prove: • 1 + 3 + 5 + … + (2 n-1) = n 2 • 4 n < 2 n , n ≥ 5 • 2 n < n! , n ≥ 4
Tiling Dominoes • Given a set of 1 × 2, how many different ways are there to tile and 2 × n rectangle placing a single domino vertically (2 × 1) or stacking two dominoes horizontally? • Example. For n = 3 there are 3 ways:
Think Recursively • What are the base cases? • How do you solve the 2 × n using solutions to smaller problems?
ACL Program to Count Dominoes • Derive a recurrence relation for D(n) the number of tilings of a 2 × n rectangle. • Write an ACL program, based on your recurrence, to compute D(n). • Produce a table of values of D(n) and compare to 2 n and (3/2)n • Formulate a conjecture and use induction to prove your result.
ACL Program • Derive a recurrence relation for D(n) the number of tilings of a 2 × n rectangle. • Write an ACL program, based on your recurrence, to compute D(n). • Produce a table of values of D(n) and compare to 2 n and (3/2)n • Formulate a conjecture and use induction to prove your result.
ACL Program to Generate Dominoes • Write a recursive ACL program (use programming mode) to generate all possible tilings of a 2 × n rectangle. • (defun Gen. Dominoes (n) … ) • Represent tilings with “V” for one vertical tile and “HH” for two stacked horizontal tiles. • (concatenate ‘string “HH” “V”) “HHV” • (length “HHV”) 3 • Return a list of strings, one for each possible tiling • Use auxiliary function mapconcat on next slide
Auxiliary Function • Write a recursive function which concatenates a string s in front of all of the strings in a list L • (mapconcat s L) • (mapconcat “V” ‘(“HH” “VV”)) (“VHH” “VVV”)
- Slides: 8