cst370 week 7
This week we covered dynamic programming and then moved on to the greedy technique. Both avoid using brute force but in different ways. Dynamic programming was developed in the 1950s, and the programming means planning, not coding. When a problem’s subproblems overlap we record each answer in a table and read the final answer off the table. Fibonacci's naive recursion solution computes F(1) five times just to get F(5), while DP computes each value once to solve fibonacci in Θ(n) instead of exponential time.
The coin row and coin collecting problems make a table of intermediate solutions and then backtrack to find the full optimal solution. Warshall’s algorithm finds the transitive closure by building matrices R(0) to R(n), where R(k) only allows the first k vertices as intermediates. Floyd’s algorithm uses the same structure with a minimum selection criteria to find all-pairs shortest paths. Both Warshall's and Floyd's algorithms run in O(n³).
Greedy is the opposite mindset. Every choice has to be feasible and locally optimal. It's not always the overall optimal solution though, but can still be used to give fast approximation of the solution. Prim’s algorithm is greedy because it repeatedly adds the vertex closest to the tree. Proving that a greedy algorithm is globally optimal is the hard part.
Comments
Post a Comment