Greedy Algorithms CSE 331 Section 2 James Daly. Reminders Exam 2 next week Thursday, April 9th Covers heaps to spanning trees Greedy algorithms (today)

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Greedy Algorithms.
Introduction to Algorithms Greedy Algorithms
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 Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
Compression & Huffman Codes
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 1 (Part 3) Tuesday, 9/4/01 Greedy Algorithms.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
© 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.
CSC 213 Lecture 18: Tries. Announcements Quiz results are getting better Still not very good, however Average score on last quiz was 5.5 Every student.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 1 (Part 3) Tuesday, 1/29/02 Design Patterns for Optimization.
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 23. Greedy Algorithms
Algorithm Design & Analysis – CS632 Group Project Group Members Bijay Nepal James Hansen-Quartey Winter
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 5: Advanced Design Techniques.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
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.
For Wednesday No reading No homework There will be homework for Friday, as well the program being due – plan ahead.
Greedy Algorithms Jeff Chastine. Greedy Algorithms Compared to Dynamic Programming – Both used in optimization problems – More efficient – Not always.
Huffman Coding Yancy Vance Paredes. Outline Background Motivation Huffman Algorithm Sample Implementation Running Time Analysis Proof of Correctness Application.
1 Algorithms & Data Structures for Games Lecture 2A Minor Games Programming.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
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 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.
Greedy Algorithms Analysis of Algorithms.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
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.
CSC317 Greedy algorithms; Two main properties:
Tries 07/28/16 11:04 Text Compression
Assignment 6: Huffman Code Generation
Tries 5/27/2018 3:08 AM Tries Tries.
Greedy Technique.
The Greedy Method and Text Compression
The Greedy Method and Text Compression
Greedy Algorithm.
Huffman Coding.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
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.
Chapter 16: Greedy algorithms Ming-Te Chi
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Chapter 16: Greedy algorithms Ming-Te Chi
CSE 326: Data Structures Lecture #24 The Algorhythmics
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Analysis of Algorithms CS 477/677
Presentation transcript:

Greedy Algorithms CSE 331 Section 2 James Daly

Reminders Exam 2 next week Thursday, April 9th Covers heaps to spanning trees Greedy algorithms (today) and dynamic programming (Thursday) will not be on Exam 2 Homework 5 is out Not due until April 16 th (after the exam) Covers some testable material

Greedy Algorithms General algorithm design technique Used to solve optimization problems Multiple options exist, need to find the best one Ex. Shortest path Ex. Minimum spanning tree UV

Greedy Algorithms Make the choice that looks the best right now Make locally optimal choices at each step Expectation of globally good solution Does not always yield an optimal solution Usually simple and fast Ex. Prim’s and Kruskal’s algorithms

Key Ingredients Greedy Choice Property A global optimal solution can be achieved by making locally optimal (greedy) choices Optimal Substructure An optimal solution contains within it optimal solutions to subproblems

Example: Job Scheduling n jobs: j 1, j 2, j 3, … j n Time for executing job j i is t i We have one machine

Job Scheduling Problem Problem 1: What is the order of jobs that minimizes the total running time? Not an optimization problem Always the same cost T 1 = 4 T 2 = 3T 3 = 5T 4 = 6 T 1 = 4T 2 = 3T 3 = 5T 4 = 6

Job Scheduling Problem Problem 2: What is the order of jobs that minimizes the average job completion time Solution: Run the quickest job first ji “Opt” New ij xyz “Opt”: x + y + z New: x-w + y-kw + z (x + y + z) – (k+1) w < Opt w = T j - T i x-w y-kw z k items

Problem: Huffman Codes We want to send a long text document using as few bits as possible How can we encode the file to save space? File Compressed Received File Encode DecodeTransmit

Huffman Codes

What if we know the frequency of each symbol? a: 45%, b: 13%, c: 12%, d: 16%, e: 9%, f: 5% Let’s try a variable length code a: 0 b:101 c: 100 d:111 e: 1101 f: * (0.45*1 + ( ) * 3 + ( ) * 4) = 224,000 bits a a ac a bed f e

Prefix Codes No code word is a prefix of another code word Ex: a: 0 b: 01 c: 011 Ex: a: 0 b: 10 c: 11 Not allowed a c b

Optimization Problem

Huffman Coding Plan: Build a tree from all characters Combine least common characters into a new symbol

Example c 12 % e 9 % f 5 % a 45 % 100%55% b 13 % d 16 % 29%26%14% a 45 % b 13 % d 16 % c 12 %

Huffman(C) Q <- C // Build a heap While (Q.Size > 1): Left <- Q.ExtractMin() Right <- Q.ExtractMin() Freq <- Left.Freq + Right.Freq Node <- (Left, Right, Freq) Q.Insert(Node) Return Q.ExtractMin() Loop happens O(n) times Extraction takes O(log n) time As does insertion O(n log n) time total