Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.

Similar presentations


Presentation on theme: "David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued."— Presentation transcript:

1 David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued

2 David Luebke 2 10/24/2015 Administrivia l Hand in Homework 4 l Hand back remaining midterms

3 David Luebke 3 10/24/2015 Review: LCS Via Dynamic Programming l Longest common subsequence (LCS) problem: n Given two sequences x[1..m] and y[1..n], find the longest subsequence which occurs in both l Brute-force algorithm: 2 m subsequences of x to check against n elements of y: O(n 2 m ) l Define c[i,j] = length of LCS of x[1..i], y[1..j] l Theorem:

4 David Luebke 4 10/24/2015 Review: Structure Of LCS Problem l Observation 1: Optimal substructure n Thus a simple recursive algorithm will suffice n Subproblems: LCS of prefixes of x,y l Observation 2: Overlapping subproblems n Many recurring instances of each subproblem n Unlike divide and conquer, where subproblems are distinct

5 David Luebke 5 10/24/2015 Review: Memoization l Memoization is one way to deal with overlapping subproblems n After computing the solution to a subproblem, store in a table n Subsequent calls just do a table lookup l Can modify recursive alg to use memoziation: n There are mn subproblems n Each wanted 3 times: compute once, lookup twice n Running time: O(mn). Running space: O(mn).

6 David Luebke 6 10/24/2015 Review: Dynamic Programming l Dynamic programming: build table bottom-up n Same table as memoization, but instead of starting at (m,n) and recursing down, start at (1,1) n Each cell can be computed in constant time from its 3 neighbors to the left and up l Running time/space: O(mn) l Can actually reduce space to O(min(m,n)) by keeping only two rows/columns around

7 David Luebke 7 10/24/2015 Review: Dynamic Programming l Summary of the basic idea: n Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems n Overlapping subproblems: few subproblems in total, many recurring instances of each n Solve bottom-up, building a table of solved subproblems that are used to solve larger ones l Variations possible! n “Table” could be 3-dimensional, triangular, a tree, etc.

8 David Luebke 8 10/24/2015 Greedy Algorithms l A greedy algorithm always makes the choice that looks best at the moment n The hope: a locally optimal choice will lead to a globally optimal solution n For some problems, it works n My everyday examples: u Walking to the Corner u Playing a bridge hand l Dynamic programming can be overkill; greedy algorithms tend to be easier to code

9 David Luebke 9 10/24/2015 Activity-Selection Problem l Problem: get your money’s worth out of a carnival n Buy a wristband that lets you onto any ride n Lots of rides, each starting and ending at different times n Your goal: ride as many rides as possible u Another, alternative goal that we don’t solve here: maximize time spent on rides l Welcome to the activity selection problem

10 David Luebke 10 10/24/2015 Activity-Selection l Formally: n Given a set S of n activities s i = start time of activity i f i = finish time of activity i n Find max-size subset A of compatible activities n Assume (wlog) that f 1  f 2  …  f n 1 2 3 4 5 6

11 David Luebke 11 10/24/2015 Activity Selection: Optimal Substructure l Let k be the minimum activity in A (i.e., the one with the earliest finish time). Then A - {k} is an optimal solution to S’ = {i  S: s i  f k } n In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity- selection over activities in S compatible with #1 n Proof: if we could find optimal solution B’ to S’ with |B| > |A - {k}|, u Then B  {k} is compatible u And |B  {k}| > |A|

12 David Luebke 12 10/24/2015 Activity Selection: Repeated Subproblems l Consider a recursive algorithm that tries all possible compatible subsets to find a maximal set, and notice repeated subproblems: S 1  A? S’ 2  A? S-{1} 2  A? S-{1,2}S’’S’-{2}S’’ yesno yes

13 David Luebke 13 10/24/2015 Greedy Choice Property l Dynamic programming? Memoize? Yes, but… l Activity selection problem also exhibits the greedy choice property: n Locally optimal choice  globally optimal sol’n n Them 17.1: if S is an activity selection problem sorted by finish time, then  optimal solution A  S such that {1}  A u Sketch of proof: if  optimal solution B that does not contain {1}, can always replace first activity in B with {1} (Why?). Same number of activities, thus optimal.

14 David Luebke 14 10/24/2015 Activity Selection: A Greedy Algorithm l So actual algorithm is simple: n Sort the activities by finish time n Schedule the first activity n Then schedule the next activity in sorted list which starts after previous activity finishes n Repeat until no more activities l Intuition is even more simple: n Always pick the shortest ride available at the time

15 David Luebke 15 10/24/2015 Minimum Spanning Tree Revisited l Recall: MST problem has optimal substructure n Prove it l Is Prim’s algorithm greedy? Why? l Is Kruskal’s algorithm greedy? Why?

16 David Luebke 16 10/24/2015 Greedy Vs. Dynamic Programming: The Knapsack Problem l The famous knapsack problem: n A thief breaks into a museum. Fabulous paintings, sculptures, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collector’s market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?

17 David Luebke 17 10/24/2015 The Knapsack Problem l More formally, the 0-1 knapsack problem: n The thief must choose among n items, where the ith item worth v i dollars and weighs w i pounds n Carrying at most W pounds, maximize value u Note: assume v i, w i, and W are all integers u “0-1” b/c each item must be taken or left in entirety l A variation, the fractional knapsack problem: n Thief can take fractions of items n Think of items in 0-1 problem as gold ingots, in fractional problem as buckets of gold dust

18 David Luebke 18 10/24/2015 The Knapsack Problem: Optimal Substructure l Both variations exhibit optimal substructure l To show this for the 0-1 problem, consider the most valuable load weighing at most W pounds n If we remove item j from the load, what do we know about the remaining load? n A: remainder must be the most valuable load weighing at most W - w j that thief could take from museum, excluding item j

19 David Luebke 19 10/24/2015 Solving The Knapsack Problem l The optimal solution to the fractional knapsack problem can be found with a greedy algorithm n How? l The optimal solution to the 0-1 problem cannot be found with the same greedy strategy n Greedy strategy: take in order of dollars/pound n Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds u Suppose item 2 is worth $100. Assign values to the other items so that the greedy strategy will fail

20 David Luebke 20 10/24/2015 The Knapsack Problem: Greedy Vs. Dynamic l The fractional problem can be solved greedily l The 0-1 problem cannot be solved with a greedy approach n It can, however, be solved with dynamic programming n Homework 5: give a dynamic programming solution to the 0-1 knapsack problem

21 David Luebke 21 10/24/2015 The End


Download ppt "David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued."

Similar presentations


Ads by Google