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 ...
随机推荐
- C++ 基类指针和子类指针相互赋值
首先,给出基类animal和子类fish [cpp] view plaincopy //======================================================== ...
- Android从无知到有知——NO.5
今天整一下利用广播实现ip拨号. 这一块主要用到的知识是android四大组件之中的一个的broadcast receiver(广播接收者).那么它接收什么东东呢,就是我们所无谓的一个个的事件,比 ...
- Java常量池解析与字符串intern简介
在Java应用程序运行时,Java虚拟机会保存一份内部的运行时常量池,它区别于class文件的常量池,是class文件常量池映射到虚拟机中的数据结构. 关于class文件常量池的部分可以参考之前的博文 ...
- Selenium - Switch & Select Api
一.多表单切换 driver.switch_to.frame() iframe :直接将一个html 页面嵌入另一个html 页面中 switch_to.frame() 默认可以直接取表单的id ...
- python爬虫请求库之selenium模块
一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器 ...
- jQuery 效果 - slideToggle() 方法
实例 通过使用滑动效果,在显示和隐藏状态之间切换 <p> 元素: $(".btn1").click(function(){ $("p").slide ...
- android布局自适应小示例(用户反馈界面)
要求: 1.整个界面刚好填满屏幕,不需要滚动 2.输入反馈内容的EditText控件高度能够自适应 3.提交按钮位于屏幕最下方 核心布局文件如下: <?xml version="1.0 ...
- Ubuntu 文件文件夹查看权限和设置权限
ubuntu下查看权限的命令为: ls -l filename ls -ld folder ubuntu下设置权限的命令为: 一共有10位数 其中: 最前面那个 - 代表的是类型 中间那三个 rw- ...
- python学习笔记(3)--IDLE双击运行后暂停
本来想找一个python的IDE什么的,用过pycharm,vs装python插件,软件都太大了,习惯了用sublime写html,js这样的简直受不了. 一直坚持用着python自带的IDLE,不过 ...
- Entity Framework(七):Fluent API配置案例
一.配置主键 要显式将某个属性设置为主键,可使用 HasKey 方法.在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键. 1.新加Product类 usi ...