Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

56
Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing

Transcript of Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Page 1: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Programming Appreciation Camp

Session 2: Recursion

Steven Halim

NUS School of Computing

Page 2: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

2[Programming Appreciation Camp November 2008]

Recap In the first session, we have learnt/done

some problem solving using algorithm: Algorithms have three control structures:

Sequence Selection (branching) Repetition (loop)

And Recursion

Page 3: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion: Informal Definition A “Definition” in an English-English dictionary

Recursion If you still don't get it, See: “Recursion”.

Informally: Repeat same thing until some conditions are met. Seems like repetition/iteration/loop

But more than that!

3[Programming Appreciation Camp November 2008]

Page 4: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Non Computing Examples Droste Effect

http://en.wikipedia.org/wiki/Droste_effect

Mathematical Definition Example: even number

2 is an even number If n is an even number, then n+2 too

Russian Dollhttp://en.wikipedia.org/wiki/Matryoshka_doll

Recursive Acronymshttp://en.wikipedia.org/wiki/Recursive_acronym

GNU: GNU’s Not Unix VISA: VISA International Service Association

[Programming Appreciation Camp November 2008]

Page 5: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Sierpinski Triangle Koch Snowflake

Recursive Tree More Fractals

http://en.wikipedia.org/wiki/Fractal

Non Computing Examples: Fractal

[Programming Appreciation Camp November 2008]

Page 6: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion in Comp Sci: Motivation (1) Given this 9x9 2-D map

L is Land W is Water

Q: “How many lakes in the map?” Adjacent ‘W’s (N, E, S, W) belong to one lake.

