03b: Recursion

ICS211, Spring 2013
Dr. Zach

(switch view)

Status

Java

JVM: Runtime Stack and Heap

JVM: Three parts of memory

(JVM = Java Virtual Machine)
*currently called "permanent generation" storage in JVM

Runtime Stack

JVM Conclusion

Recursion

Concept: Recursion

Algorithm: Handing out papers (1/2)

An iterative (loop-based) approach (called on a Teacher object):

handOutPapers(pile):
  student = first student in line
  while pile is not empty:
    paper = pile.removeTop() 
    give paper to student
    student = next student in line    

Algorithm: Handing out papers (2/2)

A recursive approach (called on a Student object):

handOutPapers(pile):
  if pile is not empty:
    paper = pile.removeTop()
    keep paper
    next = next student in line
    next.handOutPapers(pile)

Alternatives

More descriptive of real life:

handOutPapers(pile):
  paper = pile.removeTop()
  keep paper
  if pile is not empty:
    next = next student in line
    next.handOutPapers(pile)

But in code: what if passed an empty pile?

handOutPapers(pile):
  if pile is not empty:
    paper = pile.removeTop()
    keep paper
    if pile is still not empty:
      next = next student in line
      next.handOutPapers(pile)

Original version was simpler. Often simplest code if you continue to 0 or empty case.

Practical example: Quiz 02, last question

public class CodeD {
  public static void main(String[] args) {
    lotsAndLots(0);
  }

  public static void lotsAndLots(int num) {
    System.out.println(num);
    lotsAndLots(num + 1);
  }
}

Example: Print digits n to 1

public static void printDigits(int n) {
  if (n == 0) {
    return;  //base case: do nothing
  }else {
    System.out.println(n);
    printDigits(n - 1);  //recursive call on smaller problem
  }
}
A visualization of this in Python

Example: Print digits 1 to n

public static void printDigits(int n) {
  if (n <= 0) {
    return;  //base case: do nothing
  }else {
    printDigits(n - 1);    //recursive call on smaller problem
    System.out.println(n); //print after returning from recursion
  }
}

Example: Print digits 1 to n (alternatives)

Example: Digits n to 1 as String

What if we want to generate a String of the numbers?

public static void main(String[] args) {
  String str = allDigits(4);
  System.out.println(str);
}

public static String allDigits(int n) {
  if (n <= 0) {
    return "";
  }else {
    return n + " " + allDigits(n - 1);
  }
}

Recursion Lessons

Classic Example: Factorial

public static int factorial(int n) {
  if (n <= 0) {
    return 1;
  }else {
    return n * factorial(n - 1);
  }
}

Classic Example: Fibonacci

/** Computes nth Fibonacci number: 1,1,2,3,5,8,13,... */
public static int fibonacci(int n) {
  if (n <= 0) {
    return 0;  //error: n should not be less than 1
  }else if (n <= 2) {
    return 1;
  }else {
    return fibonacci(n - 1) + fibonacci(n - 2);
  }
}

Classic Example: Fibonacci, more efficiently

public static int fibonacci(int n) {
  return fib(0, 1, n);
}

private static int fib(int prev, int curr, int n) {
  if (n <= 1) {
    return curr;
  }else {
    return fib(curr, prev + curr, n - 1);
  }
}

Summary

For next time...