Skip to content

Commit b2d67c9

Browse files
committed
update DP info
1 parent 17237f8 commit b2d67c9

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎DynamicProgramming/README.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ Dynamic programming (DP) is
1010

1111
**memoize** - (remember) & re-use solutions to subproblems that help solve problem
1212

13-
## bottom-up
13+
The memoized program for a problem is similar to the recursive version with a small modification that it looks into a lookup table before computing solutions. We initialize a lookup array with all initial values as NIL. Whenever we need the solution to a subproblem, we first look into the lookup table. If the precomputed value is there then we return that value, otherwise, we calculate the value and put the result in the lookup table so that it can be reused later.
1414

15-
idea of algorithm is start solving program from bottom and go up, may uses in junction with memoize
15+
## bottom-up (Tabulation)
16+
17+
idea of algorithm is start solving program from bottom and go up, may uses in junction with memoize.
18+
19+
The tabulated program for a given problem builds a table in bottom up fashion and returns the last entry from table. For example, for the same Fibonacci number, we first calculate fib(0) then fib(1) then fib(2) then fib(3) and so on. So literally, we are building the solutions of subproblems bottom-up.
1620

1721
## Guessing
1822

@@ -26,3 +30,10 @@ try all possible ways of solving subproblem and choose the best one.
2630
1. build algorithm memoize && (recursive || build DP table bottom-up)
2731
1. try to find ways to save space(memory)
2832
1. solve original problem
33+
34+
35+
## Properties in Dynamic Programming
36+
37+
### Overlapping Subproblems
38+
39+
Like Divide and Conquer, Dynamic Programming combines solutions to sub-problems. Dynamic Programming is mainly used when solutions of same subproblems are needed again and again. In dynamic programming, computed solutions to subproblems are stored in a table so that these don’t have to be recomputed. So Dynamic Programming is not useful when there are no common (overlapping) subproblems because there is no point storing the solutions if they are not needed again. For example, Binary Search doesn’t have common subproblems. If we take an example of following recursive program for Fibonacci Numbers, there are many subproblems which are solved again and again.

‎sort/merge_sort_v3.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313

1414
def merge(a1, a2):
1515
i, j, result, m, n = 0, 0, [], len(a1), len(a2)
16-
ra = result.append
1716
while i < m and j < n:
1817
if a1[i] <= a2[j]:
19-
ra(a1[i])
18+
result.append(a1[i])
2019
i += 1
2120
else:
22-
ra(a2[j])
21+
result.append(a2[j])
2322
j += 1
2423
result += a1[i:]
2524
result += a2[j:]

0 commit comments

Comments
 (0)