Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Algorithm A greedy algorithm always makes the choice that looks best at the moment Key point: Greed makes a locally optimal choice in the hope that.

Similar presentations


Presentation on theme: "Greedy Algorithm A greedy algorithm always makes the choice that looks best at the moment Key point: Greed makes a locally optimal choice in the hope that."— Presentation transcript:

1 Greedy Algorithm A greedy algorithm always makes the choice that looks best at the moment Key point: Greed makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution Note: Greedy algorithms do not always yield optimal solutions, but for SOME problems they do

2 Greed When do we use greedy algorithms? –When we need a heuristic (e.g., hard problems like the Traveling Salesman Problem) –When the problem itself is “greedy” Greedy Choice Property (CLRS 16.2) Optimal Substructure Property (shared with DP) (CLRS 16.2) Examples: –Minimum Spanning Tree (Kruskal’s algorithm) –Optimal Prefix Codes (Huffman’s algorithm)

3 Elements of the Greedy Algorithm Greedy-choice property: “A globally optimal solution can be arrived at by making a locally optimal (greedy) choice.” –Must prove that a greedy choice at each step yields a globally optimal solution Optimal substructure property: “A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. This property is a key ingredient of assessing the applicability of greedy algorithm and dynamic programming.”

4 Greedy Algorithm: Minimum Spanning Tree Kruskal’s minimum spanning tree algorithm –INPUT: edge-weighted graph G = (V,E), with |V| = n –OUTPUT: T a spanning tree of G (touches all vertices, and therefore has n-1 edges) of minimum cost (= total edge weight) –Will grow a set of edges T until it contains n-1 edges –Algorithm: Start with T empty, then iteratively add to T the smallest (minimum-cost) remaining edge in the graph if the edge does not form a cycle in T Claim: Greedy-MST is correct. Proof (by induction):  Define a promising edge set E’  E to be any edge set that is a subgraph of some MST. We will show by induction that as T is constructed, T is always promising.

5 Proof of Kruskal’s Algorithm  Basis: |T| = 0, trivial.  Induction Step: T is promising by I.H., so it is a subgraph of some MST, call it S. Let e i be the smallest edge in E, s.t. T  {e i } has no cycle, e i  T.  If e i  S, we’re done.  Suppose e i  S, then S’ = S  {e i } has a unique cycle containing e i, and all other arcs in cycle  e i (because S is an MST!)  Call the cycle C. Observe that C with e i cannot be in T, because T  {e i } is acyclic (because Kruskal adds e i )

6 Then C must contains some edge e j s.t. e j  S, and we also know c(e j )  c(e i ). Let S’ = S  {e i } \ {e j } S’ is an MST, so T  {e i } is promising ejej eiei Proof of Kruskal’s Algorithm

7 Greedy Algorithm: Huffman Codes Prefix codes –one code per input symbol –no code is a prefix of another Why prefix codes? –Easy decoding –Since no codeword is a prefix of any other, the codeword that begins an encoded file is unambiguous –Identify the initial codeword, translate it back to the original character, and repeat the decoding process on the remainder of the encoded file

8 Greedy Algorithm: Huffman Codes Huffman coding –Given: frequencies with which with which source symbols (e.g., A, B, C, …, Z) appear in a message –Goal is to minimize the expected encoded message length Create tree (leaf) node for each symbol that occurs with nonzero frequency –Node weights = frequencies Find two nodes with smallest frequency Create a new node with these two nodes as children, and with weight equal to the sum of the weights of the two children Continue until have a single tree

9 Greedy Algorithm: Huffman Codes Example: A E G I M N O R S T U V Y Blank 1 5 7 9 13 14 15 18 19 20 21 22 24 1 3 2 2 1 2 2 2 2 1 1 1 1 3 Frequency: 1.Place the elements into minimum heap (by frequency). 2.Remove the first two elements from the heap. 3.Combine these two elements into one. 4.Insert the new element back into the heap. Note: circle for node, rectangle for weight (= frequency)

10 Greedy Algorithm: Huffman Codes Step 1:Step 2: Step 3:Step 4:2AM2TU2AM 42 VY 2 AM2TU 2 TU 4 N42 VY 2 AM

11 Greedy Algorithm: Huffman Codes Step 5: Step 6: 2 TU4N42 VY 2 AM4OR 2 TU4N42 VY 2 AM4OR4SG

