1 / 56

Programming Appreciation Camp

Programming Appreciation Camp. Session 2: Recursion Steven Halim NUS School of Computing. 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.

Télécharger la présentation

Programming Appreciation Camp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 [Programming Appreciation Camp November 2008]

  3. 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! [Programming Appreciation Camp November 2008]

  4. Non Computing Examples • Droste Effecthttp://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 Doll http://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]

  5. Non Computing Examples: Fractal • Sierpinski Triangle • Koch Snowflake • Recursive Tree • More Fractals • http://en.wikipedia.org/wiki/Fractal [Programming Appreciation Camp November 2008]

  6. 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. • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LL5L3LLLL • LLLLLLLLL The answer for this scenario: 5 lakes! [Programming Appreciation Camp November 2008]

  7. 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 • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL12LL3LL • L45LLLLLL • L678L9ALL • LLLBCDLLL • LLLLLLLLL • LLLEFLLGL • LLHLILLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LL5L3LLLL • LLLLLLLLL 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! [Programming Appreciation Camp November 2008]

  8. 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 [Programming Appreciation Camp November 2008]

  9. Why use Recursion? • Many computer algorithms can be expressed naturally in recursive form • Trying to express these algorithms iterativelycan be actually confusing… • e.g. “finding lakes” example • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL [Programming Appreciation Camp November 2008]

  10. Recursion: Implemented As Function • Function is simply defined as follow: • output function_name(inputs) process the inputs return output [Programming Appreciation Camp November 2008]

  11. 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]

  12. 7 recursion examples to get you started Learning by example [Programming Appreciation Camp November 2008]

  13. 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. [Programming Appreciation Camp November 2008]

  14. 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 = ” + ncount_down(n-1) 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 [Programming Appreciation Camp November 2008]

  15. 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 = ” + ncount_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!!!! [Programming Appreciation Camp November 2008]

  16. 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 [Programming Appreciation Camp November 2008]

  17. 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) 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) • Recursive definition: • fact(n) = 1 if (n = 0) • n * fact (n-1) if (n > 0) Note that this recursive function returns a number, which is the result of fact(n) [Programming Appreciation Camp November 2008]

  18. 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  [Programming Appreciation Camp November 2008]

  19. 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… [Programming Appreciation Camp November 2008]

  20. 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 [Programming Appreciation Camp November 2008]

  21. 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) 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! • Recursive definition: • Fn = 0 if n = 0 • 1 if n = 1 • Fn-1 + Fn-2 if n > 1 Here, we observe that a recursive function can call itself more than once! (branching recursion) [Programming Appreciation Camp November 2008]

  22. 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… [Programming Appreciation Camp November 2008]

  23. 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 [Programming Appreciation Camp November 2008]

  24. 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 Fib 2 Fib 3 1 Fib 1 Fib 0 Fib 0 Fib1 Fib 1 Fib 2 1 0 0 1 1 Fib 0 Fib1 0 1

  25. 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 = _____________ _____________ _____________ [Programming Appreciation Camp November 2008]

  26. 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) 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) [Programming Appreciation Camp November 2008]

  27. 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 [Programming Appreciation Camp November 2008]

  28. 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 [Programming Appreciation Camp November 2008]

  29. Example 5: Print Digits (2)http://en.wikipedia.org/wiki/Decimal • Recursive version: digit(n) if (n > 0) digit(n / 10) print n % 10 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 themodulus (remainder) operator. Here, we observe that base case can be as simple as doing nothing and we can still do something after recursive calls! [Programming Appreciation Camp November 2008]

  30. 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! [Programming Appreciation Camp November 2008]

  31. 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  [Programming Appreciation Camp November 2008]

  32. Recall - Task 2: Pascal’s Trianglehttp://en.wikipedia.org/wiki/Pascal%27s_triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Compute nCk or C(n,k) nCk = n! / (k! * (n – k)!) [Programming Appreciation Camp November 2008] 32

  33. 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]

  34. 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]

  35. Example 6: Ways to choose(n, k) (3)http://en.wikipedia.org/wiki/Combinations • How many ways to choose k out of n items? 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) [Programming Appreciation Camp November 2008]

  36. Example 6: Ways to choose(k, n) (4)http://en.wikipedia.org/wiki/Combinations • How many ways to choose k out of n items? 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! [Programming Appreciation Camp November 2008]

  37. 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. [Programming Appreciation Camp November 2008]

  38. 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: left else if (arr[mid] < key) return bsearch(arr, key, mid+1, high) // recursive case 2: right else return mid // found, base case 2 [Programming Appreciation Camp November 2008]

  39. 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 [Programming Appreciation Camp November 2008]

  40. 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… [Programming Appreciation Camp November 2008]

  41. 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. • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LL5L3LLLL • LLLLLLLLL The answer for this scenario: 5 lakes! [Programming Appreciation Camp November 2008]

  42. 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?) • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LLWLL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL [Programming Appreciation Camp November 2008]

  43. Lake Question (Revisited) (3)http://en.wikipedia.org/wiki/Flood_fill • Flood Fill Recursive Algorithm floodfill(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’ • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LLWLL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL [Programming Appreciation Camp November 2008]

  44. Lake Question (Revisited) (4) • Now, Use floodfillto 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 • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LLWLL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LLWL • LLWL3LLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LLWL3LLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LL5L3LLLL • LLLLLLLLL [Programming Appreciation Camp November 2008]

  45. 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]

  46. 7 more recursion examples that we will discuss together Learning by DOING [Programming Appreciation Camp November 2008]

  47. Hands-On Tasks (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! • LLLLLLLLL • LLWWLLWLL • LWWLLLLLL • LWWWLWWLL • LLLWWWLLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LLWLL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLLWWLLWL • LLWLWLLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LLWL • LL3L3LLLL • LLLLLLLLL • LLLLLLLLL • LL11LL2LL • L11LLLLLL • L111L11LL • LLL111LLL • LLLLLLLLL • LLL33LL4L • LL3L3LLLL • LLLLLLLLL [Programming Appreciation Camp November 2008]

  48. Hands-On Tasks (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 [Programming Appreciation Camp November 2008]

  49. Hands-On Tasks (3) • Create fast_pow(a,n) to compute anwith 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 [Programming Appreciation Camp November 2008]

  50. Recap - Euclidean Algorithm • First documented algorithm by Greek mathematician Euclid in 300 B.C. • To compute the GCD (greatest common divisor) of 2 integers. • Let A and B be integers with A > B ≥ 0. • If B = 0, then the GCD is A and algorithm ends. • 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. [Programming Appreciation Camp November 2008]

More Related