Greedy Algorithms Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 4 1.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Greedy Algorithms (Huffman Coding)
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Compression & Huffman Codes
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.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Lecture 6: Greedy Algorithms I Shang-Hua Teng. Optimization Problems A problem that may have many feasible solutions. Each solution has a value In maximization.
Data Structures – LECTURE 10 Huffman coding
Greedy Algorithms CSE 331 Section 2 James Daly. Reminders Exam 2 next week Thursday, April 9th Covers heaps to spanning trees Greedy algorithms (today)
Greedy Algorithms Huffman Coding
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.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
Huffman Codes. Encoding messages  Encode a message composed of a string of characters  Codes used by computer systems  ASCII uses 8 bits per character.
Greedy Algorithms and Matroids Andreas Klappenecker.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Greedy algorithms David Kauchak cs161 Summer 2009.
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. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
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.
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 2 David Kauchak cs302 Spring 2012.
Greedy Algorithms. Zhengjin,Central South University2 Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
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:
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Greedy Technique.
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
The Greedy Method and Text Compression
Greedy Algorithm.
Chapter 16: Greedy Algorithm
Algorithms (2IL15) – Lecture 2
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.
Huffman Coding CSE 373 Data Structures.
Lecture 8 Greedy Approach
Merge Sort Dynamic Programming
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
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
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(%)
Analysis of Algorithms CS 477/677
Presentation transcript:

Greedy Algorithms Analysis of Algorithms

Greedy Algorithm Paradigm Characteristics of greedy algorithms: make a sequence of choices each choice is the one that seems best so far, only depends on what's been done so far choice produces a smaller problem to be solved

Designing a Greedy Algorithm Cast the problem so that we make a greedy (locally optimal) choice and are left with one subproblem Prove there is always a (globally) optimal solution to the original problem that makes the greedy choice Show that the choice together with an optimal solution to the subproblem gives an optimal solution to the original problem

Some Greedy Algorithms Coin changing algorithm Huffman codes Kruskal's MST algorithm Prim's MST algorithm Dijkstra's Shortest Path algorithm …

Making Change Input Task Example Positive integer n Compute a minimal multiset of coins from C = {d1, d2, d3, …, dk} such that the sum of all coins chosen equals n Example n = 73, C = {1, 5, 10, 25} Solution: 3 coins of size 20, 1 coin of size 10, 3 coins of size 1

Greedy Algorithm Suppose we have n types of coins with values d1 > d2 > … > dn > 0 Input: A positive integer C m[1] := 0; m[2]:=0; … ; m[n] := 0; for i = 1 to n do while C > di do C := C – di; m[i]++; end while; end for;

Knapsack Problem There are n different items in a store Item i : weighs wi pounds worth $vi A thief breaks in Can carry up to W pounds in his knapsack What should he take to maximize the value of his haul?

Greedy Algorithm 3 items: Knapsack can hold 50 lbs Greedy algorithm: item 1 weighs 10 lbs, worth $60 ($6/lb) item 2 weighs 20 lbs, worth $100 ($5/lb) item 3 weighs 30 lbs, worth $120 ($4/lb) Knapsack can hold 50 lbs Greedy algorithm: take item 1 take item 2 no room for item 3

Finding Optimal Code Input: Output: data file of characters and number of occurrences of each character Output: a binary encoding of each character so that the data file can be represented as efficiently as possible "optimal code"

Huffman Code Idea: use short codes for more frequent characters and long codes for less frequent char a b c d e f total bits # 45 13 12 16 9 5 fixed 000 001 010 011 100 101 300 variable 111 1101 1100 224

How to Decode? 101111110100 With fixed length code, easy: break up into 3's, for instance For variable length code, ensure that no character's code is the prefix of another no ambiguity 101111110100 b d e a a

Tree Representation fixed length code 1 cost of code is sum, 1 cost of code is sum, over all chars c, of number of occurrences of c times depth of c in the tree 1 1 1 1 a b c d e f

Tree Representation 1 variable length code a 1 cost of code is sum, 1 variable length code a 1 cost of code is sum, over all chars c, of number of occurrences of c times depth of c in the tree 1 1 b c 1 d f e

Greedy Algorithm Given set C of n chars, c occurs f[c] times Insert each c into priority queue Q using f[c] as key for i := 1 to n-1 do x := extract-min(Q); y := extract-min(Q); make a new node z w/ left child x (label edge 0), right child y (label edge 1), and f[z] = f[x] + f[y]; insert z into Q;

Matroid Let S be a finite set, and F a nonempty family of subsets of S, that is, F P(S). We call (S,F) a matroid if and only if M1) If BF and A  B, then AF [hereditary] M2) If A,BF and |A|<|B|, then there exists x in B\A such that A{x} in F [exchange property] Matroid is useful when determining whether greedy technique yields optimal solutions. It covers many cases of practical interests.