12 Greedy Algorithm: Huffman Codes Step 7 Step 8 2 TU4N42 VY 2 AM5IE4SG4OR 2 TU 4 N 5 IE 4 SG 4 OR 4 2 VY 2 AM7

13 Greedy Algorithm: Huffman Codes Step 9 5 IE 4 SG 9 4 2 VY 2 AM 7 2 TU 4 N 4 OR 815

14 Greedy Algorithm: Huffman Codes Finally: Note that the 0’s (left branches) and 1’s (right branches) give the code words for each symbol 5 IE 4 SG 9 4 2 VY 2 AM 7 2 TU 4 N 4 OR 8 152401 0 000 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 11 1

15 Proof That Huffman’s Merge is Optimal Let T be an optimal prefix-code tree in which a, b are siblings at deepest level, L(a) = L(b) Suppose that x, y are two other nodes that are merged by the Huffman algorithm –x, y have lowest weights because Huffman chose them –WLOG w(x)  w(a), w(y)  w(b); L(a) = L(b)  L(x), L(y) –Swap a and x: cost difference between T and new T’ is w(x)L(x) + w(a)L(a) – w(x)L(a) – w(a)L(x) = (w(a) – w(x))(L(a) – L(x)) // both factors non-neg  0 –Similar argument for b, y  Huffman choice also optimal T x ab y

16 Dynamic Programming Dynamic programming: Divide problem into overlapping subproblems; recursively solve each in the same way. Similar to DQ, so what’s the difference: –DQ partition the problem into independent subproblems. –DP breaking it into overlapping subproblems, that is, when subproblems share subproblems. –So DP saves work compared with DQ by solving every subproblems just once ( when subproblems are overlapping).

17 Elements of Dynamic Programming Optimal substructure: A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. Whenever a problem exhibits optimal substructure, it is a good clue that DP might apply.(a greedy method might apply also.) Overlapping subproblems: A recursive algorithm for the problem solves the same subproblems over and over, rather than always generating new subproblems.

18 Dynamic Programming: Matrix Chain Product Matrix-chain multiplication problem: Give a chain of n matrices  A 1, A 2, …, A n  to be multiplied, how to get the product A 1 A 2 … A n. with minimum number of scalar multiplications. Because of the associative law of matrix multiplication, there are many possible orderings to calculate the product for the same matrix chain: –Only one way to multiply A 1  A 2 –Best way for triple: Cost (A 1, A 2 ) + Cost((A 1 A 2 )  A 3 ) or Cost (A 2, A 3 ) + Cost(A 1 (A 2  A 3 )).

19 Dynamic Programming: Matrix Chain Product How do we build bottom-up? 1)From last example: Best way for triple: Cost (A 1, A 2 ) + Cost((A 1 A 2 )  A 3 ) or Cost (A 2, A 3 ) + Cost(A 1 (A 2  A 3 )). Save the best solutions for contiguous groups of A i. 2)Cost of ( i  j )( j  k) is ijk. E.g., 3 3 3 10 5 Each of 310 entries requires 5 multiplies (+ 4 adds)

20 Cost of final multiplication? A 1 A 2 A 3 … A k-1 A k … A n. Each of these subproblems can be solved optimally – just look in the table d 1  d k d k  d n+1 Dynamic Programming: Matrix Chain Product

21 FORMULATION: –Table entries a ij, 1  i  j  n, where a ij = optimal solution = min #multiplications for A i A i+1 … A j-1 A k We want a ij to fill the table. –Let dimensions be given by vector d i, 1  i  n+1, i.e., A i is d i  d i+1

22 Dynamic Programming: Matrix Chain Product Build Table: Diagonal S contains a ij with j - i = S. S = 0:a ij =0, i=1, 2, …, n S = 1:a i, i+1 = d i d i+1 d i+2, i=1, 2, …, n-1 1  S  n:a i, i+s = (a i, k + a k+1, i+s + d i d k d i+s ) Example: (Brassard/Bratley) 4 matrices, d = (13, 5, 89, 3, 34) S = 1: a 12 =5785 a 23 =1335 a 34 =9078

