20.02.2014 Views

Worksheet 11: Recursive Functions and Recurrence ... - Classes

Worksheet 11: Recursive Functions and Recurrence ... - Classes

Worksheet 11: Recursive Functions and Recurrence ... - Classes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Worksheet</strong> <strong>11</strong>: <strong>Recursive</strong> <strong>Functions</strong> <strong>and</strong> <strong>Recurrence</strong> Relations Name:<br />

<strong>Worksheet</strong> <strong>11</strong>: <strong>Recursive</strong> <strong>Functions</strong> <strong>and</strong> <strong>Recurrence</strong><br />

Relations<br />

In Preparation: Read the section on recursive functions <strong>and</strong> recurrence relations in<br />

Chapter 4.<br />

void printBinary (int v) {<br />

if ((v == 0) || (v == 1))<br />

print(n);<br />

else {<br />

printBinary(v/2);<br />

print(v%2);<br />

}<br />

}<br />

number, we have the following equation:<br />

As described in Chapter 4, a very useful technique for<br />

analyzing recursive functions is to form recurrence<br />

relations. For example, suppose we are analyzing the<br />

function shown at left. Let n will represent the number<br />

of binary digits in the printed form. Because the<br />

argument is divided by 2 prior to the recursive call, it<br />

will have one fewer digit. Everything else, outside of the<br />

recursive call, can be performed in constant time. The<br />

base case can also be performed in constant time. If we<br />

let T(n) represent the time necessary to print an n-digit<br />

T(n) = T(n-1) + c<br />

T(1) = c<br />

Read this as asserting that the time for n is equal to the time for n-1 plus some unknown<br />

constant value. This is termed a recurrence relation.<br />

T(n) = T(n-1) + c<br />

O(n)<br />

T(n) = T(n/2) + c O(log n)<br />

T(n) = 2 * T(n/2) + c a n + c b O(n log n)<br />

T(n) = 2 * T(n-1) + c O(2 n )<br />

The four most common recurrence<br />

relations <strong>and</strong> their solutions are shown at<br />

left.<br />

The following are some example recursive functions. Describe the running time for each<br />

as a recurrence relation, <strong>and</strong> then give the big-Oh execution time:<br />

factorial(int n) {<br />

if (n = 0) return 1;<br />

else return n * factorial(n-1)<br />

}<br />

double exp (double a, int n) {<br />

if (n == 0) return 1.0:<br />

else return a * exp (a, n-1);<br />

}<br />

void Hanoi (int n, int a, int b, int c) {<br />

if (n > 0) {<br />

An Active Learning Approach to Data Structures using C 1


<strong>Worksheet</strong> <strong>11</strong>: <strong>Recursive</strong> <strong>Functions</strong> <strong>and</strong> <strong>Recurrence</strong> Relations Name:<br />

Hanoi(n-1, a, c, b);<br />

System.out.println(“Move from “ + a + “ to “ + b + “using “ + c);<br />

Hanoi(n-1, c, b, a);<br />

}<br />

}<br />

For the following, let n represent the size of the region of the array between low <strong>and</strong> high<br />

int binarySearch (double data[ ], int low, int high, double test) {<br />

if (low == high) return low;<br />

int mid = (low + high)/2;<br />

if (test < data[mid]) return binarySearch(data, low, mid, test)<br />

else return binarySearch(data, mid, high, test);<br />

}<br />

An Active Learning Approach to Data Structures using C 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!