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 ...
 
随机推荐
- Atitit.mssql 数据库表记录数and 表体积大小统计
			
Atitit.mssql 数据库表记录数and 表体积大小统计 1. EXEC sp_MSforeachtable "EXECUTE sp_spaceused '?'&quo ...
 - android.animation(6) - AnimatorSet
			
上几篇给大家分别讲了ValueAnimator和ObjectAnimator,相比而言ObjectAnimator更为方便而且由于set函数是在控件类内部实现,所以封装性更好.而且在现实使用中一般而言 ...
 - Git Manual / Git使用手册 / Git, GitLab, Git Bash, TortoiseGit (建议全文复制到Word文档中通过导航窗格查看)
			
Git使用手册 目录 1 引言 2 Git.GitLab简介 2.1 Git 2.2 GitLab 2.3 Git基本概念 3 运行环境 4 ...
 - 使用nio实现web服务器
			
package com.nio; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocket ...
 - 【转】CentOS下expect 安装
			
Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理方面发挥很大的作用. spawn命令激活一个Unix程序 ...
 - 绕过IE10直接安装VS2013
			
参考资料:http://blog.163.com/qimo601%40126/blog/static/1582209320143354446462/ 这SB设定我就懒得说了,安个IE10要安装N多WI ...
 - 关于浏览器对静态HTML页面的缓存问题
			
症状: 刚才为了测试TOMCAT的BASIC安全验证,修改了tomcat-users.xml和/WEB-INF/web.xml之后进行测试,<url-pattern>/*<url-p ...
 - 一款基于jquery的喜欢动画按钮
			
今天给大家带来一款基于jquery的喜欢动画按钮.这个实例中给了三种动画特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <p class='heading'> C ...
 - 下列JSP代码:
			
下列JSP代码: <html> <body> <% for(int i = 0; i < 10; i++) { //1 } %> </body> ...
 - 【UVa】And Then There Was One(dp)
			
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...