Lecture 1: Recursion and Induction
Background Material
- Recursive Programs
- Iterative constructs (for, while)
Reading
- Chapter 2 (sec.
2.1, 2.3, 2.4, 2.6, 2.7, 2.9) of the text.
- Also look ahead to Chapter 3 (sec. 3.11)
Topics
- Overview of the course.
- Objectives
- Topics
- Grading policy and expectations
- 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 numbers 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 sequence obtained
by prepending the bit
- Use induction to prove that the binary-reflected Gray code is a Gray
code
- 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)
- Use induction to prove that the sequence G_n(i), i= 0,...,2^n-1 is
the binary-reflected Gray code (exercise)
- 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 interpreted as a sequence of 3-bit
binary numbers is equal to the binary-reflected 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 labeled by the
number 0.
- An n-dimensional hypercube H_n is obtained from two copies of an
(n-1)-dimensional hypercube by connecting identical nodes in each of copy
of H_{n-1} by an edge. The binary representation of the labels of the
nodes in the first copy should be prepended with a 0-bit and the labels of
the matching node in the second copy should 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.cpp (recursive C++ program
to solve the Tower of Hanoi problem)
Resources
Assignments
Created: Sept. 26, 2004 by jjohnson@cs.drexel.edu