Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.

Slides:



Advertisements
Similar presentations
Lecture 7 Paradigm #5 Greedy Algorithms
Advertisements

Algorithm Design Methods Spring 2007 CSE, POSTECH.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Greedy Algorithms.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
Chapter 5 Fundamental Algorithm Design Techniques.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
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.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 2 Monday, 9/13/06 Design Patterns for Optimization Problems.
Chapter 10: Algorithm Design Techniques
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 1 (Part 3) Tuesday, 1/29/02 Design Patterns for Optimization.
Week 2: Greedy Algorithms
Lecture 7: Greedy Algorithms II
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
Data Compression Gabriel Laden CS146 – Dr. Sin-Min Lee Spring 2004.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday.
More Dynamic Programming Floyd-Warshall Algorithm.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Announcements.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative.
Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
1 Algorithms & Data Structures for Games Lecture 2A Minor Games Programming.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Announcements Finish up Network Flow today Then Review for Final on Monday ◦ HW#5 is due on Monday, let me or the TA’s know if you have trouble starting.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
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.
CS 146: Data Structures and Algorithms July 28 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Dynamic Programming … Continued
Greedy Algorithms Alexandra Stefan.
Algorithm Design Methods
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy Algorithms (Chap. 16)
Design & Analysis of Algorithm Greedy Algorithm
Presented by Po-Chuan & Chen-Chen 2016/03/08
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Exam 2 LZW not on syllabus. 73% / 75%.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Greedy Algorithms Alexandra Stefan.
Algorithm Design Methods
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Lecture 6 Greedy Algorithms
Algorithm Design Methods
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Greedy Algorithms

Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab you are covered since the lab attendance is out of 10. Assignment #3 is posted due 10/20/2010 Exam #2 on 10/27/2010

Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming Algorithms ◦ And Backtracking Algorithms These are 4 common types of algorithms used to solve problems. ◦ For many problems, it is quite likely that at least one of these methods will work.

Greedy Algorithms A greedy algorithm is one where you take the step that seems the best at the time while executing the algorithm. Previous Examples: ◦ Huffman coding ◦ Minimum Spanning Tree Algorithms – Kruskal’s and Prim’s ◦ Dijkstra’s Algorithm

Coin Changing The goal is to give change with the minimal number of coins as possible for a certain number of cents using 1 cent, 5 cent, 10 cent, and 25 cent coins. The Greedy Algorithm ◦ Keep giving as many coins of the largest denomination as possible until you have a remaining value less than the value of that denomination. ◦ Then you continue with the lower denomination and repeat until you’ve given out the correct change. If you think about it, this is the algorithm a cashier typically uses when giving out change.

Coin Changing How do we know this algorithm always works? ◦ Consider all combinations of giving change, ordered from highest denomination to lowest using 1-cent, 5-cent and 10-cent coins. ◦ 2 ways of making change for 25 cents are: 1)10, 10, 1,1,1,1,1, 2)10, 5, 5, 5 ◦ Since each larger denomination is divisible by each smaller one  We can always make a mapping for each coin in one list to a coin or set of coins in the other list. ◦ For example:

Coin Changing This argument doesn’t work for any set of coins without the divisibility rule. ◦ Consider 1,6,10 ◦ There is no way to match up these two ways of producing 30 cents: ◦ ◦66666◦66666 Consider making change for 18 cents ◦ Largest denomination first gives you 10, 6, 1, 1  However, 6, 6, 6 gives you the least amount of coins.

Coin Changing Problem Extension to 25 cent coin ◦ Even though a 10 doesn’t divide into 25, there are no values, multiples of 25, for which it’s advantageous to give a set of dimes over a set of quarters.

Single Room Scheduling Problem Given a single room to schedule, and a list of requests, the goal of this problem is to maximize the total number of events scheduled. ◦ Each request simply consists of the group, a start time and an end time during the day. The Greedy Solution: 1)Sort the requests by finish time. 2)Go through the requests in order of finish time, scheduling them in the room if the room is unoccupied at its start time.

