Skip to content

Commit 7888b06

Browse files
authored
Update README.md
1 parent 9ec40a6 commit 7888b06

File tree

1 file changed

+89
-29
lines changed

1 file changed

+89
-29
lines changed

README.md

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ Runtime, assuming n-bit registers for each entry of memo data structure(cache Ob
146146
T(n) = T(n − 1) + O(1) = O(n)
147147
T(n) = T(n − 1) + c = O(cn)
148148
T(n) = O(n ^ 2)
149+
150+
```
151+
152+
* Show updated recursion tree for F(5) with Memoization method below.
153+
154+
```
155+
156+
/<------------- F(5) ------------------>\
157+
/ \
158+
F(4)[F(n - 1)] memo[F(3)]
159+
/ \
160+
/ \
161+
F(3)[F(n - 1)] memo[F(2)]
162+
/ \
163+
/ \
164+
F(2)[F(n - 1)] memo[F(2)]
165+
/ \
166+
/ \
167+
F(1)[F(n - 1)] F(0)[F(n - 2)]
168+
149169
```
150170

151171
JavaScript implementation:
@@ -179,8 +199,58 @@ one execution with same arguments:
179199
- F(2) - 1 times
180200
- F(3) - 1 times
181201

182-
For optimization memoization method time complexity, we can store the previous two numbers only
183-
because that is all we need to get the next Fibonacci number in series.
202+
### Improved Fibonacci Algorithm by Tail recursion
203+
204+
Alternative optimization recursion version, can be implementation of [tail recursion](https://en.wikipedia.org/wiki/Tail_call).
205+
Calculate the results first, and pass the results of your current call onto the next recursive call.
206+
207+
Tail recursion JavaScript implementation.
208+
209+
```js
210+
211+
/**
212+
* Time complexity: O(n)
213+
* Space complexity: O(n)
214+
*
215+
* @param number N
216+
* @return number
217+
*/
218+
function getFibonacciNumberTailRecursion (n, a = 0, b = 1){
219+
if (n > 0) {
220+
return getFibonacciNumberTailRecursion(n - 1, b, a + b);
221+
}
222+
return a;
223+
}
224+
225+
```
226+
227+
In the tail-recursive case, with each evaluation of the recursive call, the running total is updated.
228+
229+
To prove this conjecture by induction, let shows calls for F(5) tail recursion method.
230+
231+
```
232+
F(5 , 1, 1)
233+
||
234+
\/
235+
F(4 , 2, 1)
236+
||
237+
\/
238+
F(3 , 3, 2)
239+
||
240+
\/
241+
F(2 , 5, 3)
242+
243+
Result - 5
244+
```
245+
246+
### Improved Fibonacci Algorithm by iterative implementation
247+
248+
* This methodics result of Dynamic programming investigation from Tail Recursion.
249+
250+
Based on tail recursion implementation, for recrusion calls stack need O(n) space complexity. If
251+
modify implementation to iterative solution, solving Fibonacci sequence with O(1) space complexity, simply store the previous two numbers only.
252+
Because only two numbers need to get the next Fibonacci number in series.
253+
Single for loop operation take O(n) time complexity, for space complexity using 3 variables O(3) => O(1).
184254

185255
JavaScript iterative implementation
186256

@@ -202,7 +272,7 @@ function getFibonacciNumberIterative(N) {
202272
let b = 1;
203273
let sum = null;
204274

205-
for (let i =2; i <= N; i++) {
275+
for (let i = 2; i <= N; i++) {
206276
sum = a + b;
207277
a = b;
208278
b = sum;
@@ -212,34 +282,24 @@ function getFibonacciNumberIterative(N) {
212282
}
213283
```
214284

215-
Alternative optimization recursion version, can be implementation of [tail recursion](https://en.wikipedia.org/wiki/Tail_call).
216-
Calculate the results first, and pass the results of your current call onto the next recursive call.
217-
218-
Tail recursion JavaScript implementation.
219-
220-
```js
221-
222-
/**
223-
* Time complexity: O(n)
224-
* Space complexity: O(n)
225-
*
226-
* @param number N
227-
* @return number
228-
*/
229-
function getFibonacciNumberTailRecursion (n, a = 0, b = 1){
230-
if (n > 0) {
231-
return fib(n - 1, b, a + b)
232-
}
233-
return a
234-
}
285+
Iterative calls in for loop operations for N = 5
235286

287+
```
288+
i(2), a(0), b(1), sum(null) // on init
289+
||
290+
\/
291+
i(3), a(1), b(1), sum(1)
292+
||
293+
\/
294+
i(4), a(1), b(2), sum(2)
295+
||
296+
\/
297+
i(5), a(2), b(3), sum(3)
298+
299+
Result b = sum = a(2) + b(3) = 5
236300
```
237301

238-
In the tail-recursive case, with each evaluation of the recursive call, the running total is updated.
239-
240-
#### Bonus reference.
241-
242-
Faster Math solution, not related to Dynamic programming.
302+
More faster Math solution can be implemented based on Math formula calculation. This solution just bonus reference and not related to Dynamic programming topic.
243303

244304
```js
245305
/**
@@ -255,5 +315,5 @@ function getFibonacciNumberMath (N){
255315
}
256316
```
257317

258-
Formula [reference](http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html).
318+
Formula provide by this [reference](http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html).
259319

0 commit comments

Comments
 (0)