Dynamic Programming: Fibonacci
Recently I watched an interesting video in youtube, the vbloger use calculating Fibonacci number to explain dynamic programming
after watch this video, I decide to write it down in English, also for practice my written English
ok, in this article, we will assume you already know what's Finabonacci number
commonly, we always use recursion to get the number, it's pretty easy to implement it
Recursion:
int Fib(int n)
{
if (n <= ) return ;
return Fib(n - ) + Fib(n - );
}
This is also the example when we learn recursion
the time complexity is O(X=2^n), it's calcualted like this
Fib(n) once
Fib(n -1) once
Fib(n-2) twice
Fib(n-3) Third
the total time is equal = 1+2+3...+(n - 1) = 2^n
this approach works for most of cases, but it's no effective and will cause stack over exception if the number is big, because there's call stacks
it cost really long time when I set n = 100
so we need to improve the recursion
we can see some numbers are calculated multiple times
for instance, Fib(5) = Fib(4) + Fib(3), Fib(4) = Fib(3) + Fib(2), Fib(3) will be calculated twice
Let's think about an approach to avoid it
Recursion and Memoize
in this appraoch, we will store the number when it's calculated
int Fib(int n, int[] memoized)
{
if (memoized[n] != ) return memoized[n];
if (n <= ) return ;
int f = Fib(n - ) + Fib(n - );
memoized[n] = f;
return f;
}
ok, we will only calculate once for one number, and the time complexity is O(n)
however there are still lots of call stack while calculating
the above 2 approaches are calculated from top to bottom, from n, n-1,...,1
How about calculate from bottom, just like the exmaple number see, 1,1,2,3,5,6...
Bottom Up
int Fib(int n)
{
if (n <= ) return ;
var memoized = new int[n + ];
memoized[] = ;
memoized[] = ;
for (int i = ; i <= n; i++)
{
memoized[i] = memoized[i - ] + memoized[i - ];
}
return memoized[n];
}
in this approach, we calcuate from bottom to up, altthough we add extra space for new array, but there are not so many call stacks, it's effective
The time complexity is also O(n)
ok, this is the summary of the video, I also found a video which explain dynamic programming by MIT
Please also find this video for reference
Dynamic Programming I: Fibonacci, Shortest Paths
Dynamic Programming: Fibonacci的更多相关文章
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...
- [Optimization] Dynamic programming
“就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...
- 最优化问题 Optimization Problems & 动态规划 Dynamic Programming
2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...
- 笔试算法题(44):简介 - 动态规划(Dynamic Programming)
议题:动态规划(Dynamic Programming) 分析: DP主要用于解决包含重叠子问题(Overlapping Subproblems)的最优化问题,其基本策略是将原问题分解为相似的子问题, ...
- 五大常见算法策略之——动态规划策略(Dynamic Programming)
Dynamic Programming Dynamic Programming是五大常用算法策略之一,简称DP,译作中文是"动态规划",可就是这个听起来高大上的翻译坑苦了无数人 ...
- 动态规划(Dynamic Programming)算法与LC实例的理解
动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...
- 动态规划算法详解 Dynamic Programming
博客出处: https://blog.csdn.net/u013309870/article/details/75193592 前言 最近在牛客网上做了几套公司的真题,发现有关动态规划(Dynamic ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
随机推荐
- struts2防止表单重复提交的解决方案
一.造成重复提交主要的两个原因: 在平时的开发过程中,经常可以遇到表单重复提交的问题,如做一个注册页面,如果表单重复提交,那么一个用户就会注册多次,重复提交主要由于两种原因. 1. 一是,服务器 ...
- 从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系
被调试代码: package web; import java.util.Map; import javax.servlet.http.HttpServletRequest; import or ...
- java web hello world(二)基于Servlet理解监听
java web最开始实现是通过Servlet实现,这里就来实现下,最原始的监听是如何实现的. 第一步,创建一个基本的web项目 ,参见(java web hello world(一)) 第二步,we ...
- [Application]Ctrl+C终止程序代码
代码如下: #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sign ...
- 74HC164dD驱动LED
驱动要点: 1.上升沿写入串行数据: CLK=0; DAT=num&0x01; CLK=1; 2.写入数据的数码管编码(指代码中的 tab[]) 串行数据是FIFO先进先出,也就是先写高位,移 ...
- nginx的root alias 指令
location /img/ { alias /var/www/image/; } #若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件 ...
- python的post请求抓取数据
python通过get方式,post方式发送http请求和接收http响应-urllib urllib2 python通过get方式,post方式发送http请求和接收http响应-- import ...
- Linux 高频工具快速教程
全书分为三个部分: 第一部分为基础篇,介绍我们工作中常用的工具的高频用法: 第二部分为进阶篇,介绍的工具更多的适合程序员使用,分为程序构建.程序调试及程序优化: 第三部分是工具参考篇,主要介绍实用工具 ...
- JAVA学习资源网站
中文java技术网——http://www.cn-java.com/ 灰狐动力(http://www.huihoo.com/)—— 该站点有许多的开源的项目的介绍和学习,涉及操作系统,数据库等许多方向 ...
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...