We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11
Chapter # 5
Lecture # 1
Greedy Technique
1. Definition of Greedy Technique.
2. Applications of greedy technique:
04:04 Dr. Hazem M. Bahig
Optimization problems • An optimization problem: – Given a problem instance, a set of constraints and an objective function. – Find a feasible solution for the given instance for which the objective function has an optimal value either maximum or minimum depending on the problem being solved. • A feasible solution satisfies the problem’s constraints • The constraints specify the limitations on the required solutions. • An optimization problem is one in which you want to find, not just a solution, but the best solution
04:04 Dr. Hazem M. Bahig
An Optimization problem is one in which, we are given a set of input values, which are required to be either maximized or minimized w. r. t. some constraints or conditions.
# In optimization problems, we have many possible
solutions. # Each solution has a value associated with it. # We want a solution with the optimal (min or max) value. # We say “a solution,” not “the solution,” because there may be more than 1 solution with the same, best value.
04:04 Dr. Hazem M. Bahig
Greedy Algorithms
The solution is constructed through a sequence of steps.
At each step the choice must be locally optimal.
04:04 Dr. Hazem M. Bahig
Greedy Algorithms In general, greedy algorithms have five components: 1.A candidate set, from which a solution is created , 2.A selection function, which chooses the best candidate to be added to the solution, 3.A feasibility function, that is used to determine if a candidate can be used to contribute to a solution, 4.An objective function, which assigns a value to a solution, or a partial solution, and 5.A solution function, which will indicate when we have discovered a complete solution
In some cases such a strategy is guaranteed to offer optimal
solutions, and in some other cases it may provide a compromise that produces acceptable approximations. 04:04 Dr. Hazem M. Bahig Pseudo-code for Greedy Algorithms
04:04 Dr. Hazem M. Bahig
Pseudo-code for Greedy Algorithms
chooses a candidate based on a local
selection criteria, removes it from candidate, and returns its value. checks whether adding the selected value to the current solution can result in a feasible solution (no constraints are violated).
checks whether the
problem is solved.
04:04 Dr. Hazem M. Bahig
Characteristics of greedy algorithm 1.Used to solve optimization problem. 2. Always makes the choice that looks best at the moment. That is, it makes a locally optimal choice in the hope that this choice will lead to a overall globally optimal solution. 5. Once any choice of input from C is rejected then it never considered again. 6. Do not always yield an optimal solution.
04:04 Dr. Hazem M. Bahig
Example: Counting money • Suppose you want to count out a certain amount of money, using the fewest possible bills and coins • A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot • Set of coins C={1, 5, 10}, and amount of money n=15. Coins: 10, 5 optimal (two coins)
• Set of coins C={1, 7, 10}, and amount of money n=15.
Coins: 10, 1, 1, 1, 1, 1 not optimal . Number of coins is 6 we can construct another sequence of coins with length 3: 7, 7 ,1
• Set of coins C={5, 7, 10}, and amount of money n=13.
Coins: 10 no solution
04:04 Dr. Hazem M. Bahig
– Example: To make $6.39, you can choose: • a $5 bill • a $1 bill, to make $6 • a 25¢ coin, to make $6.25 • A 10¢ coin, to make $6.35 • four 1¢ coins, to make $6.39 • For US money, the greedy algorithm always gives the optimum solution.
04:04 Dr. Hazem M. Bahig
A failure of the greedy algorithm • In some (fictional) monetary system, “krons” come in 1 kron, 7 kron, and 10 kron coins • Using a greedy algorithm to count out 15 krons, you would get – A 10 kron piece – Five 1 kron pieces, for a total of 15 krons – This requires six coins • A better solution would be to use two 7 kron pieces and one 1 kron piece – This only requires three coins • The greedy algorithm results in a solution, but not in an optimal solution