Introduction to Algorithms`

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Analysis of Algorithms CS 477/677
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Data Structures and Algorithms A. G. Malamos
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
HUFFMAN CODES.
CSC317 Greedy algorithms; Two main properties:
Algorithm Analysis Fall 2017 CS 4306/03
CSCE 411 Design and Analysis of Algorithms
Introduction to Algorithms
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Greedy Technique.
Introduction to Algorithms
Review: Dynamic Programming
The Greedy Method and Text Compression
Greedy Algorithms (Chap. 16)
The Greedy Method and Text Compression
Greedy Algorithm.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 16: Greedy algorithms Ming-Te Chi
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
Merge Sort Dynamic Programming
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structure and Algorithms
Chapter 16: Greedy algorithms Ming-Te Chi
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Lecture 2: Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Advance Algorithm Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Analysis of Algorithms CS 477/677
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Presentation transcript:

Introduction to Algorithms` Chapter 16: Greedy Algorithms

Overview Like dynamic programming, used to solve optimization problems. Problems exhibit optimal substructure (like DP). Problems also exhibit the greedy-choice property. When we have a choice to make, make the one that looks best right now. Make a locally optimal choice in hope of getting a globally optimal solution. Comp 122, Fall 2003

Greedy Strategy The choice that seems best at the moment is the one we go with. Prove that when there is a choice to make, 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. Comp 122, Fall 2003

Activity-selection Problem Input: Set S of n activities, a1, a2, …, an. si = start time of activity i. fi = finish time of activity i. Output: Subset A of maximum number of compatible activities. Two activities are compatible, if their intervals don’t overlap. Example: Activities in each line are compatible. Comp 122, Fall 2003

Optimal Substructure Assume activities are sorted by finishing times. f1  f2  …  fn. Suppose an optimal solution includes activity ak. This generates two subproblems. Selecting from a1, …, ak-1, activities compatible with one another, and that finish before ak starts (compatible with ak). Selecting from ak+1, …, an, activities compatible with one another, and that start after ak finishes. The solutions to the two subproblems must be optimal. Prove using the cut-and-paste approach. Comp 122, Fall 2003

Recursive Solution Let Sij = subset of activities in S that start after ai finishes and finish before aj starts. Subproblems: Selecting maximum number of mutually compatible activities from Sij. Let c[i, j] = size of maximum-size subset of mutually compatible activities in Sij. Recursive Solution: Comp 122, Fall 2003

Greedy-choice Property The problem also exhibits the greedy-choice property. There is an optimal solution to the subproblem Sij, that includes the activity with the smallest finish time in set Sij. Can be proved easily. Hence, there is an optimal solution to S that includes a1. Therefore, make this greedy choice without solving subproblems first and evaluating them. Solve the subproblem that ensues as a result of making this greedy choice. Combine the greedy choice and the solution to the subproblem. Comp 122, Fall 2003

Recursive Algorithm Recursive-Activity-Selector (s, f, i, j) m  i+1 while m < j and sm < fi do m  m+1 if m < j then return {am}  Recursive-Activity-Selector(s, f, m, j) else return  Initial Call: Recursive-Activity-Selector (s, f, 0, n+1) Complexity: (n) Straightforward to convert the algorithm to an iterative one. See the text. Comp 122, Fall 2003

Typical Steps Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve. Prove that there’s always an optimal solution that makes the greedy choice, so that the greedy choice is always safe. Show that greedy choice and optimal solution to subproblem  optimal solution to the problem. Make the greedy choice and solve top-down. May have to preprocess input to put it into greedy order. Example: Sorting activities by finish time. Comp 122, Fall 2003

Elements of Greedy Algorithms Greedy-choice Property. A globally optimal solution can be arrived at by making a locally optimal (greedy) choice. Optimal Substructure. Comp 122, Fall 2003

Minimum Spanning Trees Comp 122, Spring 2004

Carolina Challenge Comp 122, Fall 2003

Minimum Spanning Trees Given: Connected, undirected, weighted graph, G Find: Minimum - weight spanning tree, T Example: 7 b c 5 Acyclic subset of edges(E) that connects all vertices of G. a 1 3 -3 11 d e f 2 b c 5 a 1 3 -3 d e f Comp 122, Fall 2003

Generic Algorithm “Grows” a set A. A is subset of some MST. Edge is “safe” if it can be added to A without destroying this invariant. A := ; while A not complete tree do find a safe edge (u, v); A := A  {(u, v)} od Comp 122, Fall 2003

