Difference between revisions of "Fibonacci"
(→Videos) |
(→Videos) |
||
Line 11: | Line 11: | ||
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 [http://classes.engineering.wustl.edu/cse231/core/index.php/FAQ#Unit_2:_Futures.2FAccumulators.2FData_Races.2FMemoization memoization, dynamic programming], and the [https://en.wikipedia.org/wiki/Fibonacci_number fibonacci sequence]. | 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 [http://classes.engineering.wustl.edu/cse231/core/index.php/FAQ#Unit_2:_Futures.2FAccumulators.2FData_Races.2FMemoization memoization, dynamic programming], and the [https://en.wikipedia.org/wiki/Fibonacci_number fibonacci sequence]. | ||
− | + | * Video: <youtube>SjSHVDfXHQ4</youtube> | |
− | * <youtube>SjSHVDfXHQ4</youtube> | + | * Video: [https://www.scientificamerican.com/video/the-mind-blowing-mathematics-of-sunflowers/ Fibonacci Sunflowers] |
− | * [https://www.scientificamerican.com/video/the-mind-blowing-mathematics-of-sunflowers/ | ||
==Javadocs== | ==Javadocs== |
Revision as of 16:03, 6 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.
- Video:
- Video: Fibonacci Sunflowers
Javadocs
Studio (Required)
RecurrenceRelationSequentialFibonacciCalculator
Work: |
class: | RecurrenceRelationSequentialFibonacciCalculator.java | |
methods: | fibonacci | |
package: | fibonacci.studio | |
source folder: | student/src/main/java |
implement a recursive method for:
recurrence relation
seed values
RecurrenceRelationParallelFibonacciCalculator
Work: | |
CPL: | |
Ideal Parallelism: |
class: | RecurrenceRelationParallelFibonacciCalculator.java | |
methods: | fibonacci | |
package: | fibonacci.studio | |
source folder: | student/src/main/java |
Use futures to add parallelism to the recurrence relation solution.
MemoizationSequentialFibonacciCalculator
Work: |
class: | MemoizationSequentialFibonacciCalculator.java | |
methods: | fibonacciMemo | |
package: | fibonacci.studio | |
source folder: | student/src/main/java |
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); }
Warning: The entire point of memoization is to avoid recalculating the same computation. Be sure to store the value for each calculation as it is made even if that means splitting up a line of code. |
DynamicIterativeSequentialFibonacciCalculator
Work: |
class: | DynamicIterativeSequentialFibonacciCalculator.java | |
methods: | fibonacci | |
package: | fibonacci.studio | |
source folder: | student/src/main/java |
LinearRecurrenceSequentialFibonacciCalculator
Work: |
class: | LinearRecurrenceSequentialFibonacciCalculator.java | |
methods: | fibonacci | |
package: | fibonacci.studio | |
source folder: | student/src/main/java |
recurrence relation
- if n is odd
- else
seed values
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
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 |