CSCI A202 - Introduction to Programming (II)

Lecture 16: Recursion, Sorting and Searching

I will start by reviewing the essence of recursion.

I will solve recursively a small instance of the tower of Hanoi problem.

I will state the principles that should help you with the homework.

We started by looking at this function

int sum(int n) {
  if (n == 1) return 1; 
  else return n + sum(n - 1); 
}
which computes the sum of the first n numbers.

The function assumes a program-friendly user.

Everybody was comfortable with the way it worked.

We made a small transformation to the function:

int sum(int n) {
  int result; 
  if (n == 1) { 
    result = 1; 
  } else { 
    result = n + sum(n - 1); 
  } 
  return result; 
}
and made it compute the sum in slow motion, by storing the result in a local variable first.

Now we added print statements and transformed this:

int sum(int n) {
  int result;
  if (n == 1) { 
    result = 1; 
  } else { 
    result = n + sum(n - 1); 
  } 
  return result; 
}
into this (the print statements added in blue)
int sum(int n) {
  int result;
  if (n == 1) { 
    System.out.println("Start with " + n); 
    result = 1; 
  } else { 
    System.out.println("Add " + n + " to it"); 
    result = n + sum(n - 1); 
  } 
  return result; 
}
Jerry Faust pointed out that this function prints:
Add 6 to it
Add 5 to it 
Add 4 to it 
Add 3 to it 
Add 2 to it 
Start with 1
when called with an initial argument of 6.

So we made another simple change to the function. We switched the order of the statements in the two branches of the if statement.

int sum(int n) {
  int result;
  if (n == 1) { 
    result = 1; 
    System.out.println("Start with " + n); 
  } else { 
    result = n + sum(n - 1); 
    System.out.println("Add " + n + " to it"); 
  } 
  return result; 
}
Now it was not that easy to see what the output would be.

So we worked it out together for sum(3).

sum(3) tries to compute result by calling 
|    sum(2) which tries to compute result by calling 
|    |    sum(1) which computes result (it's 1) 
|    |    |      prints 'Start with 1'
|    |    and returns the result to sum(2) which 
|    then computes result with the value returned by sum(1)
|    and prints 'Add 2 to it' 
|    and returns the result to sum(3) which 
then computes the result with value returned by sum(2)
and prints 'Add 3 to it'
and returns the result (to whoever called it, that is, to us). 
So the result is that by switching the statements we obtain the previous output in reverse.

That is, when we call sum(6) we obtain:

Start with 1
Add 2 to it
Add 3 to it
Add 4 to it
Add 5 to it
Add 6 to it
We obtain a sequence of instructions for someone that wants to add the number himself (or herself).

That's what your hanoi method needs to do.

Produce a set of instructions.

We then described (in pseudocode) the solution to the tower of Hanoi and traced by hand the solution to a problem of size 3. I will post the derivation tree (the big diagram) later here, but it basically is the quivalent of this:

Move 3 from NY to SF using DC 
| Move 2 from NY to DC using SF 
| | Move 1 from NY to SF using DC 
| | | Move disk from peg NY to peg SF
| | End 
| | Move disk from peg NY to peg DC
| | Move 1 from SF to DC using NY
| | | Move disk from peg SF to peg DC
| | End
| End 
| Move disk from peg NY to peg SF
| Move 2 from DC to SF using NY
| | Move 1 from DC to NY using SF
| | | Move disk from peg DC to peg NY
| | End 
| | Move disk from peg DC to peg SF
| | Move 1 from NY to SF using DC
| | | Move disk from peg NY to peg SF
| | End 
| End 
End
Recursive calls are in red, and the instructions that the user sees (printed by your program) are in blue. To solve an instance of this problem the user needs to follow the instructions printed by the program.