50 Slides--Recursion, Combinatorics

# 50 Slides--Recursion, Combinatorics - CS103A HO#50...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS103A HO #50 Recursion, Combinatorics 3/3/08 1 function R(non-negative integer n) { if (n < 3) return 2n + 1 else return R(n-1) + power(R(n-2), 2) + power(R(n-3), 3) } function I(non-negative integer n) { if (n < 3) return 2n + 1 x := 1 y := 3 z := 5 for (i := 1 to n-2) { w := z + y·y + x·x·x x := y y := z z := w } return z } R(7) R(6) R(5) R(4) R(5) R(4) R(3) R(4) R(3) R(2) R(3) R(2) R(1) R(4) R(3) R(2) R(3) R(2) R(1) R(3) R(2) R(1) R(3) R(2) R(1) R(2) R(1) R(0) R(2) R(1) R(0) R(2) R(1) R(0) R(2) R(1) R(0) Binary Search of a Sorted Array low high x ? int binsearch(int x, int v, int low, int high) recursive binary search: find x in v[low]..v[high]; return index of location; assume low <= high, v sorted { int mid; mid := (low + high) / 2; if (x = v[mid]) return(mid); else if ((x < v[mid]) AND (low < mid)) return(binsearch(x, v, low, mid-1)); else if ((x > v[mid]) AND (high > mid)) return(binsearch(x, v, mid+1, high)); else return(-1); } BASE CASE: If array size is 1, then low = high = mid, and we find the value x or return -1. int binsearch(int x, int v, int low, int high) recursive binary search: find x in v[low]..v[high]; return index of location; assume low <= high, v sorted { int mid; mid := (low + high) / 2; if (x = v[mid]) return(mid); else if ((x < v[mid]) AND (low < mid)) return(binsearch(x, v, low, mid-1)); else if ((x > v[mid]) AND (high > mid)) return(binsearch(x, v, mid+1, high)); else return(-1); } INDUCTIVE STEP: Assume the algorithm is correct for size 1, 2, ..., n. Show that it is correct for size n+1. If x is at mid, the algorithm correctly returns mid. If we are certain x is not in the array, the algorithm correctly returns -1. Otherwise a recursive call is made on a smaller array, and this case is covered by the inductive hypothesis. How Does Recursion Work Inside a Computer? We’ll start by examining how non-recursive function calls work. CS103A HO #50 Recursion, Combinatorics 3/3/08 2 1 2 3 4 5 6 1 2 R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } Non-Recursive Call R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } 1 2 3 4 5 6 1 2 a 3 b 4 ret OS R(3, 4) R,1 PC Stack Stack frame Non-Recursive Call R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } 1 2 3 4 5 6 1 2 a 3 b 4 ret OS R(3, 4) R,1 PC Stack Non-Recursive Call SP R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } 1 2 3 4 5 6 1 2 a 3 b 4 ret OS R(3, 4) R,3 PC Stack Non-Recursive Call SP R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } 1 2 3 4 5 6 1 2 a 3 b 4 ret OS R(3, 4) F,1 PC Stack x 7 ret R,4 Non-Recursive Call SP R(a,b) { if (a = 0) return 0 ... F(a+b) ... ... } F(x) { ... } 1 2 3 4 5 6 1 2 a 3 b 4 ret OS R(3, 4) R,4 PC Stack Non-Recursive Call SP CS103A HO #50 Recursion, Combinatorics 3/3/08 3 1 2 3 4 5 6 1 2 OS PC Stack R(a,b) { if (a = 0) return 0 ......
View Full Document

## This note was uploaded on 10/01/2011 for the course CS 103A taught by Professor Plummer,r during the Winter '07 term at Stanford.

### Page1 / 10

50 Slides--Recursion, Combinatorics - CS103A HO#50...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online