Single Room Scheduling Problem Example: Say you’ve gone to a music festival ◦ There are so many shows and so little time! ◦ If each one of the shows schedule the first day are equally important for you to see, ◦ And you will only go to a show if you can stay the entire time, ◦ Maximize the number of shows you can go to. 5-7pm: Green Day 7-9pm: Weezer 7:30-9:30: Gorillaz 9-11pm: 311 9:30pm-10:30pm: Coldplay 11pm-2am: The Killers

Single Room Scheduling Problem Example: The Greedy Solution: 1)Sort the requests by finish time. 2)Go through the requests in order of finish time, scheduling them in the room if the room is unoccupied at its start time. 5-7pm: Green Day 7-9pm: Weezer 7:30-9:30pm: Gorillaz 9:30pm-10:30pm: Coldplay 9-11pm: pm-2am: The Killers 11pm – 2am The Killers 9:30pm-10:30pm Coldplay 7-9pm Weezer 5-7pm Green Day

Single Room Scheduling Proof Prove that this algorithm does indeed maximize the number of events scheduled Shown on the board

Multiple Room Scheduling Given a set of requests with start and end times, the goal here is to schedule all events using the minimal number of rooms Greedy Algorithm: 1)Sort all the requests by start time. 2)Schedule each event in any available empty room. If no room is available, schedule the event in a new room.

Multiple Room Scheduling Example Greedy Algorithm: 1)Sort all the requests by start time. 2)Schedule each event in any available empty room. If no room is available, schedule the event in a new room. Final Exam Schedule for Computer Science: 7-10am: CS1 7-10am: Intro to C 9am-12pm: OOP 10am-1pm: OS 12-3pm: CS 2 1-4pm: Comp Arch. ROOM am: CS 1 ROOM am: Intro to C ROOM 3 9am-12pm: OOP 10am-1pm: OS12pm-3pm: CS 2 1-4pm: Comp Arch

Multiple Room Scheduling Proof Let k be the number of rooms this algorithm uses for scheduling. When the kth room is scheduled, it MUST have been the case that all k-1 rooms before it were in use. At the exact point in time that the k room gets scheduled, we have k simultaneously running events. It's impossible for any schedule to handle this type of situation with less than k rooms. Thus, the given algorithm minimizes the total number of rooms used.

Fractional Knapsack Problem Your goal is to maximize the value of a knapsack that can hold at most W units worth of goods from a list of items I 1, I 2,... I n. Each item has two attributes: 1)A value/unit; let this be v i for item I i. 2)Weight available; let this be w i for item I i. The algorithm is as follows: 1) Sort the items by value/unit. 2)Take as much as you can of the most expensive item left, moving down the sorted list. You may end up taking a fractional portion of the "last" item you take.

Fractional Knapsack Example Say we have: ◦ 4 lbs. of I 1 available with a value of $50/lb. ◦ 25 lbs. of I 3 available with a value of $40/lb. ◦ 40 lbs. of I 2 available with a value of $30/lb. The algorithm is as follows: 1) Sort the items by value/unit. 2)Take as much as you can of the most expensive item left, moving down the sorted list. You may end up taking a fractional portion of the "last" item you take. You will do the following: 1)Take 4 lbs of I 1. 2)Take 25 lbs. of I 3. 3)Tale 21 lbs. of I 2. Value of knapsack = 4* * *30 = $1830.

Fractional Knapsack Proof Why is this maximal? ◦ Because if we were to exchange any good from the knapsack with what was left over, it is IMPOSSIBLE to make an exchange of equal weight such that the knapsack gains value. ◦ The reason for this is that all the items left have a value/lb. that is less than or equal to the value/lb. of ALL the material currently in the knapsack. ◦ At best, the trade would leave the value of the knapsack unchanged. ◦ Thus, this algorithm produces the maximal valued knapsack!

References Slides adapted from Arup Guha’s Computer Science II Lecture notes: ctures/ ctures/ Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss Additional images: xkcd.com