23 Dynamic Programming: Matrix Chain Product S = 2: a 13 = min ( a 11 + a 23 + 1353, a 12 + a 33 +13893) = 1530 a 24 = min ( a 22 + a 34 + 58934, a 23 + a 44 +5334) = 1845 S = 3: a 14 = min({k=1} a 11 + a 24 + 13534, {k=2} a 12 + a 34 + 138934, {k=3} a 13 + a 44 + 13334) = 2856 (note: max cost is 54201 multiplies) Complexity: S>0, choose among S choices for each of n-S elements in diagonal, so runtime is  (n 3 ). Proof:  i = 1 to n i(n-i) =  i = 1 to n (ni – i 2 ) = n(n(n+1)/2) – (n(n+1)(2n+1)/6) =  (n 3 )

24 Dynamic Programming: Longest Common Subsequence Longest Common Subsequence: Give two strings [a 1 a 2 … a m ] and [b 1 b 2 … b n ], what is the largest value P such that: For indices 1  i 1  i 2  …  i p  m, and 1  j 1  j 2  …  j p  n, We have a ix = b jx, for 1  x  P Example: So P = 4, i = {1, 2, 3, 5}, j = {3, 4, 5, 6} b a a b a c b a c b a a a

25 Dynamic Programming: Longest Common Subsequence Let L(k, l) denote length of LCS for [a 1 a 2 … a k ] and [b 1 b 2 … b l ]. Then we have facts: L(p, q)  L(p-1, q-1). 1)L(p, q) = L(p-1, q-1) + 1if a p = b q when a p and b q are both in LCS. 2)L(p, q)  L(p-1, q) when a p is not in LCS. 3)L(p, q)  L(p, q-1) when b q is not in LCS.

26 Dynamic Programming: Longest Common Subsequence ALGORITHM: for i = 1 to m for j = 1 to n if a i = b j then L(i, j) = L(i-1, j-1) + 1 else L(i, j) = max{L(i, j-1), L(i-1, j)} Time complexity:  (n 2 ).

27 Dynamic Programming: Knapsack The problem: The knapsack problem is a particular type of integer program with just one constraint: Each item that can go into the knapsack has a size and a benefit. The knapsack has a certain capacity. What should go into the knapsack so as to maximize the total benefit? Hint: Recall shortest path method. Define F k (y) = max (0  k  n) with (0  y  b) Then, what is F k (y)? Max value possible using only first k items when weight limit is y.

28 Dynamic Programming: Knapsack B.C.’s:1. F 0 (y) = 0  y  no items chosen 2. F k (0) = 0  k  weight limit = 0 3. F 1 (y) =  y/w 1  v 1 Generally speaking: F k (y) = max{F k-1 (y), F k (y-w k )+v k } Then we could build matrix: use entries above, here is an example: Kth item not used Kth item used once

29 Dynamic Programming: Knapsack Example:k=4, b=10, y=#pounds, k=#item types allowed v 1 =1 w 1 =2; v 2 =3 w 2 =3; v 3 =5 w 3 =4;v 4 =9 w 4 =7 F k (y) = max{F k-1 (y), F k (y-w k )+v k } F k (y) n  b table: YKYK 1 2345678910 10112233445 20133466799 30135568 11 4013556910 12

30 Dynamic Programming: Knapsack Note: 12 = max(11, 9+ F 4 (3))=max(11,9+3)=12 What is missing here? (Like in SP, we know the SP’s cost, but we don’t know SP itself…) So, we need another table? i(k,j) = max index such that item type j is used in F k (y), i.e., i(k,y)=j  x j  1; x q =0  q>j B.C.’s:i(1,y) =0 if F 1 (y) = 0 i(1,y) =0 if F 1 (y)  0 General:

31 Dynamic Programming: Knapsack Trace Back: if i(k,y) =q, use item q once, check i(k,y-q). Example: E.g. F 4 (10) = 12. i(4,10)=4  4 th item used once YKYK 1 2345678910 10111111111 20122222222 30123333333 40123334344

32 Dynamic Programming: Knapsack i(4, 10 - w 4 ) =i(4,3)=2  2 nd item used once i(4, 3 – w 2 ) =i(4,0)=0  done Notice i(4,8)=3  don’t use most valuable item.


Download ppt "Greedy Algorithm A greedy algorithm always makes the choice that looks best at the moment Key point: Greed makes a locally optimal choice in the hope that."

Similar presentations


Ads by Google