Difference between revisions of "Fibonacci"
| Line 90: | Line 90: | ||
{{Sequential|public BigInteger fibonacci(int n)}} | {{Sequential|public BigInteger fibonacci(int n)}} | ||
| − | {{Warning | There is no need to store k in a BigInteger. Since it is smaller than n, it will fit just fine in an int. }} | + | {{Warning | Do NOT invoke, for example: <code>fibonacci(k)</code> or <code>fibonacci(k-1)</code>, multiple times. Catch the result in a variable and reuse it. }} |
| + | {{Tip | There is no need to store k in a BigInteger. Since it is smaller than n, it will fit just fine in an int. }} | ||
{{Tip | You can shift left by 1 if you would prefer to not have to multiply by the big integer value of 2.}} | {{Tip | You can shift left by 1 if you would prefer to not have to multiply by the big integer value of 2.}} | ||
===recurrence relation=== | ===recurrence relation=== | ||
Revision as of 22:05, 9 February 2018
Contents
- 1 Motivation
- 2 Background
- 3 Studio (Required)
- 4 Fun (Optional)
- 5 Testing Your Solution
Motivation
It is important to always reduce the work, then parallelize if possible. In this studio you will build a beautiful, elegant solution to Fibonacci which can be easily parallelized yielding off-the-charts ideal parallelism. Sadly, it performs terribly since the work is exponential. It is far better to build a linear or log time algorithm whether or not it can be parallelized.
We will gain experience with the future construct.
We will convert recurrence relations into code.
Additionally, (and of minor importance) we gain experience working with the BigInteger class.
Background
The fibonacci sequence is a mathematical concept often used in computer science as a means to demonstrate iteration and recursion. Although you should be familiar with it from CSE 131, we will use the fibonacci sequence to demonstrate memoization and dynamic programming. Follow these links for a quick recap on memoization, dynamic programming, and the fibonacci sequence.
Code To Use
Parallel Docs
BigInteger Docs
fibonacci(47) is greater than Integer.MAX_VALUE which would overflow if we used the primitive int data type. We use BigInteger instead to prevent this problem.
Studio (Required)
RecurrenceRelationSequentialFibonacciCalculator
| Work: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(\Phi^n)} |
| class: | RecurrenceRelationSequentialFibonacciCalculator.java | |
| methods: | fibonacci | |
| package: | fibonacci.studio | |
| source folder: | student/src/main/java |
method: public BigInteger fibonacci(int n)
(sequential implementation only)
implement a recursive method for:
recurrence relation
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_n = F_{n-1} + F_{n-2}}
seed values
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_0=0}
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_1=1}
RecurrenceRelationParallelFibonacciCalculator
| Work: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(\Phi^n)} |
| CPL: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(n)} |
| Ideal Parallelism: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \dfrac{\Phi^n}{n}} |
| class: | RecurrenceRelationParallelFibonacciCalculator.java | |
| methods: | fibonacci | |
| package: | fibonacci.studio | |
| source folder: | student/src/main/java |
method: public BigInteger fibonacci(int n)
(parallel implementation required)
Use futures to add parallelism to the recurrence relation solution.
MemoizationSequentialFibonacciCalculator
| Work: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(n)} |
| class: | MemoizationSequentialFibonacciCalculator.java | |
| methods: | fibonacciMemo | |
| package: | fibonacci.studio | |
| source folder: | student/src/main/java |
method: private BigInteger fibonacciMemo(BigInteger[] memos, int n)
(sequential implementation only)
Implement the recurrence relation algorithm but employ memoization for the win.
@Override
public BigInteger fibonacci(int n) {
BigInteger[] memos = new BigInteger[n + 1];
return fibonacciMemo(memos, n);
}
DynamicIterativeSequentialFibonacciCalculator
| Work: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(n)} |
| class: | DynamicIterativeSequentialFibonacciCalculator.java | |
| methods: | fibonacci | |
| package: | fibonacci.studio | |
| source folder: | student/src/main/java |
method: public BigInteger fibonacci(int n)
(sequential implementation only)
Imagine you are simply writing out Fibonacci numbers:
- 0, 1, 1, 2, 3, 5, 8, 13...
Convert that process of summing the two previous numbers over and over into code.
LinearRecurrenceSequentialFibonacciCalculator
| Work: | Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(\log{}n)} |
| class: | LinearRecurrenceSequentialFibonacciCalculator.java | |
| methods: | fibonacci | |
| package: | fibonacci.studio | |
| source folder: | student/src/main/java |
method: public BigInteger fibonacci(int n)
(sequential implementation only)
fibonacci(k) or fibonacci(k-1), multiple times. Catch the result in a variable and reuse it. |
recurrence relation
- if n is odd
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle k = \dfrac{n+1}{2}}
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_n = (F_{k} \times F_{k}) + (F_{k-1} \times F_{k-1})}
- else
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle k = \dfrac{n}{2}}
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_n = ((2 \times F_{k-1}) + F_{k}) \times F_{k}}
seed values
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_0=0}
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F_1=1}
Fun (Optional)
RecurrenceRelationParallelWithThresholdFibonacciCalculator
Similar to #RecurrenceRelationParallelFibonacciCalculator but with a threshold in place to prevent creating too many tasks for the number of processors.
MemoizationParallelFibonacciCalculator
Create all of your futures [0..N] up front and then call get on memo[n].
DynamicRecursiveSequentialFibonacciCalculator
RoundPhiToTheNOverSqrt5SequentialFibonacciCalculator
Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \dfrac{\Phi^n}{\sqrt{5}}}
Testing Your Solution
Correctness
| class: | FibonacciTestSuite.java | |
| package: | fibonacci.studio | |
| source folder: | testing/src/test/java |
Performance
| class: | FibonacciTiming.java | |
| package: | fibonacci.studio | |
| source folder: | src/main/java |
| class: | FibonacciIterations.java | |
| package: | fibonacci.studio | |
| source folder: | src/main/java |