Data Structures and Algorithms with Object-Oriented Design Patterns (3)

 
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
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值