6[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLL5L3LLLLLLLLLLLLL

The answer for this scenario: 5 lakes!

Page 7: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion in Comp Sci: Motivation (2) Hard to get answer using standard iteration!

numOfW 0for each cell(i,j) in the map // (0,0) to (9,9) if (cell(i,j) is character ‘W’) numOfW numOfW + 1printf numOfW // W = 18 in this case

7[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLL5L3LLLLLLLLLLLLL

LLLLLLLLLLL12LL3LLL45LLLLLLL678L9ALLLLLBCDLLLLLLLLLLLLLLLEFLLGLLLHLILLLLLLLLLLLLL

A=10, B=11, …, I = 18

So, how to answer: 5 lakes?

Use recursion!

This problem will be revisitedat the latter part of this session!

Page 8: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion: Basic Ideahttp://en.wikipedia.org/wiki/Recursion_(computer_science)

Recursion is a programming paradigm! Complements sequence, selection, repetition

Divide: break up a probleminto sub-problems of the same type!

Conquer: Solve the problem with a functionthat calls itself to solve each sub-problem Somewhat similar to repetition, but not 100% Terminating condition: one or more of these sub-

problems are so simple that they can be solved directly without further calls to the function

8[Programming Appreciation Camp November 2008]

Page 9: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Why use Recursion? Many computer algorithms can be expressed

naturally in recursive form Trying to express these algorithms iteratively

can be actually confusing… e.g. “finding lakes” example

9[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

Page 10: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion: Implemented As Function Function is simply defined as follow:

output function_name(inputs) process the inputs return output

10[Programming Appreciation Camp November 2008]

Page 11: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion: Basic Form Template for most recursive function

Recursive_Function_Name(Parameter_List)

// you will always see “selection statement” (if, switch, etc)

if (base_case) // problem is trivial, there can be more than one

do_something_simple // produce the result directly

else // recursive case

// Simplify the problem and then call this same function again

Recursive_Function_Name(Modified_Parameter_List)

Modified_Parameter_List must bring the recursive function closer to (one of) the base case!

[Programming Appreciation Camp November 2008]

Page 12: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

LEARNING BY EXAMPLE7 recursion examples to get you started

12[Programming Appreciation Camp November 2008]

Page 13: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 1: Countdown (1) http://www.nasa.gov/returntoflight/launch/countdown101.html

Problem Description: We want to count down the space shuttle launch,

e.g. 10, 9, 8, …, 3, 2, 1, BLAST OFF!!! Must use recursion!

Although this can be done iteratively.

13[Programming Appreciation Camp November 2008]

Page 14: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 1: Countdown (2) http://www.nasa.gov/returntoflight/launch/countdown101.html

Recursive version:count_down(n) if (n <= 0) print “BLAST OFF!!!!” else print “Time = ” + n count_down(n-1)

14[Programming Appreciation Camp November 2008]

Base Case occurs when n <= 0

If this happens, simply print “BLAST OFF!!!!”

Recursive Case occurs when n > 0

If this happens, print the current time, and then call a simpler problem: count_down(n-1)

Note that this recursive function returns nothing

Page 15: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 1: Countdown (3) http://www.nasa.gov/returntoflight/launch/countdown101.html

Recursive version:count_down(n) if (n <= 0) print “BLAST OFF!!!!” else print “Time = ” + n count_down(n-1)

Sample calls:count_down(3) Time = 3Time = 2Time = 1BLAST OFF!!!!

Iterative version:count_down_itr(n) for (t=n to 0, decrement by 1)

print “Time = ” + t print BLAST OFF!!!!”// actually the iterative// version is simpler// for this case

Sample calls:count_down_itr(3) Time = 3Time = 2Time = 1BLAST OFF!!!!

15[Programming Appreciation Camp November 2008]

Page 16: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 2: Factorial (1)http://en.wikipedia.org/wiki/Factorial

Problem Description: fact(n), n!, product of 1 to n, is defined as:

fact(n) = n * (n-1) * (n-2) * ... * 2 * 1, and fact(0) = 1 fact(<0) is not defined This is an iterative paradigm!

Using recursion, it can be defined as fact(n) = 1 if (n = 0) // simple sub-problem n * fact (n-1) if (n > 0) // calls itself

16[Programming Appreciation Camp November 2008]

n n!

0 1

1 1

2 2

3 6

4 24

5 120

Page 17: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 2: Factorial (2)http://en.wikipedia.org/wiki/Factorial

Recursive version:fact(n) if (n = 0) return 1 else return n * fact(n-1)

17[Programming Appreciation Camp November 2008]

Base Case occurs when n = 0

If this happens, simply return 1, 0! = 1

Recursive Case occurs when n > 0

If this happens, return n * result of simpler problem of fact(n-1)

Note that this recursive function returns a number, which is the result of fact(n)

Recursive definition:

fact(n) = 1 if (n = 0) n * fact (n-1) if (n > 0)

Page 18: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 2: Factorial (3)http://en.wikipedia.org/wiki/Factorial

Recursive version:fact(n) if (n = 0) return 1 else return n * fact(n-1)

Sample calls:fact(0) 1 (base case)fact(1) 1*fact(0) = 1*1 = 1fact(2) 2*fact(1) = 2*1 = 2fact(3) 3*fact(2) = 3*2 = 6fact(4) 4*fact(3) = 4*6 = 24fact(5) 5*fact(4) = 5*24 = 120…see visual explanation

18[Programming Appreciation Camp November 2008]

Page 19: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 2: Factorial (3)http://en.wikipedia.org/wiki/Factorial

Recursive version:fact(n) if (n = 0) return 1 else return n * fact(n-1)

Sample calls:fact(0) 1 (base case)fact(1) 1*fact(0) = 1*1 = 1fact(2) 2*fact(1) = 2*1 = 2fact(3) 3*fact(2) = 3*2 = 6fact(4) 4*fact(3) = 4*6 = 24fact(5) 5*fact(4) = 5*24 = 120…

Iterative version:fact_itr(n) { result 1 for (i=1 to n, increment by 1) result result * i return result

Sample calls:fact_itr(0) 1fact_itr (1) 1fact_itr (2) 1*2 = 2fact_itr (3) 1*2*3 = 6fact_itr (4) 1*2*3*4 = 24fact_itr (5) 1*2*3*4*5 = 120…

19[Programming Appreciation Camp November 2008]

Page 20: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 3: Fibonacci (1)http://en.wikipedia.org/wiki/Fibonacci_number

Problem Description: Fibonacci numbers are defined recursively:

Fn = 0 if n = 0 1 if n = 1 Fn-1 + Fn-2 if n > 1

20[Programming Appreciation Camp November 2008]

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Page 21: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 3: Fibonacci (2)http://en.wikipedia.org/wiki/Fibonacci_number

Recursive version:fib(n) if (n <= 1) // for 0 and 1 return n else return fib(n-1) + fib(n-2)

21[Programming Appreciation Camp November 2008]

Base Case occurs when n <= 1 (0 or 1 only)

If this happens, simply return n (the value itself)

Recursive Case occurs when n > 1

If this happens, return fib(n-1) + fib(n-2) we call the same function twice!

Here, we observe that a recursive function can call itself more than once! (branching recursion)

Recursive definition:

Fn = 0 if n = 01 if n = 1Fn-1 + Fn-2 if n > 1

Page 22: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 3: Fibonacci (3)http://en.wikipedia.org/wiki/Fibonacci_number

Recursive version:fib(n) if (n <= 1) // for 0 and 1 return n else return fib(n-1) + fib(n-2)

Sample calls:fib(0) 0 (base case)fib(1) 1 (base case) fib(2) fib(0)+fib(1) = 0+1 = 1fib(3) fib(1)+fib(2) = 1+1 = 2fib(4) fib(2)+fib(3) = 1+2 = 3fib(5) fib(3)+fib(4) = 2+3 = 5…

Iterative version:fib[0] 0fib[1] 1for (i=2 to n, increment by 1) fib[i] fib[i-1] + fib[i-2]

Sample calls:fib[0] = 0fib[1] = 1fib[2] = 1fib[3] = 2fib[4] = 3fib[5] = 5…

22[Programming Appreciation Camp November 2008]

Page 23: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 3: Fibonacci (4)http://en.wikipedia.org/wiki/Fibonacci_number

Recursive version:

Sample calls:On my machine

fib(20) ~ 0.0 second fib(30) ~ 0.0 secondfib(35) ~ 1 secondfib(36) ~ 2 secondsfib(37) ~ 4 secondsfib(38) ~ 6 secondsfib(39) ~ 8 seconds fib(40) ~ 11 seconds

Iterative version:

Sample calls:On my machine

fib(20) ~ 0.0 second fib(30) ~ 0.0 secondfib(35) ~ 0.0 second fib(36) ~ 0.0 second fib(37) ~ 0.0 second fib(38) ~ 0.0 second fib(39) ~ 0.0 second fib(40) ~ 0.0 second

23[Programming Appreciation Camp November 2008]

Page 24: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 3: Fibonacci (5)http://en.wikipedia.org/wiki/Fibonacci_number

The Recursive Pattern of Fibonacci(5) Many repetitions… Slow! Fib 5

Fib 3 Fib 4

Fib 1 Fib2

1 Fib 1 Fib 0

1 0

Fib 2 Fib 3

Fib 0 Fib1 Fib 1 Fib 2

0 1 1 Fib 0 Fib1

0 1

Page 25: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 4: Exponentiation, 2n (1)http://en.wikipedia.org/wiki/Exponentiation

Problem Description: Exponentiation, written as an,

where a is called as “base”and n is called “the exponent”.

When n >= 0, exponentiation is defined as: an = a * a * …. * a (n times) This is iterative definition

What is the recursive formulation? an = _____________

_____________

_____________

25[Programming Appreciation Camp November 2008]

Page 26: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 4: Exponentiation, 2n (2)http://en.wikipedia.org/wiki/Exponentiation

Recursive version:pow2(n) if (n = 0) return 1 else if (n = 1) return 2 else return 2 * pow2(n-1)

26[Programming Appreciation Camp November 2008]

Base Case occurs when n = 0 or n = 1 there can be >= 1 base case(s)

If this happens, return 1 for 20 or 2 for 21

Recursive Case occurs when n > 1

If this happens, return 2 * pow2(n-1) pow2(n-1) is a simplified problem

Here, we observe that a recursive function can have more than one base cases (processed differently)

Page 27: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 4: Exponentiation, 2n (3)http://en.wikipedia.org/wiki/Exponentiation

Recursive version:pow2(n) if (n = 0) return 1 else if (n = 1) return 2 else return 2 * pow2(n-1)

Sample calls:pow2(1) 2 (base case)pow2(2) 2*pow2(1) = 2*2 = 4pow2(3) 2*pow2(2) = 2*4 = 8 pow2(4) 2*pow2(3) = 2*8 = 16 …

Iterative version:pow2_itr(n) result 1 for (i=1 to n, increment by 1) result result * 2 return result

Sample calls:pow2_itr(1) 1*2=2pow2_itr (2) 1*2*2=4pow2_itr (3) 1*2*2*2=8pow2_itr (4) 1*2*2*2*2=16

27[Programming Appreciation Camp November 2008]

Page 28: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 5: Print Digits (1)http://en.wikipedia.org/wiki/Decimal

Problem Description: Given a decimal number (base 10),

as what we used in daily life… Print its digit line by line. e.g. 2008 is printed as:

Answer (red lines):2008

28[Programming Appreciation Camp November 2008]

Page 29: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 5: Print Digits (2)http://en.wikipedia.org/wiki/Decimal

Recursive version:digit(n)

if (n > 0) digit(n / 10) print n % 10

29[Programming Appreciation Camp November 2008]

Base Case occurs when n <= 0

If this happens, do nothing!

Recursive Case occurs when n > 0

If this happens, call print_digit with simplified problem: one less ‘digit’ (n / 10) then, print n % 10. % is called the modulus (remainder) operator.

Here, we observe that base case can be as simple as doing nothing and we can still do something after recursive calls!

Page 30: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 5: Print Digits (3)http://en.wikipedia.org/wiki/Decimal

Recursive version:digit(n)

if (n > 0) digit(n / 10) print n % 10

Sample calls:digit(2008)

2

0

0

8

Details in the next slide!

Iterative version *:digit_itr(n) while (n > 0) print n % 10 n n / 10

Sample calls:digit_itr(2008)

8

0

0

2

* This is not what we want…

* We need to reverse the answer!

30[Programming Appreciation Camp November 2008]

Page 31: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 5: Print Digits (4)http://en.wikipedia.org/wiki/Decimal

Recursive version:digit(n)

if (n > 0) digit(n / 10) print n % 10

Sample calls:digit(2008)

2

0

0

8

See diagram for clarity

digit(2008)

digit(200)

digit(20)

digit(2)

digit(0)

Do nothing!

print 2 %10 = 2

print 20 %10 = 0

print 200 % 10 = 0

print 2008 % 10 = 8

31[Programming Appreciation Camp November 2008]

Page 32: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

32[Programming Appreciation Camp November 2008]

Recall - Task 2: Pascal’s Trianglehttp://en.wikipedia.org/wiki/Pascal%27s_triangle1

1 11 2 1

1 3 3 11 4 6 4 1

1 5 10 10 5 1

Compute nCk or C(n,k)

nCk = n! / (k! * (n – k)!)

Page 33: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 6: Ways to choose(n, k) (1)http://en.wikipedia.org/wiki/Combinations

How many ways to choose k out of n items? Enumerate the Base Cases

When k < 0, e.g. (k=-1) out of (n=3)? 0, this problem is undefined for k < 0…

When k > n, e.g. (k=4) out of (n=3)? 0, also undefined for k > n…

When k == n, e.g. (k=3) out of (n=3)? 1, take all

When k == 0, e.g. (k=0) out of (n=3)? 1, do not take anything

[Programming Appreciation Camp November 2008]

Page 34: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 6: Ways to choose(n, k) (2)http://en.wikipedia.org/wiki/Combinations

How many ways to choose k out of n items? Think about the General (recursive) Cases

When 0 < k < n, e.g. (k=2) out of (n=3)? Consider the item one by one, e.g. item X!

choose(n,k) = choose(n-1,k-1)

The number of ways if I take X The problem becomes simpler:

choosing k-1 (k=1) out of n-1 (n=2) plus choose(n-1,k)

The number of ways if I do not take X The problem becomes simpler:

choosing k (k=2) out of n-1 (n=2)

[Programming Appreciation Camp November 2008]

Page 35: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 6: Ways to choose(n, k) (3)http://en.wikipedia.org/wiki/Combinations

How many ways to choose k out of n items?

[Programming Appreciation Camp November 2008]

Recursive version only:choose(n, k) if (k = 0 or k = n) return 1 else return choose(n-1,k-1) + choose(n-1,k)

Page 36: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 6: Ways to choose(k, n) (4)http://en.wikipedia.org/wiki/Combinations

How many ways to choose k out of n items?

[Programming Appreciation Camp November 2008]

This problem is not intuitive to be

solved iteratively!

However, the recursive solution has many similar

sub-problems (compare with

Fibonacci)

Special:Closed form:

nCk = n!/(k!*(n-k)!)6 in this case!

Page 37: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 7: Binary Search (1)http://en.wikipedia.org/wiki/Binary_search

Searching in Sorted Array, e.g. array A =

Standard: Scan item one by one from left to right

Can we do better? Yes, using a technique called binary search Idea: Narrow the search space by half (left/right

side) successively until we find the item or until we are sure that the item is not in the sorted array.

37[Programming Appreciation Camp November 2008]

Index 0 1 2 3 4 5 6 7 8 9

Sorted Value 3 7 20 24 47 58 70 73 81 99

Page 38: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 7: Binary Search (2)http://en.wikipedia.org/wiki/Binary_search

Recursive version (iterative not shown):bsearch(arr, key, low, high) {

if (high < low)return -1 // not found, base case 1

mid (low + high) / 2

if (arr[mid] > key)return bsearch(arr, key, low, mid-1) //

recursive case 1: leftelse if (arr[mid] < key)

return bsearch(arr, key, mid+1, high) // recursive case 2: right

elsereturn mid // found, base case 2

38[Programming Appreciation Camp November 2008]

Page 39: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Example 7: Binary Search (3)http://en.wikipedia.org/wiki/Binary_search

Assume we have a sorted array A:

Sample calls: bsearch(A, 7, 0, 9) // mid is 4, A[4] = 47, 7 < 47, go to left side

bsearch(A, 7, 0, 3) // mid is 1, A[1] = 7, 7 == 7, found it

return 1; // found in index 1 bsearch(A, 47, 0, 9) // mid is 4, A[4] = 47, 47 == 47, found it

return 4; // found in index 4 bsearch(A, 82, 0, 9) // mid is 4, A[4] = 47, 82 > 47, go to right side

bsearch(A, 82, 5, 9) // mid is 7, A[7] = 73 , 82 > 73, go to right side

bsearch(A, 82, 8, 9) // mid is 8, A[8] = 81, 82 > 81, go to right side

bsearch(A, 82, 9, 9) // mid is 9, A[9] = 99, 82 < 99, go to left side

bsearch(A, 82, 9, 8) // base case, not found

return -1; // not found

39

Index 0 1 2 3 4 5 6 7 8 9

Sorted Value 3 7 20 24 47 58 70 73 81 99

[Programming Appreciation Camp November 2008]

Page 40: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Recursion: Recap A function that call itself

With simpler sub-problem Can have one or more recursive cases

Has simple/trivial sub-problems: base cases This is the terminating condition Can be as simple as “doing nothing” Can have one or more base cases

Can solve certain problems naturally Can be slower than iterative counterparts

This can be optimized though…

40[Programming Appreciation Camp November 2008]

Page 41: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Lake Question (Revisited) (1) Given this 9x9 2-D map

L is Land W is Water

Q: “How many lakes in the map?” Adjacent ‘W’s (N, E, S, W) belong to one lake.

41[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLL5L3LLLLLLLLLLLLL

The answer for this scenario: 5 lakes!

Page 42: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Lake Question (Revisited) (2) How to connect adjacent (N, E, S, W) ‘W’s? The idea: Analogy of real-life “water”!

If we pour water to coordinate (1,2), The water will fill the area indicated with ‘1’s!

But how to do this? (flooding the lakes?)

42[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LLWLLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

Page 43: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Lake Question (Revisited) (3)http://en.wikipedia.org/wiki/Flood_fill

Flood Fill Recursive Algorithmfloodfill(r, c, ID) if (r < 0 or r > 9 or c < 0 or r > 9) return // base case 1: exceeding boundary if (cell(r,c) is not character ‘W’) return // base case 2: not water cell(r,c) ID // simplify the problem W to ID: 1 ‘W’ less // recursively fill the 4 directions floodfill(r , c+1 , ID) // east floodfill(r-1 , c , ID) // north floodfill(r , c-1 , ID) // west floodfill(r+1 , c , ID) // south

Sample calls:floodfill(1,2,‘1’) // fill lake starting from (1,2) with ‘1’

43[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LLWLLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

Page 44: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Lake Question (Revisited) (4) Now, Use floodfill

to flood each lake,then increment the lake counter!

lake 0for each cell(i,j) in the map if (cell(i,j) is character ‘W’) floodfill(i, j, ‘1’+lake) lake lake + 1printf lake // 5 in this case

44[Programming Appreciation Camp November 2008]

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LLWLLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LLWLLLWL3LLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLLWL3LLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLL5L3LLLLLLLLLLLLL

Page 45: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Final Say: Recursion is Powerful It is a programming paradigm:

Divide and Conquer Many data structures are recursively defined

Linked List, Tree or Heap: these data structures and their operations are naturally recursive, etc

Many algorithms are naturally recursive Sorting: Merge sort and Quick sort are recursive Top-Down Dynamic Programming, etc

We do not discuss them now,but you may explore on your own

[Programming Appreciation Camp November 2008]

Page 46: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

LEARNING BY DOING7 more recursion examples that we will discuss together

46[Programming Appreciation Camp November 2008]

Page 47: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Hands-On Tasks (1)1. Modify recursive function floodfill(r,c,ID)

so that it is able to go to 8 directions(N, NE, E, SE, S, SW, W, NW)!

Given the same map,there will be just 4 lakes this time:look at lake number 3!

47

LLLLLLLLLLLWWLLWLLLWWLLLLLLLWWWLWWLLLLLWWWLLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LLWLLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLLWWLLWLLLWLWLLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LLWLLL3L3LLLLLLLLLLLLL

LLLLLLLLLLL11LL2LLL11LLLLLLL111L11LLLLL111LLLLLLLLLLLLLLL33LL4LLL3L3LLLLLLLLLLLLL

[Programming Appreciation Camp November 2008]

Page 48: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Hands-On Tasks (2)2. Create pow(a,n) to compute an!

Hint: modify pow2(n) a bit Sample calls:

pow(3,3) = 27 pow(4,2) = 16 pow(5,3) = 125 pow(7,3) = 343

48[Programming Appreciation Camp November 2008]

Page 49: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Hands-On Tasks (3)3. Create fast_pow(a,n) to compute an

with a better recursive formulation: Insight: a8 a8 = a4*a4; a4 = a2*a2; and a2 = a*a Only 3 multiplications to compute a8!

Logarithmic growth: log2 8 = 3 Special case: Odd exponent: a7=a*a6

Sample calls: fast_pow(3,3) = 27 fast_pow(4,2) = 16 fast_pow(5,3) = 125 fast_pow(7,3) = 343

49[Programming Appreciation Camp November 2008]

Page 50: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

50[Programming Appreciation Camp November 2008]

Recap - Euclidean Algorithm First documented algorithm by Greek mathematician

Euclid in 300 B.C. To compute the GCD (greatest common divisor) of 2

integers.

1. Let A and B be integers with A > B ≥ 0.

2. If B = 0, then the GCD is A and algorithm ends.

3. Otherwise, find q and r such that

A = q.B + r where 0 ≤ r < B

Note that we have 0 ≤ r < B < A and GCD(A,B) = GCD(B,r).

Replace A by B, and B by r. Go to step 2.

Page 51: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Hands-On Tasks (4)4. Create greatest common divisor function:

gcd(a,b) according to: gcd(a,b) = a if (b == 0)

gcd(b,a%b) otherwise Sample calls:

gcd(5,2) = 1, i.e., common divisor of 5 and 2 is 1 gcd(6,2) = 2 gcd(3,12) = 3 gcd(15,25) = 5

51[Programming Appreciation Camp November 2008]

Page 52: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

52[Programming Appreciation Camp November 2008]

Hands-On Tasks (5)5. A word is a palindrome if it reads the same

forward and backward How do you determine if a word is a palindrome?

Recursively! Sample calls:

Palindrome(“”) = false Palindrome(“O”) = true Palindrome(“OO”) = true Palindrome(“NOON”) = true Palindrome(“D”) = true Palindrome(“ADA”) = true Palindrome(“RADAR”) = true

Page 53: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

53[Programming Appreciation Camp November 2008]

Hands-On Tasks (6)6. North-east (NE) path: you may only move

northward or eastward Find the number of north-east paths

between A and C. Recursively!

C

A

Page 54: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

54[Programming Appreciation Camp November 2008]

Hands-On Tasks (7)7. Given this list of coin denominations: $1, 50

cents, 20 cents, 10 cents, 5 cents, 1 cent, find the smallest number of coins needed for a given amount. You do not need to list out what coins are used.

Example 1: For $3.75, 6 coins are needed.

Example 2: For $5.43, 10 coins are needed. Recursively!

Page 55: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

Q & A Session That’s all for today Any questions?

Contact: Steven Halim

[email protected] http://www.comp.nus.edu.sg/~stevenha/myteaching

55[Programming Appreciation Camp November 2008]

Page 56: Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.

THE END

56[Programming Appreciation Camp November 2008]