Greedy Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Greedy Algorithms Amihood Amir Bar-Ilan University.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Greed is good. (Some of the time)
Analysis of Algorithms
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 5. Greedy Algorithms - 1 Greedy.
Data Structures – LECTURE 10 Huffman coding
Greedy Algorithms Huffman Coding
Lecture 7: Greedy Algorithms II
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
1 Analysis of Algorithms Chapter - 08 Data Compression.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 5: Advanced Design Techniques.
Data Structures and Algorithms A. G. Malamos
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
GREEDY ALGORITHMS UNIT IV. TOPICS TO BE COVERED Fractional Knapsack problem Huffman Coding Single source shortest paths Minimum Spanning Trees Task Scheduling.
Greedy Algorithms CSc 4520/6520 Fall 2013 Problems Considered Activity Selection Problem Knapsack Problem – 0 – 1 Knapsack – Fractional Knapsack Huffman.
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
Bahareh Sarrafzadeh 6111 Fall 2009
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
Greedy Algorithms Chapter 16 Highlights
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
Greedy Algorithms Analysis of Algorithms.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
CSCI 58000, Algorithm Design, Analysis & Implementation Lecture 12 Greedy Algorithms (Chapter 16)
HUFFMAN CODES.
Greedy Algorithms Alexandra Stefan.
CSC317 Greedy algorithms; Two main properties:
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
The Greedy Method and Text Compression
Introduction to Algorithms`
Greedy Algorithm.
Chapter 16: Greedy Algorithm
Analysis & Design of Algorithms (CSCE 321)
Huffman Coding.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Advanced Algorithms Analysis and Design
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Chapter 16: Greedy algorithms Ming-Te Chi
Advanced Algorithms Analysis and Design
Merge Sort Dynamic Programming
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Chapter 16: Greedy algorithms Ming-Te Chi
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Lecture 2: Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Advance Algorithm Dynamic Programming
Huffman codes Binary character code: each character is represented by a unique binary string. A data file can be coded in two ways: a b c d e f frequency(%)
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Analysis of Algorithms CS 477/677
Presentation transcript:

Greedy Algorithms

Concept Greedy algorithms are also used solve optimization problems. Greedy approach suggests constructing a solution through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached.

Concept… On each step –and this is central point of this technique-the choice made must be -feasible, i.e., it has to satisfy the problem’s constraints Locally optimal, i.e., it has to be the best local choice among all feasible choices available on that step Irrevocable, i.e., once made, it cannot be changed on subsequent steps of the algorithm

Greedy Approach Vs Dynamic programming Greedy Approach works in top down manner while Dynamic programming works in bottom up manner Greedy Approach makes a choice first then evaluate it while Dynamic programming evaluate all options then make choice. Greedy Approach does not provide guarantee for optimal solution while Dynamic programming does.

An Activity Selection Problem This is the problem of scheduling several competing activities that require exclusive use of a common resource, with a goal of selecting a maximum-size set of mutually compatible activities. Suppose we have a set S = {a1, a2, . . . , an} of n proposed activities that wish to use a resource, such as a lecture hall, which can be used by only one activity at a time. Each activity ai has a start time si and a finish time fi, where 0 ≤ si < fi < ∞.

An Activity Selection Problem

An Activity Selection Problem…

The optimal substructure of the activity-selection problem

The optimal substructure of the activity-selection problem…

The optimal substructure of the activity-selection problem…

The optimal substructure of the activity-selection problem…

The optimal substructure of the activity-selection problem…

A recursive solution Let c[i, j ] be the number of activities in a maximum-size subset of mutually compatible activities in Si j. We have c[i, j ] = 0 whenever Si j = ∅; in particular, c[i, j ] = 0 for i ≥ j. if ak is used in a maximum- size subset of mutually compatible activities of Si j, we have recurrence. c[i, j ] = c[i, k] + c[k, j ] + 1 .

A recursive solution…

Converting a dynamic-programming solution to a greedy solution

A recursive greedy algorithm We assume that the n input activities are ordered by monotonically increasing finish time, according to equation. If not, we can sort them into this order in O(n lg n) time, breaking ties arbitrarily. The initial call is RECURSIVE-ACTIVITY- SELECTOR(s, f, 0, n).

Recursive Greedy Algorithm Example

An iterative greedy algorithm The procedure GREEDY-ACTIVITY-SELECTOR is an iterative version of the procedure RECURSIVE-ACTIVITY-SELECTOR. It also assumes that the input activities are ordered by monotonically increasing finish time. It collects selected activities into a set A and returns this set when it is done.

Greedy versus dynamic programming Because the optimal-substructure property is exploited by both the greedy and dynamic- programming strategies. It is hard to decide which technique we should use to solve the problem. To illustrate above, let us investigate two variants of a classical optimization problem. The 0-1 knapsack problem & fractional knapsack problem

fractional knapsack problem The setup is the same, but the thief can take fractions of items, rather than having to make a binary (0-1) choice for each item. You can think of an item in the 0-1 knapsack problem as being like a gold slab, while an item in the fractional knapsack problem is more like gold dust. Both knapsack problems exhibit the optimal- substructure property.

fractional knapsack problem Although the problems are similar, the fractional knapsack problem is solvable by a greedy strategy, whereas the 0-1 problem is not. To solve the fractional problem, we first compute the value per pound vi/wi for each item. Obeying a greedy strategy, the thief begins by taking as much as possible of the item with the greatest value per pound. If the supply of that item is exhausted and he can still carry more, he takes as much as possible of the item with the next greatest value per pound,and so forth until he can’t carry any more. Thus, by sorting the items by value per pound, the greedy algorithm runs in O(nlg n) time.

Difference between 0-1 and fractional Knapsack

Solution to the Fractional Knapsack Problem In this version of a problem the items can be broken into smaller piece, so the thief may decide to carry only a fraction xi of object i, where 0 ≤ xi ≤ 1. Item i contributes xiwi to the total weight in the knapsack, and xivi to the value of the load. In Symbol, the fraction knapsack problem can be stated as follows. maximize nSi=1 xivi subject to constraint nSi=1 xiwi ≤ W It is clear that an optimal solution must fill the knapsack exactly,. Thus in an optimal solution nSi=1 xiwi = W.

Greedy-fractional-knapsack (w, v, W) FOR i =1 to n     do x[i] =0 weight = 0 while weight < W     do i = best remaining item         IF weight + w[i] ≤ W             then x[i] = 1                 weight = weight + w[i]             else                 x[i] = (w - weight) / w[i]                 weight = W return x

Huffman codes Huffman codes are a widely used and very effective technique for compressing data. We consider the data to be a sequence of characters. Huffman’s greedy algorithm uses a table of the frequencies of occurrence of the characters to build up an optimal way of representing each character as a binary string.

Suppose we have a 100,000-character data file that we wish to store compactly. We observe that the characters in the file occur with the frequencies given by following figure 1(next page). That is, only six different characters appear, and the character a occurs 45,000 times.

Figure 1 a b c d e f Frequency (in thousands) 45 13 12 16 9 5 Fixed- length codeword 000 001 010 011 100 101 Variable- length codeword 111 1101 1100 Figure 1

There are many ways to represent such a file of information. We consider the problem of designing a binary character code (or code for short) wherein each character is represented by a unique binary string. If we use a fixed-length code, we need 3 bits to represent six characters: a = 000, b = 001, . . . , f = 101. This method requires 300,000 bits to code the entire file. Can we do better? A variable-length code can do considerably better than a fixed- length code, by giving frequent characters short codewords and infrequent characters long codewords. Above figure 1 shows such a code; here the 1-bit string 0 represents a, and the 4-bit string 1100 represents f. This code requires (45 · 1 + 13 · 3 + 12 · 3 + 16 · 3 + 9 · 4 + 5 · 4) · 1,000 = 224,000 bits to represent the file, a savings of approximately 25%. In fact, this is an optimal character code for this file.

Prefix codes We consider here only codes in which no codeword is also a prefix of some other codeword. Such codes are called prefix codes. Encoding is always simple for any binary character code; we just concatenate the codewords representing each character of the file. For example, with the variable length prefix code of Figure 1, we code the 3-character file abc as 0·101·100 = 0101100, where we use “·” to denote concatenation.

Prefix codes Prefix codes are desirable because they simplify decoding. Since no codeword is a prefix of any other, the codeword that begins an encoded file is unambiguous. We can simply identify the initial codeword, translate it back to the original character, and repeat the decoding process on the remainder of the encoded file. In our example, the string 001011101 parses uniquely as 0 · 0 · 101 · 1101, which decodes to aabe.

Prefix codes The decoding process needs a convenient representation for the prefix code so that the initial codeword can be easily picked off. A binary tree whose leaves are the given characters provides one such representation. We interpret the binary codeword for a character as the path from the root to that character, where 0 means “go to the left child” and 1 means “go to the right child.” Figure on next slide shows the trees for the two codes of our example. Note that these are not binary search trees, since the leaves need not appear in sorted order and internal nodes do not contain character keys.

Prefix codes

Prefix codes An optimal code for a file is always represented by a full binary tree, in which every nonleaf node has two children . The fixed-length code in our example is not optimal since its tree, shown in Figure 2(a), is not a full binary tree

Constructing a Huffman code Huffman invented a greedy algorithm that constructs an optimal prefix code called a Huffman code. In the following algorithm, 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 in a bottom-up manner. It begins with a set of |C| leaves and performs a sequence of |C| − 1 “merging” operations to create the final tree. A min-priority queue Q, keyed on f , 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.

Constructing a Huffman code 2 Q ← C 3 for i ← 1 to n − 1 4 do allocate a new node z 5 left[z]← x ← EXTRACT-MIN(Q) 6 right[z]← y ← EXTRACT-MIN(Q) 7 f [z]← f [x] + f [y] 8 INSERT(Q, z) 9 return EXTRACT-MIN(Q) (Return the root of the tree.)

Constructing a Huffman code Analysis of the running time of Huffman’s algorithm It 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 procedure. 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).

Constructing a Huffman code

Constructing a Huffman code

A task-scheduling problem This is the problem of optimally scheduling unit-time tasks on a single processor, where each task has a deadline, along with a penalty that must be paid if the deadline is missed. A unit-time task is a job, such as a program to be run on a computer, that requires exactly one unit of time to complete. Given a finite set S of unit-time tasks, A schedule for S is a permutation of S specifying the order in which these tasks are to be performed. The first task in the schedule begins at time 0 and finishes at time 1, the second task begins at time 1 and finishes at time 2, and so on.

A task-scheduling problem The problem of scheduling unit-time tasks with deadlines and penalties for a single processor has the following inputs: • a set S = {a1, a2, . . . , an} of n unit-time tasks; • a set of n integer deadlines d1, d2, . . . , dn, such that each di satisfies 1 ≤ di ≤ n and task ai is supposed to finish by time di; and • a set of n nonnegative weights or penalties w1,w2, . . . , wn, such that we incur a penalty of wi if task ai is not finished by time di and we incur no penalty if a task finishes by its deadline. We are asked to find a schedule for S that minimizes the total penalty incurred for missed deadlines.

A task-scheduling problem Algorithm: 1. Arrange all jobs in decreasing order of penalties in array J. Take an array A of size of maximum deadline. Start from right end of the array A, search for the job which contain deadline ≥ di in array J. For every slot i.

A task-scheduling problem Algorithm: 1. Array all jobs in decreasing order of penalties in array J (use merge sort (O(nlgn) time)). Take an array A of size of maximum deadline. Start from right end of the array A, search for the job which contain deadline ≥ di in array J. For every slot i (using linear search (O(n2)) ). Total time O(nlgn+n2)=O(n2)

A task-scheduling problem: Example

THETOPPERSWAY.COM