Chapter 16: Greedy algorithms Ming-Te Chi

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Greedy Algorithms.
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
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.
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.
Chapter 9: Huffman Codes
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
Data Structures and Algorithms A. G. Malamos
Introduction to Algorithms Chapter 16: Greedy Algorithms.
CSC5101 Advanced Algorithms Analysis
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
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
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization.
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
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.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CSCI 58000, Algorithm Design, Analysis & Implementation Lecture 12 Greedy Algorithms (Chapter 16)
Greedy Algorithms General principle of greedy algorithm
Greedy algorithms: CSC317
HUFFMAN CODES.
Greedy Algorithms Alexandra Stefan.
CSC317 Greedy algorithms; Two main properties:
Introduction to Algorithms
The Greedy Method and Text Compression
Analysis of Algorithms CS 477/677
Greedy Algorithms (Chap. 16)
Proving the Correctness of Huffman’s Algorithm
Introduction to Algorithms`
Greedy Algorithm.
Chapter 16: Greedy Algorithm
Chapter 9: Huffman Codes
Chapter 16: Greedy Algorithms
ICS 353: Design and Analysis of Algorithms
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
CS6045: Advanced Algorithms
Algorithms (2IL15) – Lecture 2
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
Lecture 8 Greedy Approach
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Advanced Algorithms Analysis and Design
Lecture 6 Topics Greedy Algorithm
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
ICS 353: Design and Analysis of Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Greedy algorithms.
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Asst. Prof. Dr. İlker Kocabaş
Huffman Coding Greedy Algorithm
Advance Algorithm Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Proving the Correctness of Huffman’s Algorithm
Analysis of Algorithms CS 477/677
Presentation transcript:

Chapter 16: Greedy algorithms Ming-Te Chi

Introduction 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. Ch16: Greedy algorithms

照片來源:政大圖書館茶言觀政網站 Ch16: Greedy algorithms

16.1 An activity-selection problem Ch16: Greedy algorithms

An activity-selection problem Suppose we have a set S = {a1, a2, ..., an} of n proposed activities that with to use a resource. Each activity ai has a start time si and a finish time fi, where 0  si < fi < . If selected, activity ai take place during the half-open time interval [si, fi). Activities ai and aj are compatible if the intervals [si, fi) and [sj, fj) do not overlap (i.e., ai and aj are compatible if si  fj or sj  fi). Ch16: Greedy algorithms

The activity-selection problem is to select a maximum-size subset of mutually compatible activities. Example: i 1 2 3 4 5 6 7 8 9 10 11 si 12 fi 13 14 Ch16: Greedy algorithms

We start by formulating a dynamic programming solution to this program in which we combine optimal solutions to two subproblems to form an optimal solution to the original problem. We shall then observe that we need only consider one choice – the greedy choice – and that when we make the greedy choice, one of the subproblems is guaranteed to be empty, so that only one nonempty subproblem remains. Ch16: Greedy algorithms

The optimal substructure of the activity-selection problem Sij={akS : fi  sk < fk  sj} Aij is a maximum set of mutually compatible activities in Sij f0=0 and sn+1 = . Then S = S0,n+1, and the ranges for i and j are given by 0  i, j  n+1. Aij = Aik {ak} Akj Sij is the subset of activities in S that can start after activity ai finishes and finish before activity aj starts. Ch16: Greedy algorithms

A recursive solution Ch16: Greedy algorithms

Converting a dynamic-programming solution to a greedy solution Theorem 16.1 Consider any nonempty subproblem Sk, and let am be the activity in Sk with the earliest finish time: Sk = {ai  Sk : si >= fk}. then Activity am is included in some maximum-size subset of mutually compatible activities of Sk. Ch16: Greedy algorithms

A recursive greedy algorithm   Ch16: Greedy algorithms

The operation of RECURSIVE-ACTIVITY-SELECTOR on the 11 activities given earlier Ch16: Greedy algorithms

Ch16: Greedy algorithms

An iterative greedy algorithm Ch16: Greedy algorithms

16.2 ELEMENTS OF THE GREEDY STRATEGY Ch16: Greedy algorithms

1. Determine the optimal substructure of the problem. 2. Develop a recursive solution. 3. Prove that at any stage of the recursion, one of the optimal choices is the greedy choice. Thus, it is always safe to make the greedy choice. Ch16: Greedy algorithms

5. Develop a recursive algorithm that implements the greedy strategy. 4. Show that all but one of the subproblems induced by having make the greedy choice are empty. 5. Develop a recursive algorithm that implements the greedy strategy. 6. Convert the recursive algorithm to an iterative algorithm. Alternatively, we could have fashioned our optimal substructure with a greedy choice in mind. Ch16: Greedy algorithms

Designing a greedy algorithm 1. Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve. 2. Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe. 3. Demonstrate that, having made the greedy choice , what remains is a subproblem with the property that if we combine an optimal solution to the subproblem with the greedy choice we have made, we arrive at an optimal solution to the original problem. Ch16: Greedy algorithms

Key ingredients Greedy-choice property: A global optimal solution can be achieved by making a local optimal (greedy) choice. Optimal substructure: An optimal solution to the problem within its optimal solution to sub problem. Ch16: Greedy algorithms

Greedy versus Dynamic Programming Given a knapsack capacity W N items, value: v0, v1, …, vn weight: w0, w1, …, wn Fractional knapsack problem 0-1 knapsack problem (Exercise 16.2-2) Ch16: Greedy algorithms

The greedy strategy does not work for the 0-1 knapsack Ch16: Greedy algorithms

16.3 Huffman codes a b c d e f Frequency (in hundred) 45 13 12 16 9 5 Fixed length codeword 000 001 010 011 100 101 Variable length codeword 111 1101 1100 Prefix code: no codeword is also a prefix of some other codeword. Huffman coding is an entropy encoding algorithm used for lossless data compression David Albert Huffman in 1952 Ch16: Greedy algorithms

Simple encoding and decoding. Can be shown that the optimal data compression achievable by a character code can always be achieved with prefix codes. Simple encoding and decoding. An optimal code for a file is always represented by a full binary tree. Ch16: Greedy algorithms

Tree correspond to the coding schemes Ch16: Greedy algorithms

Constructing a Huffman code Ch16: Greedy algorithms

The steps of Huffman’s algorithm Ch16: Greedy algorithms

Ch16: Greedy algorithms

Correction of Huffman’s algorithm Lemma 16.2. Let C be an alphabet in which c  C has frequency f [c]. Let x and y be the two characters in C having the lowest frequencies. Then there exists an optimal prefix code in C in which the codeword for x and y having the same length and differ only in the last bit. Ch16: Greedy algorithms

Proof. Ch16: Greedy algorithms

Ch16: Greedy algorithms

Lemma 16.3 Let C be a given alphabet with frequency f[c] defined for each character c  C. Let x and y be two characters in C with minimum frequency. Let C’ be the alphabet C with characters x, y removed and character z added, so that C’ = C - {x, y}{z}; define f for C' as for C, except that z.freq = x.freq + y.freq. Ch16: Greedy algorithms

Lemma 16.3 (cont) Let T' be any tree representing an optimal prefix code for the alphabet C'. Then the tree T, obtained from T' by replacing the leaf node for z with an internal node having x and y as children, represents an optimal prefix code for the alphabet C. Proof B(T) = B(T') + x.freq + y.freq Ch16: Greedy algorithms

Theorem 16.4 Procedure HUFFMAN produces an optimal prefix code. Ch16: Greedy algorithms