23
CS240 Fall 2015 Mike Lam, Professor Recursion r ecursion n. [ri-kur-zhuh n] 1. See “recursion”

CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

CS240Fall 2015

Mike Lam, Professor

Recursion

recursion

n. [ri-kur-zhuh n]

1. See “recursion”

Page 2: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recursion

● The expression of a problem solution in a way that dependson solutions to smaller instances of the same problem

● For some problems, a recursive solution is cleaner than thecorresponding iterative solution

● Classics:

– A list is either● 1) an “empty list” or● 2) an item followed by a list

– fact(n) = 1 if n ≤ 1, n * fact(n-1) if n>1

– fib(n) = 1 if n ≤ 1, fib(n-1) + fib(n-2) if n > 1

– Tower of Hanoi / Brahma

Page 3: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recursion

● The language runtime handles the actual semanticsof recursive behavior

● Usually, it tracks recursive calls using a stack

● Every function call pushes a new entry (called an“activation record” or “frame”) to the stack

● A record is popped when a function returns, andexecution returns to the function on the top of thestack

Page 4: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recursion

● “Call stack”

● Details aremachine- andlanguage-dependent

● More info inCS430

Image from Wikipedia article “Call Stack”

Page 5: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recursion

● Single vs. binary vs. multiple recursion

– fact(n) = 1 if n ≤ 1, n * fact(n-1) if n > 1

– fib(n) = 1 if n ≤ 1, fib(n-1) + fib(n-2) if n > 1

● Trace: fact(4) vs. fib(4)

Page 6: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recursion

● Single vs. binary vs. multiple recursion

– fact(n) = 1 if n ≤ 1, n * fact(n-1) if n > 1

– fib(n) = 1 if n ≤ 1, fib(n-1) + fib(n-2) if n > 1

● Trace: fact(4) vs. fib(4)

fact(4)

fact(3)

fact(2)

fact(1)

fib(4)

fib(3) fib(2)

fib(2) fib(1) fib(1) fib(0)

fib(1) fib(0)

Page 7: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

bool find_helper(int arr[], int item, int left, int right)

{

if (left > right) return false;

int mid = (right - left) / 2 + left;

if (arr[mid] > item)

return find_helper(arr, item, left, mid-1);

else if (arr[mid] < item)

return find_helper(arr, item, mid+1, right);

else

return true;

}

bool find(int arr[], int arr_len, int item)

{

return find_helper(arr, item, 0, arr_len - 1);

}

Page 8: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

Page 9: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5) left = 0right = 6

Page 10: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

[1, 4, 5, 7, 9, 11, 15]

left = 0right = 6

mid = 3

Page 11: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

left = 0right = 6

mid = 3

left = 0right = 2

Page 12: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

left = 0right = 6

mid = 3

left = 0right = 2

mid = 1

Page 13: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

left = 0right = 6

mid = 3

left = 0right = 2

mid = 1

left = 2right = 2

Page 14: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● Trace: find([1,4,5,7,9,11,15], 7, 5)

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

[1, 4, 5, 7, 9, 11, 15]

left = 0right = 6

mid = 3

left = 0right = 2

mid = 1

left = 2right = 2

mid = 2

Page 15: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

A note on the word “binary”

● Binary search is not binary recursion!

– Only recurses on one half of the list

– So it is single recursion

● Binary sum is binary recursion

– Recurses on both sides of the list

Page 16: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● What is the running time of a binary search?

Page 17: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● What is the running time of a binary search?

● Need a way to express recursion costs mathematically

● Write a function!

– Express T(n) in terms of itself

Page 18: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Binary Search

● What is the running time of a binary search?

● Need a way to express recursion mathematically

● Write a function!

– Express T(n) in terms of itself

● For binary search: T(n) = 1 + T(n/2)

– To search n items, do one comparison then recurse onthe appropriate half-list

Page 19: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Recurrences

● Recursive formulas are called “recurrences”

● We still want to find a “closed-form” description

– Something like “2n” or “log2 n” or “5n2”

● Later, we will learn how to solve recurrences

● But first, we need to be comfortable tracingrecursive code

Page 20: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Exercise 1

● Given the following code:

int foo(int n)

{

if (n < 2)

return 1;

else

return n * foo(n-1);

}

● Trace the following call:

printf("%d\n", foo(4));

Page 21: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Exercise 2

● Given the following code:

bool bar_helper(char *text, int start, int end)

{

if (end - start <= 0)

return true;

return text[start] == text[end] &&

bar_helper(text, start+1, end-1);

}

bool bar(char *text)

{

return bar_helper(text, 0, strlen(text)-1);

}

● Trace the following call:

printf("%s\n", bar("abbaba") ? "True" : "False");

Page 22: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Exercise 3

● Given the following code:

int baz(int x, int n)

{

if (n == 0) return 1;

int y = baz(x, n/2);

if (n % 2 == 1) return x * y * y;

else return y * y;

}

● Trace the following call:

printf("%d\n", baz(2, 10));

Page 23: CS240 recursion Fall 2015 n kur-zh n] 1. See “recursion” · 2015-10-23 · Recursion The expression of a problem solution in a way that depends on solutions to smaller instances

Exercise 4

● Given the following code:

void hanoi(int n, char src, char dst, char tmp)

{

if (n == 1) {

printf("move from %c to %c\n", src, dst);

} else {

hanoi(n-1, src, tmp, dst);

hanoi( 1, src, dst, tmp);

hanoi(n-1, tmp, dst, src);

}

}

● Trace the following call:

hanoi(3, 'a', 'c', 'b');