Dynamic Programming
:material-circle-edit-outline: 约 161 个字 :fontawesome-solid-code: 44 行代码 :material-image-multiple-outline: 2 张图片 :material-clock-time-two-outline: 预计阅读时间 2 分钟
Intro¶
- Fibonacci Numbers
The problem is, normal recursion will calculate the same subproblem multiple times, which is not efficient.
DP solution:
Ordering Matrix Multiplications¶
- Problem:
Given a sequence of matrices to be multiplied, determine the order in which to carry out the multiplications so as to minimize the number of multiplications.
-
Solution:
If we have \(n\) matrices, \(b_n=\sum_{i=1}^{n-1}b_ib_{n-i}, where b_n=\text{different way to compute the sequence of n matrices}\), and if we do normal iteration to deal with this problem, the time complexity will be \(O(\frac{4^n}{n\sqrt{n}})\)
-
DP solution:
The time conplexity is \(O(N^3)\)
Optimal Binary Search Tree¶
Note
Ultilize the recursive essence of a tree
All-Pairs Shortest Path¶
-
Problem:
For all pairs of vi and vj ( i j ), find the shortest path between.
-
Solution:
Single-source algorithm-> \(O(|V|^3)\)
-
A more clever DP when coping dense graphs: 、
Reduce the time of sorting, unlike djikstra
Conclusion¶
- DP:
Note