1 / 17

Greedy Algorithms

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,

bergen
Télécharger la présentation

Greedy Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. Fractional Knapsack Problem • Given n items of sizes s1, s2, …, sn and values v1, v2, …, vn and size C, the problem is to find x1, x2, …, xn that maximize subject to

  4. Solution to Fractional Knapsack Problem • Consider yi= vi / si • What is yi? • What is the solution?

  5. Activity Selection Problem • Problem Formulation • Given a set of n activities, S = {a1, a2, ..., an}thatrequire 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 aineeds the resource during period [si, fi), which is a half-open interval, where • si = start time of the activity, and • fi = finish time of the activity. • Note: Could have many other objectives: • Schedule room for longest time. • Maximize income rental fees.

  6. 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.

  7. Solving the Activity Selection Problem • Define Si,j= {akS : fi sk< fksj} • activities that start after aifinishes and finish before ajstarts • Activities in Si,jare compatible with: • Add the following [fictitious] activities a0 = [– , 0) and an+1 = [ , +1) • Hence, S = S0,n+1 and the range of Si,j is 0 i,jn+1

  8. Solving the Activity Selection Problem • Assume that activities are sorted by monotonically increasing finish time: • i.e., f0 f1 f2 ... fn <fn+1 • Then, Si,j =  for i  j. Proof: • Therefore, we only need to worry about Si,j where 0 i < jn+1

  9. Solving the Activity Selection Problem • Suppose that a solution to Si,jincludes ak. We have 2 sub-problems: • Si,k(start after aifinishes, finish before akstarts) • Sk,j(start after akfinishes, finish before ajstarts) • The Solution to Si,jis (solution to Si,k)  {ak}  (solution to Sk,j) • Since akis in neither sub-problem, and the subproblems are disjoint, |solution to S| = |solution to Si,k|+1+|solution to Sk,j|

  10. Recursive Solution to Activity Selection Problem • Let Ai,j= optimal solution to Si,j. So Ai,j= Ai,k {ak} Ak,j, assuming: • Si,jis nonempty, and • we know ak. • Hence,

  11. Finding the Greedy Algorithm • Theorem: Let Si,j , and let ambe the activity in Si,jwith the earliest finish time: fm=min { fk: akSi,j}. Then: • amis used in some maximum-size subset of mutually compatible activities of Si,j • Sim= , so that choosing amleaves Sm,jas the only nonempty subproblem.

  12. Recursive Greedy Algorithm

  13. Iterative Greedy Algorithm

  14. Greedy Strategy • Determine the optimal substructure. • Develop a recursive solution. • 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. • Show that all but one of the subproblems resulting from the greedy choice are empty. • Develop a recursive greedy algorithm. • Convert it to an iterative algorithm.

  15. Money Change Problem • Given a currency system that has n coins with values v1, v2 , ..., vn, where v1 = 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, x1, x2 , ..., xn, are nonnegative integers (so ximay be zero).

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

More Related