Data Structures and Algorithms with Object-Oriented Design Patterns (3)
来源:互联网 发布:烟花算法 matlab 编辑:程序博客网 时间:2024/04/28 00:20
Algorithm Patterns
Brute-Force Algorithms
A Brute-Force Algorithm processes problem in the most simple, direct and obvious way. It can end up doing far more works to solve problem than a more sophisticated algorithm. But it’s easier to implement.
Greedy Algorithms
A Greedy Algorithm makes the sequence of decisions such that once a decision has been made, it’s never reconsidered. A greedy algorithm can run significantly fast than brute-force one, but unfortunately, it is not always the case that a greedy strategy leads to the correct solution.
Example: Counting Change BFA: O (n^2) GA: O (N) but is not always the best solution
Backtracking Algorithms
A backtracking algorithm systematically considers all possible outcomes for each decision. It is like a brute-force one. However, it is distinguished by the way in which the space of possible solution is explored. Sometimes, the backtracking algorithm can detect an exhaustive search is unnecessary and, therefore, it can perform much better.
Example 1: Balancing Scales
Example 2: 0/1 Knapsack Problem
The solution space of the backtracking algorithm is a decision tree. We can implement it in both Depth-First way and Breadth-First way. And furthermore, we can make backtracking algorithms more efficient by a Branch and Bound strategy, using which we can prune the unnecessary branch in the decision tree. The trade-off is that the solution space is being pruned at the added expense of performing the test as each node is visited. And in mean time, the branch and bound technique is just a heuristic – sometimes it works and sometimes does not.
Divide and Conquer – A Top-Down Algorithm
Divide and Conquer algorithms subdivide the problem into one or more subproblems each of which is similar to the given problem. And then, the subproblems are solved independently. Finally, the solutions to all the subproblems are combined in order to obtain the solution to the original problem.
Divide and Conquer Algorithms are often implemented using recursion.
Example 1: Binary Search O (log n) T(n)=T(n/2)+O(1)
Example 2: Fibonacci Number O (n) T(n)=2T(n/2)+O(1)
F2k-1=(Fk)^2+(Fk-1)^2 F2k=(Fk)^2+2FkFk-1
Example 3: Merge Sort O (n log n) T(n)=2T(n)+O(n)
Example 4: Matrix Multiplication O (n^3) O(n^log 7)
Dynamic Programming – A Bottom-Up Algorithm
In order to solve a given problem, a series of subproblems is solved. And all intermediate solutions are kept in a table in order to prevent unnecessary duplication of effort.
Example 1: Generalized Fibonacci Numbers (save the sum of first n number)
Example 2: Compute Binomial Coefficients Space: O (n); Time: O (n^2)
(n, m)=(n-1, m)+(n-1, m-1) save the intermediate solutions in Pascal Triangle.
Example 3: Paradigm Typeset Problem:
Randomized Algorithm
Monte Carlo Methods
Simulated Annealing
- Data Structures and Algorithms with Object-Oriented Design Patterns (3)
- Data Structures and Algorithms with Object-Oriented Design Patterns (1)
- Data Structures and Algorithms with Object-Oriented Design Patterns (4)
- Data Structures and Algorithms with Object-Oriented Design Patterns (5)
- Data Structures and Algorithms with Object-Oriented Design Patterns (2 -- under construction)
- Data Structures And Algorithms With Object-Oriented+Design+Patterns in C#
- 翻译Data Structures and Algorithms with Object-Oriented Design Patterns in Python,写在前面的话
- Data Structures and Algorithms
- Data Structures and Algorithms
- Object-Oriented Analysis and Design with Applications
- Pythonic Data Structures and Algorithms(Array 3)
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(3)——Basic Data Structures
- Object Oriented Design -- Data and Algorithm Separation (1)
- Object Oriented Design -- Data and Algorithm Separation (2)
- Object-Oriented Analysis and Design with Applications (2nd Edition)
- Design Patterns For Object-Oriented JavaScript
- Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Deve
- Data Structures and Algorithms in Java
- IT程序员35岁之后的三条活路
- JS处理文件操作
- 使用异步 I/O 大大提高应用程序的性能
- Photoshop文字之——制作木板雕刻字
- ASP.NET2.0---C# 中需要注意的问题
- Data Structures and Algorithms with Object-Oriented Design Patterns (3)
- 匿名用户无权访问Portlet
- JSEclipse插件安装
- 用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联
- Photoshop文字之——制作逼真的水中倒影文字特效
- 一些有用的asp函数
- 面试题
- javascript获得页面各个部分的焦点
- GridView无数据时显示表头