|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Fibonacci number computation: Three implementation strategies compared. More...
#include <iostream>#include <cstdlib>#include <chrono>#include <tclap/CmdLine.h>#include <tpl_arrayStack.H>#include <ah-errors.H>Go to the source code of this file.
Classes | |
| struct | Activation_Record |
| Activation record structure for stack-based Fibonacci. More... | |
Macros | |
| #define | NUM(p) ((p)->n) |
| Accessor macros for top of stack (current activation record) | |
| #define | F1(p) ((p)->f1) |
| #define | RESULT(p) ((p)->result) |
| #define | RETURN_POINT(p) ((p)->return_point) |
Functions | |
| long long | fib_recursive (int n) |
| Compute Fibonacci number recursively. | |
| long long | fib_iterative (int n) |
| Compute Fibonacci number iteratively. | |
| long long | fib_stack (int n) |
| Compute Fibonacci number using explicit stack management. | |
| int | main (int argc, char *argv[]) |
Variables | |
| constexpr char | P1 = 1 |
| Return point labels (simulating goto targets after recursive calls) | |
| constexpr char | P2 = 2 |
| Return point after second recursive call fib(n-2) | |
Fibonacci number computation: Three implementation strategies compared.
This example demonstrates three fundamentally different approaches to computing Fibonacci numbers, each illustrating important programming concepts and trade-offs. The Fibonacci sequence is one of the most famous sequences in mathematics and computer science, making it perfect for teaching algorithm design.
Defined recursively as:
Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Approach: Direct translation of mathematical definition
Code Structure:
Complexity:
Pros:
Cons:
Best for: Educational purposes, small n, understanding recursion
Approach: Bottom-up computation using a loop
Code Structure:
Complexity:
Pros:
Cons:
Best for: Production code, large n, performance-critical applications
Approach: Simulates recursive call stack explicitly using ArrayStack
Key Concept: Shows how recursion works internally
What It Demonstrates:
Complexity:
Educational Value:
Best for: Learning how recursion works, understanding call stacks
| Method | Time | Space | Redundant Work |
|---|---|---|---|
| Recursive | O(2^n) | O(n) | Yes (exponential) |
| Iterative | O(n) | O(1) | No |
| Stack-based | O(n) | O(n) | No |
For even better performance, consider:
Definition in file fibonacci.C.
| #define F1 | ( | p | ) | ((p)->f1) |
Definition at line 277 of file fibonacci.C.
| #define NUM | ( | p | ) | ((p)->n) |
Accessor macros for top of stack (current activation record)
Definition at line 276 of file fibonacci.C.
| #define RESULT | ( | p | ) | ((p)->result) |
Definition at line 278 of file fibonacci.C.
| #define RETURN_POINT | ( | p | ) | ((p)->return_point) |
Definition at line 279 of file fibonacci.C.
| long long fib_iterative | ( | int | n | ) |
Compute Fibonacci number iteratively.
Bottom-up computation maintaining only the last two values.
Time complexity: O(n) - linear Space complexity: O(1) - constant
| n | The index of the Fibonacci number to compute |
Definition at line 227 of file fibonacci.C.
References Aleph::maps().
Referenced by main().
| long long fib_recursive | ( | int | n | ) |
Compute Fibonacci number recursively.
Classic recursive definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2)
Time complexity: O(2^n) - exponential Space complexity: O(n) - call stack depth
| n | The index of the Fibonacci number to compute |
Definition at line 202 of file fibonacci.C.
References fib_recursive().
Referenced by fib_recursive(), and main().
| long long fib_stack | ( | int | n | ) |
Compute Fibonacci number using explicit stack management.
This implementation manually manages what the compiler does automatically for recursive calls:
The flow simulates this recursive code:
Time complexity: O(2^n) - same as recursive (we're simulating it exactly) Space complexity: O(n) - explicit stack instead of call stack
| n | The index of the Fibonacci number to compute |
Definition at line 309 of file fibonacci.C.
References AH_ERROR, F1, Aleph::maps(), NUM, P1, P2, Aleph::ArrayStack< T >::pop(), Aleph::ArrayStack< T >::pushn(), RESULT, RETURN_POINT, Aleph::ArrayStack< T >::size(), and Aleph::ArrayStack< T >::top().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 398 of file fibonacci.C.
References fib_iterative(), fib_recursive(), fib_stack(), and Aleph::maps().
|
constexpr |
Return point labels (simulating goto targets after recursive calls)
Return point after first recursive call fib(n-1)
Definition at line 272 of file fibonacci.C.
Referenced by fib_stack().
|
constexpr |
Return point after second recursive call fib(n-2)
Definition at line 273 of file fibonacci.C.
Referenced by fib_stack().