Dynamic Programming Algorithms

There are two ways to implement a DP algorithm:

  1. Bottom-up, also known as tabulation
  2. Top-down, also known as memoization

Bottom-up (Tabulation) (可以理解成顺思维)

Bottom-up is implemented with iteration and starts at the base cases. Using the Fibonacci sequence as an example again. The base cases for the Fibonacci sequence are F(0) = 0 and F(1) = 1. With bottom-up, we would see these base cases to calculate F(2), and then use the result to calculate F(3), and so on all the way up F(n).

// Pseudocode example for bottom-up

F = array of length (n+1)
F[0] = 0
F[1] = 1
for i from 2 to n:
	F[i] = F[i - 1] + F[i - 2]

Top-down (Memoization) (可以理解成逆思维)

Top-down is implemented with recursion and made efficient with memoization. If we wanted to find the nth Fibonacci number F(n), we try to compute this by finding F(n-1) and F(n-2). This defines a recursive pattern that will continue on until we reach the base cases F(0) = F(1) = 1. The problem with just implementing it recursively is that there is a ton of unnecessary repeated computation.

memoizing a result means to store the result of a function call, usually in a hashmap or an array, so that when the same function call is made again, we can simply return the memoized result instead of recalculating the result.

// Pseudocode example for top-down

memo = hashmap
Function F(integer i):
	if i is 0 or 1:
		return i
	if i doesn't exist in memo:
		memo[i] = F(i - 1) + F(i - 2)
	return memo[i]

Which is better?

Any DP algorithm can be implemented with either method, and there are reasons for choosing either over the other. However, each method has one main advantage that stands out:

  • A bottom-up implementation’s runtime is usually faster, as iteration does not have the overhead that recursion does.
  • A top-down implementation is usually much easier to write. This is because with recursion, the ordering of subproblems does not matter, whereas with tabulation, we need to go through a logical ordering of solving subproblems.

总结:
Top-down uses recursion
Bottom-up uses iteration

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值