Definitions no edge in the set crosses the cut cut respects the edge set {(a, b), (b, c)} a light edge crossing cut (could be more than one) 5 7 a b c 1 -3 11 3 cut partitions vertices into disjoint sets, S and V – S. d e f 2 this edge crosses the cut one endpoint is in S and the other is in V – S. Comp 122, Fall 2003

Theorem 23.1 edge in A (x, y) crosses cut. Theorem 23.1: Let (S, V-S) be any cut that respects A, and let (u, v) be a light edge crossing (S, V-S). Then, (u, v) is safe for A. Proof: Let T be a MST that includes A. Case: (u, v) in T. We’re done. Case: (u, v) not in T. We have the following: edge in A (x, y) crosses cut. Let T´ = T - {(x, y)}  {(u, v)}. Because (u, v) is light for cut, w(u, v)  w(x, y). Thus, w(T´) = w(T) - w(x, y) + w(u, v)  w(T). Hence, T´ is also a MST. So, (u, v) is safe for A. x cut y u shows edges in T v Comp 122, Fall 2003

Corollary In general, A will consist of several connected components. Corollary: If (u, v) is a light edge connecting one CC in (V, A) to another CC in (V, A), then (u, v) is safe for A. Comp 122, Fall 2003

Kruskal’s Algorithm Starts with each vertex in its own component. Repeatedly merges two components into one by choosing a light edge that connects them (i.e., a light edge crossing the cut between them). Scans the set of edges in monotonically increasing order by weight. Uses a disjoint-set data structure to determine whether an edge connects vertices in different components. Comp 122, Fall 2003

Prim’s Algorithm Builds one tree, so A is always a tree. Starts from an arbitrary “root” r . At each step, adds a light edge crossing cut (VA, V - VA) to A. VA = vertices that A is incident on. Comp 122, Fall 2003

Prim’s Algorithm Uses a priority queue Q to find a light edge quickly. Each object in Q is a vertex in V - VA. Key of v is minimum weight of any edge (u, v), where u  VA. Then the vertex returned by Extract-Min is v such that there exists u  VA and (u, v) is light edge crossing (VA, V - VA). Key of v is  if v is not adjacent to any vertex in VA. Comp 122, Fall 2003

Prim’s Algorithm Q := V[G]; for each u  Q do key[u] :=  od; key[r] := 0; [r] := NIL; while Q   do u := Extract - Min(Q); for each v  Adj[u] do if v  Q  w(u, v) < key[v] then [v] := u; key[v] := w(u, v) fi od Complexity: Using binary heaps: O(E lg V). Initialization – O(V). Building initial queue – O(V). V Extract-Min’s – O(V lgV). E Decrease-Key’s – O(E lg V). Using Fibonacci heaps: O(E + V lg V). (see book)  decrease-key operation Note: A = {(v, [v]) : v  v - {r} - Q}. Comp 122, Fall 2003

Example of Prim’s Algorithm Not in tree 5 7 a/0 b/ c/ 1 Q = a b c d e f 0   -3 11 3 d/ e/ f/ 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/ 1 Q = b d c e f 5 11   -3 11 3 d/11 e/ f/ 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/7 1 Q = e c d f 3 7 11  -3 11 3 d/11 e/3 f/ 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/1 1 Q = d c f 0 1 2 -3 11 3 d/0 e/3 f/2 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/1 1 Q = c f 1 2 -3 11 3 d/0 e/3 f/2 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/1 1 Q = f -3 -3 11 3 d/0 e/3 f/-3 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 7 a/0 b/5 c/1 1 Q =  -3 11 3 d/0 e/3 f/-3 2 Comp 122, Fall 2003

Example of Prim’s Algorithm 5 a/0 b/5 c/1 1 3 -3 d/0 e/3 f/-3 Comp 122, Fall 2003

Greedy Algorithms Similar to dynamic programming, but simpler approach Also used for optimization problems Idea: When we have a choice to make, make the one that looks best right now Make a locally optimal choice in hope of getting a globally optimal solution Greedy algorithms don’t always yield an optimal solution Makes the choice that looks best at the moment in order to get optimal solution.

Fractional Knapsack Problem Knapsack capacity: W There are n items: the i-th item has value vi and weight wi Goal: find xi such that for all 0  xi  1, i = 1, 2, .., n  wixi  W and  xivi is maximum

