 # Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.

## Presentation on theme: "Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1."— Presentation transcript:

Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1

Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming algorithms, –greedy algorithms are iterative in nature. –An optimal solution is reached from local optimal solutions. –This approach does not work all the time. –A proof that the algorithm does what it claims is needed, and usually not easy to get.

Fractional Knapsack Problem Given n items of sizes s 1, s 2, …, s n and values v 1, v 2, …, v n and size C, the problem is to find x 1, x 2, …, x n  that maximize subject to

Solution to Fractional Knapsack Problem Consider y i = v i / s i What is y i ? What is the solution?

Activity Selection Problem Problem Formulation –Given a set of n activities, S = {a 1, a 2,..., a n } that require exclusive use of a common resource, find the largest possible set of nonoverlapping activities (also called mutually compatible). For example, scheduling the use of a classroom. –Assume that a i needs the resource during period [s i, f i ), which is a half-open interval, where s i = start time of the activity, and f i = finish time of the activity. Note: Could have many other objectives: –Schedule room for longest time. –Maximize income rental fees.

Activity Selection Problem: Example Assume the following set S of activities that are sorted by their finish time, find a maximum-size mutually compatible set. i123456789 sisi 12415891113 fifi 3578910111416

i123456789 sisi 12415891113 fifi 3578910111416

Solving the Activity Selection Problem Define S i,j = {a k  S : f i  s k < f k  s j } –activities that start after a i finishes and finish before a j starts Activities in S i,j are compatible with: – Add the following [fictitious] activities a 0 = [– , 0) and a n+1 = [ ,  +1) Hence, S = S 0,n+1 and the range of S i,j is 0  i,j  n+1

Solving the Activity Selection Problem Assume that activities are sorted by monotonically increasing finish time: –i.e., f 0  f 1  f 2 ...  f n < f n+1 Then, S i,j =  for i  j. Proof: Therefore, we only need to worry about S i,j where 0  i < j  n+1

Solving the Activity Selection Problem Suppose that a solution to S i,j includes a k. We have 2 sub-problems: –S i,k (start after a i finishes, finish before a k starts) –S k,j (start after a k finishes, finish before a j starts) The Solution to S i,j is (solution to S i,k )  {a k }  (solution to S k,j ) Since a k is in neither sub-problem, and the subproblems are disjoint, |solution to S| = |solution to S i,k |+1+|solution to S k,j |

Recursive Solution to Activity Selection Problem Let A i,j = optimal solution to S i,j. So A i,j = A i,k  {a k }  A k,j, assuming: –S i,j is nonempty, and –we know a k. Hence,

Finding the Greedy Algorithm Theorem: Let S i,j  , and let a m be the activity in S i,j with the earliest finish time: f m = min { f k : a k  S i,j }. Then: 1.a m is used in some maximum-size subset of mutually compatible activities of S i,j 2.S im = , so that choosing a m leaves S m,j as the only nonempty subproblem.

Recursive Greedy Algorithm

Iterative Greedy Algorithm

Greedy Strategy 1.Determine the optimal substructure. 2.Develop a recursive solution. 3.Prove that at any stage of recursion, one of the optimal choices is the greedy choice. Therefore, it's always safe to make the greedy choice. 4.Show that all but one of the subproblems resulting from the greedy choice are empty. 5.Develop a recursive greedy algorithm. 6.Convert it to an iterative algorithm.

Money Change Problem Given a currency system that has n coins with values v 1, v 2,..., v n, where v 1 = 1, the objective is to pay change of value y in such a way that the total number of coins is minimized. More formally, we want to minimize the quantity subject to the constraint Here, x 1, x 2,..., x n, are nonnegative integers (so x i may be zero).

Money Change Problem What is a greedy algorithm to solve this problem? Is the greedy algorithm optimal?

Download ppt "Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1."

Similar presentations