Lecture: Recursion and Induction
Background Material
- Induction
- Recursive Programs
- Iterative constructs (for, while)
Reading
- Chapter 2 (sec.
2.1, 2.3, 2.4, 2.6, 2.7, 2.9) of Foundations of CS.
- Also look ahead to Chapter 3 (sec. 3.11)
Topics
- Recursion
- Recurrence Relations
- Induction
- Recursive Definitions
- More Induction
- Tower of Hanoi
- Problem statement and program
- Trace through program execution and review recursive functions
- Use induction to prove that the program is correct
- Recurrence relation for number of moves:
- M(n) = 2*M(n-1) + 1, n>1
- M(1) = 1
- Guess solution for M(n) from table of values and prove by induction
- Unwind recurrence to obtain equivalent sum and reprove result using
induction again
- Geometric series (exc. use induction to prove the formula for geometric
series)
- Use induction to prove that the number of moves used by the program
is optimal
- Gray Codes
- Problem statement and definition
- An n-bit Gray code is a sequence of 2^n binary numbe
rs where
consecutive numbers differ by exactly one bit.
- E.G. [000,001,011,010,110,111,101,100]
- Shank Encoding
- Recursive construction/definition of binary reflected Gray code
- G_1 = [0,1]
- G_n = [0G_{n-1},1G'_{n-1}], where bG_k is the sequen
ce obtained by
prepending the bit b, and G' is G in reverse order
- Iterative definition of the binary-reflected Gray code
- Let G_n(i) be a fuction from the interval [0,...,2^n
-1] defined
by G_n(i) = i ^ (i >>1)
- Return to the Tower of Hanoi
- For the n-disk problem let d_0,...,d_{n-1} be n coordinates
that can
be either 0 or 1. Initialize d_i = 0, and set d_i to the
complement (the complement of 0 is 1 and the complement of 1 is
0) of
d_i whenever the ith disk is moved.
- Retrace the solution to the 3-disk problem and keep track of
(d_0,d_1,d_2). Observe that the sequence, when interprete
d as a
sequence of 3-bit binary numbers is equal to the binary-reflecte
d Gray
code. Why? Can you prove this? How can this be
used to
provide an iterative solution to the Tower of Hanoi problem.
- Hypercube
- Definition - an n-dimensional hypercube is a graph
(a collection of
vertices and edges) with 2^n nodes with the property that
if the nodes are labeled by the binary representation of
the number 0,...,2^n -1,
there is an edge between every two nodes that differ by
exactly one bit in their binary representation.
- Recursive definition
- A one-dimensional hypercube is a single node l
abeled by the
number 0.
- An n-dimensional hypercube H_n is obtained from two
copies of an
(n-1)-dimensional hypercube by connecting identical node
s in each of
copy of H_{n-1} by an edge. The binary representat
ion of the
labels of the nodes in the first copy should be prepende
d with a 0-bit
and the labels of the matching node in the second copy s
hould be
prepended with a 1-bit
- How can you prove that the two definitions are equivalent?
- Derive a recurrence relation for the number of edges in an
n-dimensional hypercube.
- A Hamiltonian path is a sequence of edges that visit each
node exactly once. Can you find a Hamiltonian path for a
3-dimensional hypercube? Such a path provides a Gray
code. Why?
- Can you generalize your construction, using the recursive
definition of a hypercube, to construct a Hamiltonian path
for an n-dimensional hypercube? How does your
construction relate to the Tower of Hanoi problem?
Lecture Notes
Programs
- hanoi.scm (recursive scheme program
to solve the Tower of Hanoi problem)
Resources
- DrRacket - Variant of the programming language Scheme
Assignments