ebook img

Algorithms: Greedy Algorithms PDF

73 Pages·2012·0.6 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Algorithms: Greedy Algorithms

Algorithms: Greedy Algorithms Amotz Bar-Noy CUNY Spring 2012 AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 1/62 Greedy Algorithms Greedy algorithms make decisions that “seem” to be the best following some greedy criteria. In Off-Line problems: Thewholeinputisknowninadvance. Possibletodosomepreprocessingoftheinput. Decisionsareirrevocable. In Real-Time and On-Line problems: Thepresentcannotchangethepast. Thepresentcannotrelyontheun-knownfuture. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 2/62 How and When to use Greedy Algorithms? Initial solution: Establish trivial solutions for a problem of a small size. Usually n = 0 or n = 1. Topbottomprocedure: Foraproblemofsizen,lookforagreedy decision that reduces the size of the problem to some k < n and then, apply recursion. Bottom up procedure: Construct the solution for a problem of size n based on some greedy criteria applied on the solutions to the problems of size k = 1,...,n−1. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 3/62 The Coin Changing Problem Input: Integercoindenominationsd >···>d >d =1. n 2 1 Anintegeramounttopay: A. Output: Number of coins n for each denomination d to get the i i exact amount. A=n d +n d +n d +n d . n n n−1 n−1 2 2 1 1 Goal: Minimize total number of coins. N =n +···+n +n . n 2 1 Remark: There is always a solution with N = A since d = 1. 1 AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 4/62 Examples USA: d = 100, d = 50, d = 25, d = 10, d = 5, d = 1. 6 5 4 3 2 1 A=73=2·25+2·10+3·1. N =2+2+3=7. Old British: d = 240, d = 20, d = 1. 3 2 1 A=307=1·240+3·20+7·1. N =1+3+7=11. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 5/62 Greedy Solution Idea: Use the largest possible denomination and update A. Implementation: Coin-Changing(d > ··· > d > d = 1) n 2 1 for i = n downto 1 n = (cid:98)A/d(cid:99) i i A = A mod d = A−nd i i i Return(N = n +···+n +n ) n 2 1 Correctness: A = n d +n d +n d +n d . n n n−1 n−1 2 2 1 1 Complexity: Θ(n) division and mod integer operations. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 6/62 A coin system for which Greedy is not optimal: d =4,d =3,d =1andA=6: 3 2 1 Greedy: 6=1·4+2·1 ⇒ N =3. Optimal: 6=2·3 ⇒ N =2. A coin system for which Greedy is very “bad”: d =x +1,d =x,d =1andA=2x: 3 2 1 Greedy: 2x =1·(x +1)+(x −1)·1 ⇒ N =x. Optimal: 2x =2·x ⇒ N =2. Optimality Greedy is optimal for the USA system. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 7/62 A coin system for which Greedy is very “bad”: d =x +1,d =x,d =1andA=2x: 3 2 1 Greedy: 2x =1·(x +1)+(x −1)·1 ⇒ N =x. Optimal: 2x =2·x ⇒ N =2. Optimality Greedy is optimal for the USA system. A coin system for which Greedy is not optimal: d =4,d =3,d =1andA=6: 3 2 1 Greedy: 6=1·4+2·1 ⇒ N =3. Optimal: 6=2·3 ⇒ N =2. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 7/62 Optimality Greedy is optimal for the USA system. A coin system for which Greedy is not optimal: d =4,d =3,d =1andA=6: 3 2 1 Greedy: 6=1·4+2·1 ⇒ N =3. Optimal: 6=2·3 ⇒ N =2. A coin system for which Greedy is very “bad”: d =x +1,d =x,d =1andA=2x: 3 2 1 Greedy: 2x =1·(x +1)+(x −1)·1 ⇒ N =x. Optimal: 2x =2·x ⇒ N =2. AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 7/62 Efficiency Optimal solution: Check all possible combinations. Notapolynomialtimealgorithm. Another optimal solution: Polynomial in both n and A. Notastronglypolynomialtimealgorithm. Objective: Findasolutionthatispolynomialonlyinn. Probablyimpossible!? AmotzBar-Noy (CUNY) GreedyAlgorithms Spring2012 8/62

Description:
Algorithms: Greedy Algorithms Amotz Bar-Noy CUNY Spring 2012 Amotz Bar-Noy (CUNY) Greedy Algorithms Spring 2012 1 / 62
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.