CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com
CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Recursion – Definition – Examples Math Functions Algorithms Data – Computability
CS-2852 Data Structures, Andrew J. Wozniewicz What Is Recursion? Recursion, n. see Recursion Some functional programming languages don’t have loops (equivalent to imperative looping constructs) A function “calls itself” – directly or not Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz Visual Recursion Source: Wikipedia
CS-2852 Data Structures, Andrew J. Wozniewicz Recursion in Mathematics By this base case and recursive rule, one can generate the set of all natural numbers 1 is a natural number, and each natural number has a successor, which is also a natural number. SET THEORY DEFINITION FOR NATURAL NUMBERS
CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Function fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) Fibonacci Series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… Leonardo Pisano Bigollo c – c Liber Abaci (The Book of Calculation) (the growth of a population of rabbits under idealized assumptions) Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, Fibonacci – Golden Ratio
CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Numbers BINARY REPRESENTATION The first 511 terms of the Fibonacci sequence represented in binary.
CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Function 1: int fib(int n) { 2: if (n==0) 3: return 0; 4: if (n==1) 5: return 1; 6: return fib(n-1) + fib(n-2); 7: } DEMO!
CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Numbers - Plotted
CS-2852 Data Structures, Andrew J. Wozniewicz Factorial Function ! 1: int fact(int n) { 2: if (n==0) 3: return 0; 6: return n*fact(n-1); 7: } 0! = 1 n! = n * (n-1)
CS-2852 Data Structures, Andrew J. Wozniewicz Greatest Common Divisor long gcd(long a, long b) { // a > b, b != 0 if (b==0) return a; return gcd(b, a % b); } EUCLID’S ALGORITHM
CS-2852 Data Structures, Andrew J. Wozniewicz Recursively Count From n Downto 0 void countDown(int n) { for (int i = n; i >= 0; i--) System.out.print(i); } void countDown(int n) { System.out.print(i); countDown(n-1); } void countDown(int n) { System.out.print(n); if (n == 0) return; countDown(n-1); }
CS-2852 Data Structures, Andrew J. Wozniewicz Other Examples of Recursive Algorithms Linear search through an array Binary search in an array
CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Algorithm Design Identify the base case – the small problem that can be solved directly Split the big problem into smaller subproblems – Divide-and-Conquer Combine the solutions to the smaller subproblems
CS-2852 Data Structures, Andrew J. Wozniewicz Tail Recursion Occurs when the recursive call is at the “end” of the recursive function. The algorithm can usually be rewritten to use (pure) iteration instead. Computational Equivalence: – f1() { while(C) { S; }; return Q; } – f2() { if (C) then { S; return f(); } else { return Q; } }
CS-2852 Data Structures, Andrew J. Wozniewicz Recursion versus Iteration Both allow to repeat an operation You can always write a recursive solution to a problem solvable by iteration. The reverse is not necessarily true: – Some recursion problems (non-tail recursion) cannot be solved by iteration alone.
CS-2852 Data Structures, Andrew J. Wozniewicz “Proving” Correctness Verify that the base case is recognized and solved correctly. Verify that each recursive case makes progress towards the base case. Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly.
CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Data Structures Linked-List: – Node – Node, Linked-List Tree – Node – Left-(Sub) Tree – Right-(Sub)Tree
CS-2852 Data Structures, Andrew J. Wozniewicz Summary Recursion – Recursive Functions Fibonacci Factorial Greatest Common Divisor – Recursive Algorithms Designing Recursive Algorithms Recursion versus Iteration Proving Correctness – by Induction Tail Recursion – Recursive Data Structures
Questions? Image copyright © 2010 andyjphoto.com