Lecture 9: Recursion and Fibonacci Numbers PDF
Document Details
Uploaded by FervidDune
ETH Zurich
Tags
Summary
This lecture covers recursion, focusing on Fibonacci numbers, and explores different approaches and C++ implementations. It details various algorithms, including iterative and recursive methods. The lecture slides present visual demonstrations and code examples to illustrate the concepts.
Full Transcript
15. Recursion II Decrease and Conquer vs. Divide and Conquer, Recursion vs. Iteration 397 Sum of Vector Well-known problem: sum of all entries of a vector std::vector v Simple iterative solution: int sum(const std::vector& v)...
15. Recursion II Decrease and Conquer vs. Divide and Conquer, Recursion vs. Iteration 397 Sum of Vector Well-known problem: sum of all entries of a vector std::vector v Simple iterative solution: int sum(const std::vector& v) { int s = 0; for (int i = 0; i < int(v.size()); ++i) { s += v.at(i); } return s; } 398 Sum of Vector: Variant 1 sum({}) 0 sum({7}) 7+ 7 sum({6, 7}) 6+ 13 sum({4, 6, 7}) 4+ 17 sum({-1, 4, 6, 7}) −1+ 16 399 Sum of Vector: Variant 1 int sum(const std::vector& v, int from) { if (from >= int(v.size())) return 0; else return v.at(from) + sum(v, from + 1); } call with e.g. sum({-1, 4, 6, 7}, 0); 400 Sum of Vector: Variant 2 sum({-1}) sum({4}) sum({6}) sum({7}) −1 6 + 4 + 7 sum({-1, 4}) sum({6, 7}) 3 + 13 sum({-1, 4, 6, 7}) 16 401 Sum of Vector: Variant 2 int sum(const std::vector& v, int from, int to) { if (from > to) return 0; else if (from == to) return v.at(from); else { int middle = (from + to) / 2; return sum(v, from, middle) + sum(v, middle + 1, to); } } call with e.g. sum({-1, 4, 6, 7}, 0, 3); 402 Recursive Strategies......... Decrease and Conquer Divide and Conquer remove one element, halving the elements, one recursive call on remainder one recursive call per half limitedly parallelizable oftentimes well-parallelizable large recursion depth smaller recursion depth 403 Fibonacci Numbers 0, if n = 0 Fn := 1, if n = 1 Fn−1 + Fn−2, if n > 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... 404 Fibonacci Numbers in C++ Laufzeit fib(50) takes “forever” because it computes F48 two times, F47 3 times, F46 5 times, F45 8 times, F44 13 times, F43 21 times... F1 ca. 109 times (!) int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); // n > 1 } Fibonacci Visualised................................................ F47 F46 F46 F45 F46 F45 F45 F44 F48 F47 F47 F46 F49 F48 F50 Recomputing same values = re-solving same subproblems 406 Fast (Iterative) Fibonacci Numbers Idea: Compute each Fibonacci number only once, in the order F0 , F1 , F2 ,... , Fn Memorize the most recent two Fibonacci numbers (variables fib1 and fib2) Compute the next number as a sum of fib1 and fib2 Can be implemented recursively and iteratively, the latter is easier/more direct 407 Fast Fibonacci Numbers in C++ int fibonacci(int n) { if (n == 0) return 0; if (n == 1) return 1; int fib2 = 0; // value two before int fib1 = 1; // value one before int fib; // current value very fast, also for fib(50) for (int i = 2; i r; std::cout > s; // computation and output std::cout