 Greedy Algorithms. Greedy Algorithm  Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems.  If the local optimum.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Greedy Algorithms.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Problem: Huffman Coding Def: binary character code = assignment of binary strings to characters e.g. ASCII code A = B = C =
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.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Monday, 12/2/02 Design Patterns for Optimization Problems Greedy.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
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
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 1 (Part 3) Tuesday, 1/29/02 Design Patterns for Optimization.
Greedy Algorithms CSE 331 Section 2 James Daly. Reminders Exam 2 next week Thursday, April 9th Covers heaps to spanning trees Greedy algorithms (today)
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
Huffman Codes. Encoding messages  Encode a message composed of a string of characters  Codes used by computer systems  ASCII uses 8 bits per character.
Huffman Encoding Veronica Morales.
1 Analysis of Algorithms Chapter - 08 Data Compression.
Greedy Algorithms Dr. Yingwu Zhu. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Greedy Algorithms Jeff Chastine. Greedy Algorithms Compared to Dynamic Programming – Both used in optimization problems – More efficient – Not always.
Huffman coding Content 1 Encoding and decoding messages Fixed-length coding Variable-length coding 2 Huffman coding.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset.
CSCE350 Algorithms and Data Structure Lecture 19 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
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.
Bahareh Sarrafzadeh 6111 Fall 2009
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
COSC 3101A - Design and Analysis of Algorithms 9 Knapsack Problem Huffman Codes Introduction to Graphs Many of these slides are taken from Monica Nicolescu,
Greedy Algorithms.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
1Computer Sciences Department. 2 Advanced Design and Analysis Techniques TUTORIAL 7.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 5. Greedy Algorithms - 1 Greedy.
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.
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)
Greedy Algorithms Alexandra Stefan.
CSC317 Greedy algorithms; Two main properties:
CSCE 411 Design and Analysis of Algorithms
Greedy Technique.
The Greedy Method and Text Compression
Introduction to Algorithms`
Greedy Algorithm.
CS200: Algorithm Analysis
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
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
Presentation transcript:

 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 Algorithm

Knapsack Problem n items a thief’s knapsack of size W

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 $60 $100$120 $135 knapsack ($6/unit)($5/unit)($4/unit)($3/unit) n = 4, W = 50

Fractional Knapsack Problem $60 $100 $120 $135 knapsack ($6/unit) ($5/unit) ($4/unit) ($3/unit) Greedy algorithm: greatest value per unit 50 $240

0-1 Knapsack Problem $60 $100 $120 $135 optimal ($6/unit) ($5/unit) ($4/unit) ($3/unit) 50 value per unit $160 $135 $220

0-1 Knapsack Problem optimal 50 value per unit $160 $135 $220 Difficult to get the optimal solution with a greedy strategy. Dynamic Programming :

Optimal Substructure vs Subproblem Solution T 1,5 T 1,6 T 2,5 T 2,6 T i,j : the solution of a subproblem A subproblem solution? A local optimum?

Greedy Algorithm vs Dynamic Programming Dynamic ProgrammingGreedy Algorithm Computes all subproblemsFind a local optimum Always finds the optimal solution May not be able to find the optimal solution Less efficientMore efficient

Optimal Substructure vs Subproblem Solution  Subproblem solution - From all subproblem solutions  Optimal substructure - With only constant number of parameters - Without subproblems or future choices Usually top-down Usually Bottom-up

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? abcdef Fixed-length

Variable-Length Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? abcdef Variable-length What 0010 menas? = a a b a = a d a = c e …

Prefix Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? abcdef Variable-length 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? abcdef Variable-length No codeword is a prefix of another codeword.

Variable-Length Code abcdefTota l Frequency Fixed-length Variable-length Is it the optimal way?

Variable-Length Code abcdefTota l Frequency Fixed-length Variable-length Alternative?

Huffman Tree charcodefreq a00045 b00113 c01012 d01116 e1009 f1015 Fixed-length code

Huffman Tree charcodefreq a045 b10113 c10012 d11116 e11019 f11005 Variable-length code

Huffman’s Algorithm 0 0 Every non-leaf node has two children. 0 Observation 1 The longest code : at least 2 characters

Huffman’s Algorithm Observation 1 Observation 2 The longest code : at least 2 characters The longest 2 codes : the least frequent two characters 1 0

Huffman’s Algorithm Observation 1 Observation 2 Observation 3 The longest code : at least 2 characters The longest 2 codes : the least frequent two characters A non-leaf node : handled like a leaf node T T ’

Huffman’s Algorithm A non-leaf node : handled like a leaf node Observation T T ’ 1 0 Total length in T = Total length in T ‘ + 16 * 1 + (9 + 5) * 2

Huffman’s Algorithm Observation 1 Observation 2 Observation 3 The longest code : at least 2 characters The longest 2 codes : the least frequent two characters A non-leaf node : handled like a leaf node T T ’

Huffman’s Algorithm Merging two least frequent nodes.

Huffman’s Algorithm Merging two least frequent nodes. 1 0

Huffman’s Algorithm Merging two least frequent nodes

Huffman’s Algorithm Merging two least frequent nodes

Huffman’s Algorithm Merging two least frequent nodes

Huffman’s Algorithm Merging two least frequent nodes charcode a0 b101 c100 d111 e1101 f1100

Greedy Algorithm  Optimization Algorithms : finds a proper local optimum.  Not as powerful as Dynamic Programming, but simpler.  Greedy algorithms - Knapsack Problem - Huffman Code

 Graphs

What is a graph? V = { 1, 2, 3, 4, 5 } E = { {1,2}, {1,3}, {2,3}, {2,4}, {2,5}, {3,4} } G = ( V, E )

Directed and Undirected Directed graphUndirected graph

Representations of Graphs Directed graph Adjacency-list

Representations of Graphs Directed graphAdjacency-Matrix

Representations of Graphs Adjacency-Matrix Adjacency-list Adjacency ListAdjacency Matrix space|V| + |E||V| 2 Finding all edges|V| + |E||V| 2 Finding one edgenum of edges1

Adjacency Matrix of Graphs Directed graphAdjacency-Matrix

Adjacency Matrix of Graphs A = A T =

Weighted Graphs