Fractional Knapsack - Example E.g.: 50 20 --- 30 50 $80 + Item 3 30 20 Item 2 $100 + 20 Item 1 10 10 $60 $60 $100 $120 $240 $6/pound $5/pound $4/pound

Fractional Knapsack Problem Greedy strategy 1: Pick the item with the maximum value E.g.: W = 1 w1 = 100, v1 = 2 w2 = 1, v2 = 1 Taking from the item with the maximum value: Total value taken = v1/w1 = 2/100 Smaller than what the thief can take if choosing the other item Total value (choose item 2) = v2/w2 = 1

Fractional Knapsack Problem Greedy strategy 2: Pick the item with the maximum value per pound vi/wi If the supply of that element is exhausted and the thief can carry more: take as much as possible from the item with the next greatest value per pound It is good to order items based on their value per pound

Fractional Knapsack Problem Alg.: Fractional-Knapsack (W, v[n], w[n]) While w > 0 and as long as there are items remaining pick item with maximum vi/wi xi  min (1, w/wi) remove item i from list w  w – xiwi w – the amount of space remaining in the knapsack (w = W) Running time: (n) if items already ordered; else (nlgn)

Huffman Code Problem Huffman’s algorithm achieves data compression by finding the best variable length binary encoding scheme for the symbols that occur in the file to be compressed.

Huffman Code Problem The more frequent a symbol occurs, the shorter should be the Huffman binary word representing it. The Huffman code is a prefix-free code. No prefix of a code word is equal to another codeword.

Overview Huffman codes: compressing data (savings of 20% to 90%) Huffman’s greedy algorithm uses a table of the frequencies of occurrence of each character to build up an optimal way of representing each character as a binary string C: Alphabet

Example Assume we are given a data file that contains only 6 symbols, namely a, b, c, d, e, f With the following frequency table: Find a variable length prefix-free encoding scheme that compresses this data file as much as possible?

Huffman Code Problem Left tree represents a fixed length encoding scheme Right tree represents a Huffman encoding scheme

Example

Constructing A Huffman Code // C is a set of n characters // Q is implemented as a binary min-heap O(n) Total computation time = O(n lg n) O(lg n) O(lg n) O(lg n)

Cost of a Tree T For each character c in the alphabet C let f(c) be the frequency of c in the file let dT(c) be the depth of c in the tree It is also the length of the codeword. Why? Let B(T) be the number of bits required to encode the file (called the cost of T)

Huffman Code Problem In the pseudocode that follows: we assume that C is a set of n characters and that each character c €C is an object with a defined frequency f [c]. The algorithm builds the tree T corresponding to the optimal code A min-priority queue Q, is used to identify the two least-frequent objects to merge together. The result of the merger of two objects is a new object whose frequency is the sum of the frequencies of the two objects that were merged.

Running time of Huffman's algorithm The running time of Huffman's algorithm assumes that Q is implemented as a binary min-heap. For a set C of n characters, the initialization of Q in line 2 can be performed in O(n) time using the BUILD-MINHEAP The for loop in lines 3-8 is executed exactly n - 1 times, and since each heap operation requires time O(lg n), the loop contributes O(n lg n) to the running time. Thus, the total running time of HUFFMAN on a set of n characters is O(n lg n).

Prefix Code Prefix(-free) code: no codeword is also a prefix of some other codewords (Un-ambiguous) An optimal data compression achievable by a character code can always be achieved with a prefix code Simplify the encoding (compression) and decoding Encoding: abc  0 . 101. 100 = 0101100 Decoding: 001011101 = 0. 0. 101. 1101  aabe Use binary tree to represent prefix codes for easy decoding An optimal code is always represented by a full binary tree, in which every non-leaf node has two children |C| leaves and |C|-1 internal nodes Cost: Depth of c (length of the codeword) Frequency of c

Huffman Code Reduce size of data by 20%-90% in general If no characters occur more frequently than others, then no advantage over ASCII Encoding: Given the characters and their frequencies, perform the algorithm and generate a code. Write the characters using the code Decoding: Given the Huffman tree, figure out what each character is (possible because of prefix property)

Application on Huffman code Both the .mp3 and .jpg file formats use Huffman coding at one stage of the compression

Dynamic Programming vs. Greedy Algorithms We make a choice at each step The choice depends on solutions to subproblems Bottom up solution, from smaller to larger subproblems Greedy algorithm Make the greedy choice and THEN Solve the subproblem arising after the choice is made The choice we make may depend on previous choices, but not on solutions to subproblems Top down solution, problems decrease in size