lecture 23 recursive algorithms
play

Lecture 23: Recursive Algorithms Dr. Chengjiang Long Computer - PowerPoint PPT Presentation

Lecture 23: Recursive Algorithms Dr. Chengjiang Long Computer Vision Researcher at Kitware Inc. Adjunct Professor at SUNY at Albany. Email: clong2@albany.edu Outline Definition of Recursive Algorithms Several Recursive Algorithms


  1. Lecture 23: Recursive Algorithms Dr. Chengjiang Long Computer Vision Researcher at Kitware Inc. Adjunct Professor at SUNY at Albany. Email: clong2@albany.edu

  2. Outline Definition of Recursive Algorithms • Several Recursive Algorithms • Efficiency of Recursive Algorithms • 2 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  3. Outline Definition of Recursive Algorithms • Several Recursive Algorithms • Efficiency of Recursive Algorithms • 3 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  4. Recursive Algorithms Recursive definitions can be used to describe functions • and sets as well as algorithms. A recursive procedure is a procedure that invokes • itself. A recursive algorithm is an algorithm that contains a • recursive procedure. An algorithm is called recursive if it solves a problem • by reducing it to an instance of the same problem with smaller input. 4 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  5. Example A procedure to compute ! " . • procedure power (a≠0: real, n ∈ N) if n = 0 then return 1 else return a· power (a, n−1) q Note recursive algorithms are often simpler to code than iterative ones… q However, they can consume more stack space if your compiler is not smart enough 5 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  6. Outline Definition of Recursive Algorithms • Several Recursive Algorithms • Efficiency of Recursive Algorithms • 6 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  7. Recursive Euclid’s Algorithm gcd(a, b) = gcd((b mod a), a) • procedure gcd(a,b ∈ N with a < b ) if a = 0 then return b else return gcd(b mod a, a) 7 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  8. Recursive Linear Search {Finds x in series a at a location ≥ i and ≤ j } • procedure search ( a : series; i , j : integer; x : item to find) if a i = x return i {At the right item? Return it!} if i = j return 0 {No locations in range? Failure!} return search ( a , i +1, j , x ) {Try rest of range} Note there is no real advantage to using recursion here • over just looping for loc := i to j… Recursion is slower because procedure call costs • 8 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  9. Recursive Binary Search {Find location of x in a , ≥ i and ≤ j } procedure binarySearch ( a , x , i , j ) m := ⎣ ( i + j )/2 ⎦ {Go to halfway point} if x = am return m {Did we luck out?} if x < am ∧ i < m {If it’s to the left, check that .} return binarySearch ( a , x , i , m −1) else if x > am ∧ j > m {If it’s to right, check that .} return binarySearch ( a , x , m +1, j ) else return 0 {No more items, failure.} 9 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  10. Recursive Fibonacci Algorithm procedure fibonacci ( n ∈ N ) if n = 0 return 0 if n = 1 return 1 return fibonacci ( n − 1) + fibonacci ( n − 2) q Is this an efficient algorithm? q How many additions are performed? 10 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  11. Analysis of Fibonacci Procedure Theorem: The recursive procedure fibonacci ( n ) performs f n +1 − 1 additions. Proof: By strong structural induction over n , based on • the procedure’s own recursive definition. Basis step: • fibonacci (0) performs 0 additions, and f 0 +1 − 1 = f 1 − 1 = 1 − • 1 = 0. Likewise, fibonacci (1) performs 0 additions, and f 1 +1 − 1 = f 2 • − 1 = 1 − 1 = 0. 11 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  12. Analysis of Fibonacci Procedure Inductive step: For k >1, by strong inductive hypothesis, fibonacci ( k ) • and fibonacci ( k −1) do f k +1 − 1 and f k − 1 additions respectively. fibonacci ( k +1) adds 1 more, for a total of • ( f k +1 − 1) + ( f k − 1) + 1 = f k +1 + f k − 1 = f k +2 − 1. 12 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  13. Iterative Fibonacci Algorithm procedure iterativeFib ( n ∈ N ) • if n = 0 then • return 0 • else begin • x : = 0 • y := 1 • for i := 1 to n – 1 begin • Requires only z := x + y • n – 1 additions x := y • y := z • end • end • return y {the n th Fibonacci number} • 13 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  14. Recursive Merge Sort Example 14 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  15. Recursive Merge Sort procedure mergesort ( L = 1,…, n ) if n > 1 then m := ⎣ n /2 ⎦ {this is rough ½-way point} L 1 := 1,…, m L 2 := m+ 1,…, n L := merge ( mergesort ( L 1), mergesort ( L 2)) return L The merge takes Θ( n ) steps, and therefore the • merge-sort takes Θ( n log n ). 15 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  16. Merging Two Sorted Lists 16 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  17. Recursive Merge Method {Given two sorted lists A = ( a 1 ,…, a | A | ), B = ( b 1 ,…, b | B | ), returns a sorted list of all.} procedure merge ( A , B : sorted lists) if A = empty return B {If A is empty, it’s B .} if B = empty return A {If B is empty, it’s A .} if a 1 < b 1 then return ( a 1 , merge (( a 2 ,…, a | A | ), B )) else return ( b 1 , merge ( A , ( b 2 ,…, b | B | ))) 17 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  18. Outline Definition of Recursive Algorithms • Several Recursive Algorithms • Efficiency of Recursive Algorithms • 18 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  19. Efficiency of Recursive Algorithms The time complexity of a recursive algorithm may • depend critically on the number of recursive calls it makes. Example: Modular exponentiation to a power n can • take log( n ) time if done right, but linear time if done slightly differently. Task: Compute ! " mod m , where m ≥2, n ≥0, and • 1≤ b < m . 19 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  20. Modular Exponentiation #1 Uses the fact that ! " = b· ! "#$ and that • x·y mod m = x· ( y mod m ) mod m . (Prove the latter theorem at home.) {Returns % & mod m .} procedure mpower ( b , n, m : integers with m ≥2, n ≥0, and 1≤ b < m ) if n =0 then return 1 else return ( b·mpower ( b , n −1, m )) mod m Note this algorithm takes Θ( n ) steps! • 20 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  21. Modular Exponentiation #2 Uses the fact that ! "# = ! # · " = (! # ) " • Then, ! "# mod m = ( ! # mod m ) " mod m . • procedure mpower ( b , n , m ) {same signature} if n =0 then return 1 else if 2| n then return mpower ( b , n /2, m )2 mod m else return ( b·mpower ( b , n −1, m )) mod m What is its time complexity? Θ(log n ) steps • 21 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  22. A Slight Variation Nearly identical but takes Θ( n ) time instead! • procedure mpower ( b , n , m ) {same signature} if n =0 then return 1 else if 2| n then return ( mpower ( b , n /2, m )· mpower ( b , n /2, m )) mod m else return ( mpower ( b , n −1, m )· b ) mod m The number of recursive calls made is critical! 22 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

  23. Next class Topic: Recursive Algorithms and Basic Counting Rules • Pre-class reading: Chap 5.4 and Chap 6.1 • 23 C. Long ICEN/ICSI210 Discrete Structures Lecture 23 October 30, 2018

Recommend


More recommend