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
 
原创粉丝点击