430 likes | 622 Views
Greedy Algorithms. Greedy Algorithm. Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum is a part of the global optimum, we get the global optimum. Greedy Algorithm vs Dynamic Programming. Dynamic Programming. Greedy
E N D
Greedy Algorithm • Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. • If the local optimum is a part of the global optimum, we get the global optimum.
Greedy Algorithm vsDynamic Programming Dynamic Programming Greedy Algorithm
Knapsack Problem a thief’s knapsack of size W n items
Knapsack Problem • 0-1 knapsack problem - Each item must be either taken or left behind. • Fractional knapsack problem - The thief can take fractions of items.
Knapsack Problem 50 n = 4, W = 50 45 30 20 10 $135 $100 $120 $60 knapsack ($6/unit) ($5/unit) ($4/unit) ($3/unit)
Fractional Knapsack Problem $240 Greedy algorithm: greatest value per unit 50 20 30 45 30 20 50 20 10 $120 10 $135 $100 ($4/unit) $60 ($3/unit) ($5/unit) knapsack ($6/unit)
0-1 Knapsack Problem $220 $135 50 50 50 10 30 45 30 45 $60 $160 ($6/unit) 20 50 20 20 $120 ($4/unit) 10 $100 $135 value per unit value optimal ($5/unit) ($3/unit)
0-1 Knapsack Problem $220 $135 50 50 50 Difficult to get the optimal solution with a greedy strategy. 30 45 $160 20 50 Dynamic Programming : 20 10 value per unit value optimal
Optimal Substructure vsSubproblem Solution T1,5 T1,6 Ti,j : the solution of a subproblem 8 4 3 4 A subproblem solution? A local optimum? 1 2 5 7 T2,5 T2,6
Optimal Substructure vsSubproblem Solution • Subproblem solution - From all subproblem solutions • Optimal substructure - With only constant number of parameters - Without subproblems or future choices Usually Bottom-up Usually top-down
Huffman Codes • A lossless data compression algorithm. • It uses variable-length code
Variable-Length Code Six characters : a, b, c, d, e, f How can we represent them with binary strings?
Variable-Length Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? 0 0 1 0 = a a b a What 0010 menas? 0 01 0 = a d a 00 10 = c e …
Prefix Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? No codeword is a prefix of another codeword.
Prefix Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? No codeword is a prefix of another codeword.
Variable-Length Code Is it the optimal way?
Huffman Tree Fixed-length code 100 0 1 86 14 0 1 0 58 28 14 0 1 0 1 0 1 a: 45 c: 12 e: 9 b: 13 d: 16 f: 5
Huffman Tree Variable-length code 100 0 1 55 a: 45 0 1 25 30 0 1 0 1 d: 16 14 c: 12 b: 13 1 0 f: 5 e: 9
Huffman’s Algorithm Observation 1 The longest code : at least 2 characters 0 0 A Every non-leaf node has two children. B 0 B
Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters A F 1 0 1 0 B G C H 1 1 0 0 D E I J
Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 14 d: 16
Huffman’s Algorithm Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 1 0 14 d: 16 Total length in T = Total length in T ‘ + 16 * 1 + (9 + 5) * 2 1 0 e: 9 f: 5
Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 14 d: 16
Huffman’s Algorithm Merging two least frequent nodes. a: 45 b: 13 c: 12 d: 16 e: 9 f: 5
Huffman’s Algorithm Merging two least frequent nodes. 14 a: 45 b: 13 c: 12 d: 16 1 0 e: 9 f: 5
Huffman’s Algorithm Merging two least frequent nodes. 25 14 a: 45 d: 16 1 0 1 0 c: 12 b: 13 e: 9 f: 5
Huffman’s Algorithm Merging two least frequent nodes. 25 30 a: 45 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5
Huffman’s Algorithm Merging two least frequent nodes. 55 a: 45 0 1 25 30 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5
Huffman’s Algorithm Merging two least frequent nodes. 100 0 1 55 a: 45 0 1 25 30 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5
Greedy Algorithm • Optimization Algorithms : finds a proper local optimum. • Not as powerful as Dynamic Programming, but simpler. • Greedy algorithms - Knapsack Problem - Huffman Code
What is a graph? G = ( V, E ) 1 2 V = { 1, 2, 3, 4, 5 } E = { {1,2}, {1,3}, {2,3}, {2,4}, {2,5}, {3,4} } 5 3 4
Directed and Undirected Directed graph Undirected graph 1 2 1 2 5 5 3 4 3 4
Representations of Graphs Directed graph Adjacency-list 1 2 1 2 / 3 4 / 2 5 1 3 / 3 3 4 2 3 / 4 2 / 5
Representations of Graphs Directed graph Adjacency-Matrix 1 2 3 4 5 1 2 1 2 5 3 3 4 4 5
Representations of Graphs Adjacency-list Adjacency-Matrix
Adjacency Matrix of Graphs Directed graph Adjacency-Matrix 1 2 3 4 5 1 2 1 2 5 3 3 4 4 5
Adjacency Matrix of Graphs A = AT =
Weighted Graphs 1.7 1 2 -0.2 2.0 5 0.4 -2.1 3.6 3 4 -0.3 3.1