Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Neuroinformatics wikipedia , lookup
Data analysis wikipedia , lookup
Inverse problem wikipedia , lookup
K-nearest neighbors algorithm wikipedia , lookup
Algorithm characterizations wikipedia , lookup
Data assimilation wikipedia , lookup
Pattern recognition wikipedia , lookup
Theoretical computer science wikipedia , lookup
History of the Church–Turing thesis wikipedia , lookup
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com Agenda • Recursion – Definition – Examples • Math Functions • Algorithms • Data – Computability CS-2852 Data Structures, Andrew J. Wozniewicz What Is Recursion? DEFINITION Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. • Recursion, n. see Recursion • Some functional programming languages don’t have loops (equivalent to imperative looping constructs) • A function “calls itself” – directly or not CS-2852 Data Structures, Andrew J. Wozniewicz Visual Recursion Source: Wikipedia CS-2852 Data Structures, Andrew J. Wozniewicz Recursion in Mathematics SET THEORY DEFINITION FOR NATURAL NUMBERS 1 is a natural number, and each natural number has a successor, which is also a natural number. • By this base case and recursive rule, one can generate the set of all natural numbers CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Function 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… Fibonacci Series • fib(0) = 0 • fib(1) = 1 • fib(n) = fib(n-1) + fib(n-2) (the growth of a population of rabbits under idealized assumptions) Leonardo Pisano Bigollo c. 1170 – c. 1250 Liber Abaci (The Book of Calculation) Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, Fibonacci 1.618 – Golden Ratio CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Numbers BINARY REPRESENTATION http://mathworld.wolfram.com 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 9E+09 8E+09 7E+09 50000 45000 40000 6E+09 5E+09 35000 30000 25000 4E+09 20000 15000 3E+09 10000 2E+09 5000 0 1E+09 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 CS-2852 Data Structures, Andrew J. Wozniewicz Factorial Function ! 0! = 1 n! = n * (n-1) 1: int fact(int n) { 2: if (n==0) 3: return 0; 6: return n*fact(n-1); 7: } CS-2852 Data Structures, Andrew J. Wozniewicz Greatest Common Divisor EUCLID’S ALGORITHM long gcd(long a, long b) { // a > b, b != 0 if (b==0) return a; return gcd(b, a % b); } CS-2852 Data Structures, Andrew J. Wozniewicz Recursively Count From n Downto 0 9876543210 void countDown(int n) { for System.out.print(i); System.out.print(n); (int i = n; i >= 0; i--) countDown(n-1); if (n System.out.print(i); == 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 CS-2852 Data Structures, Andrew J. Wozniewicz Questions? Image copyright © 2010 andyjphoto.com