COP 4531 Complexity & Analysis of Data Structures & Algorithms

Similar documents
Scheduling Single Machine Scheduling. Tim Nieberg

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Algorithm Design and Analysis

Scheduling Shop Scheduling. Tim Nieberg

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

Near Optimal Solutions

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Classification - Examples

Closest Pair Problem

Approximation Algorithms

Introduction to Scheduling Theory

1 Approximating Set Cover

Data Structures and Algorithms Written Examination

GRAPH THEORY LECTURE 4: TREES

Dynamic Programming. Lecture Overview Introduction

JUST-IN-TIME SCHEDULING WITH PERIODIC TIME SLOTS. Received December May 12, 2003; revised February 5, 2004

Single machine models: Maximum Lateness -12- Approximation ratio for EDD for problem 1 r j,d j < 0 L max. structure of a schedule Q...

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Lecture 22: November 10

Applied Algorithm Design Lecture 5

Lecture 3: Finding integer solutions to systems of linear equations

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

CSC2420 Spring 2015: Lecture 3

Lecture Outline Overview of real-time scheduling algorithms Outline relative strengths, weaknesses

Dynamic Programming Problem Set Partial Solution CMPSC 465

Offline sorting buffers on Line

Definition Given a graph G on n vertices, we define the following quantities:

Topic: Greedy Approximations: Set Cover and Min Makespan Date: 1/30/06

Satisfiability Checking

Single machine parallel batch scheduling with unbounded capacity

Problem Set 7 Solutions

Approximability of Two-Machine No-Wait Flowshop Scheduling with Availability Constraints

Real Time Scheduling Basic Concepts. Radek Pelánek

1. Prove that the empty set is a subset of every set.

Arithmetic Coding: Introduction

CS473 - Algorithms I

PRIME FACTORS OF CONSECUTIVE INTEGERS

[Refer Slide Time: 05:10]

Turing Degrees and Definability of the Jump. Theodore A. Slaman. University of California, Berkeley. CJuly, 2005

Aperiodic Task Scheduling

Large induced subgraphs with all degrees odd

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Cpt S 223. School of EECS, WSU

Page 1. CSCE 310J Data Structures & Algorithms. CSCE 310J Data Structures & Algorithms. P, NP, and NP-Complete. Polynomial-Time Algorithms

SOLUTIONS TO ASSIGNMENT 1 MATH 576

The last three chapters introduced three major proof techniques: direct,

11. APPROXIMATION ALGORITHMS

Guessing Game: NP-Complete?

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Strong and Weak Ties

Today s topic: So far, we have talked about. Question. Task models

Systems of Linear Equations

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

8 Primes and Modular Arithmetic

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Practical Guide to the Simplex Method of Linear Programming

14.1 Rent-or-buy problem

Midterm Practice Problems

The Taxman Game. Robert K. Moniot September 5, 2003

Common Approaches to Real-Time Scheduling

Session 6 Number Theory

Lecture Notes on Linear Search

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Real-Time Scheduling (Part 1) (Working Draft) Real-Time System Example

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Section IV.1: Recursive Algorithms and Recursion Trees

3. Mathematical Induction

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Graphs without proper subgraphs of minimum degree 3 and short cycles

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Chapter 13: Query Processing. Basic Steps in Query Processing

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

Special Situations in the Simplex Algorithm

Passive Discovery Algorithms

Lecture 13 - Basic Number Theory.

The Graphical Method: An Example

CMPSCI611: Approximating MAX-CUT Lecture 20

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

1 if 1 x 0 1 if 0 x 1

Lecture 10 Scheduling 1

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

FUZZY CLUSTERING ANALYSIS OF DATA MINING: APPLICATION TO AN ACCIDENT MINING SYSTEM

LEARNING OBJECTIVES FOR THIS CHAPTER

5 INTEGER LINEAR PROGRAMMING (ILP) E. Amaldi Fondamenti di R.O. Politecnico di Milano 1

A binary search algorithm for a special case of minimizing the lateness on a single machine

8 Divisibility and prime numbers

Scheduling Real-time Tasks: Algorithms and Complexity

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new GAP (2) Graph Accessibility Problem (GAP) (1)

From Last Time: Remove (Delete) Operation

1.204 Lecture 10. Greedy algorithms: Job scheduling. Greedy method

Linear Programming. March 14, 2014

MATHEMATICS Unit Decision 1

CSCE 310J Data Structures & Algorithms. Dynamic programming 0-1 Knapsack problem. Dynamic programming. Dynamic Programming. Knapsack problem (Review)

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

Transcription:

COP 4531 Complexity & Analysis of Data Structures & Algorithms Greedy Algorithms Thanks to the text authors and K Wayne and Piyush Kumar who contributed to these slides

What are greedy algorithms? Similar to dynamic programming and also used for optimization problems Idea When a choice needs to be made, make the one that looks the best right now Thus make a locally optimal choice in hopes of getting a globally optimal solution We will define some general characteristics of when greedy algorithms can give optimal solutions We have already seen that greedy algorithms naturally arise in the minimum spanning tree problem and that we were able to determine a greedy choice, the safe edge, in that problem which gave us two good solution Will explore several problems that can be solved using greedy algorithms 2

Greedy Algorithms For some problems, Greed is good works For some, it finds a good solution which is not global optimal Heuristics Approximation Algorithms For others, it can do very bad 3

Activity Selection = Interval Scheduling n activities (jobs) require exclusive use of a common resource For example use of a classroom Set of activities S = {a 1,, a n } job a j needs resource during period [s j, f j ) s j is the start time of the job f j is the finish time of the job Goal: select the largest possible set of non-overlapping (mutually compatible) jobs 4

Example Activity Selection Job a j starts at s j and finishes at f j Two jobs compatible if they don't overlap Goal: find maximum subset of mutually compatible jobs b a c d e f g optimal is set {b, e, h} note that the smallest interval c is not in the optimal set 0 1 2 3 4 5 6 7 8 9 10 11 h Time 5

Making a greedy choice Choose an activity to add to optimal solution before solving subproblems For the activity-selection problem, we can get away with considering only the greedy choice: the activity that leaves the resource available for as many other activities as possible Question: Which activity leaves the resource available for the most other activities? Answer: The first activity to finish (If more than one activity has earliest finish time, can choose any such activity) Since activities are sorted by finish time, just choose activity a 1 That leaves only one subproblem to solve: finding a maximum size set of mutually compatible activities that start after a 1 finishes This is clearly recursive By optimal substructure, if a 1 is in an optimal solution, then an optimal solution to the original problem consists of a 1 plus all activities in an optimal solution to S 1 Only need to prove that a 1 is always part of some optimal solution 6

Activity Selection: Greedy Algorithms Greedy template Consider jobs in some order Take each job provided it's compatible with the ones already taken [Earliest start time] Consider jobs in ascending order of start time s j [Earliest finish time] Consider jobs in ascending order of finish time f j [Shortest interval] Consider jobs in ascending order of interval length f j - s j [Fewest conflicts] For each job, count the number of conflicting jobs c j Schedule in ascending order of conflicts c j 7

Activity Selection: Greedy Algorithms Greedy template Consider jobs in some order Take each job provided it's compatible with the ones already taken Note that these three strategies do not work and are easily shown to be not optimal breaks earliest start time breaks shortest interval breaks fewest conflicts 8

Activity Selection: Greedy Algorithm Greedy algorithm Consider jobs in increasing order of finish time Take each job provided it's compatible with the ones already taken Sort jobs by finish times so that f 1 f 2 f n A φ jobs selected for j = 1 to n { if (job j compatible with A) A A {j} } return A Implementation O(n log n) Remember job j* that was added last to A Job j is compatible with A if s j f j* 9

More precise algorithm GREEDY-ACTIVITY-SELECTOR(s, f) n = slength A = {a 1 } k = 1 for m = 2 to n if s[m] f[k] return A A = A [a m } k = m Time Complexity θ(n) for this algorithm Need to sort jobs which is O(n lg n) 10

Interval Scheduling: Analysis Theorem Greedy algorithm is optimal Pf (by contradiction) Assume greedy is not optimal, and let's see what happens Let i 1, i 2, i k denote set of jobs selected by greedy Let j 1, j 2, j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,, i r = j r for the largest possible value of r job i r+1 finishes before j r+1 Greedy: i 1 i 1 i r i r+1 OPT: j 1 j 2 j r j r+1 why not replace job j r+1 with job i r+1? 11

Interval Scheduling: Analysis Theorem Greedy algorithm is optimal Pf (by contradiction) Assume greedy is not optimal, and let's see what happens Let i 1, i 2, i k denote set of jobs selected by greedy Let j 1, j 2, j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,, i r = j r for the largest possible value of r job i r+1 finishes before j r+1 Greedy: i 1 i 1 i r i r+1 OPT: j 1 j 2 j r i r+1 solution still feasible and optimal, but contradicts maximality of r 12

41 Interval Partitioning

Interval Partitioning Interval partitioning Lecture j starts at s j and finishes at f j Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room Ex: This schedule uses 4 classrooms to schedule 10 lectures e j c d g b h a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 14

Interval Partitioning Interval partitioning Lecture j starts at s j and finishes at f j Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room Ex: This schedule uses only 3 c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 15

Interval Partitioning: Lower Bound on Optimal Solution Def The depth of a set of open intervals is the maximum number that contain any given time Key observation Number of classrooms needed depth Ex: Depth of schedule below = 3 schedule below is optimal a, b, c all contain 9:30 Q Does there always exist a schedule equal to depth of intervals? c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 16

Interval Partitioning: Greedy Algorithm Greedy algorithm Consider lectures in increasing order of start time: assign lecture to any compatible classroom Sort intervals by starting time so that s 1 s 2 s n d 0 number of allocated classrooms for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 Implementation O(n log n) } d d + 1 For each classroom k, maintain the finish time of the last job added Keep the classrooms in a priority queue 17

Interval Partitioning: Greedy Analysis Observation Greedy algorithm never schedules two incompatible lectures in the same classroom Theorem Greedy algorithm is optimal Pf Let d = number of classrooms that the greedy algorithm allocates Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s j Thus, we have d lectures overlapping at time s j + ε Key observation all schedules use d classrooms 18

42 Scheduling to Minimize Lateness

Scheduling to Minimizing Lateness Minimizing lateness problem Single resource processes one job at a time Job j requires t j units of processing time and is due at time d j If j starts at time s j, it finishes at time f j = s j + t j Lateness: j = max { 0, f j - d j } Goal: schedule all jobs to minimize maximum lateness L = max j Note: a related problem of finding a maximal size subset of jobs that meets deadline is NP-hard 1 2 3 4 5 6 t j 3 2 1 4 3 2 Ex: d j 6 8 9 9 14 15 lateness = 2 lateness = 0 max lateness = 6 d 3 = 9 d 2 = 8 d 6 = 15 d 1 = 6 d 5 = 14 d 4 = 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 20

Minimizing Lateness: Greedy Algorithms Greedy template Consider jobs in some order [Shortest processing time first] Consider jobs in ascending order of processing time t j [Earliest deadline first] Consider jobs in ascending order of deadline d j [Smallest slack] Consider jobs in ascending order of slack d j - t j 21

Minimizing Lateness: Greedy Algorithms Greedy template Consider jobs in some order [Shortest processing time first] Consider jobs in ascending order of processing time t j t j d j counterexample [Smallest slack] Consider jobs in ascending order of slack d j - t j 1 1 100 2 10 10 1 2 t j d j 1 2 10 10 counterexample 22

Minimizing Lateness: Greedy Algorithm Greedy algorithm Earliest deadline first Sort n jobs by deadline so that d 1 d 2 d n t 0 for j = 1 to n Assign job j to interval [t, t + t j ] s j t, f j t + t j t t + t j output intervals [s j, f j ] max lateness = 1 d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 23

Minimizing Lateness: No Idle Time Observation There exists an optimal schedule with no idle time d = 4 d = 6 0 1 2 3 4 5 6 d = 4 d = 6 0 1 2 3 4 5 6 d = 12 Observation The greedy schedule has no idle time d = 12 7 8 9 10 11 7 8 9 10 11 24

Minimizing Lateness: Inversions Def An inversion in schedule S is a pair of jobs i and j such that: i < j but j scheduled before i inversion before swap j i Observation Greedy schedule has no inversions Observation If a schedule (with no idle time) has an inversion, it has one with a pair of inverted jobs scheduled consecutively (In the above, remember that jobs sorted in ascending order of due dates Thus i < j means job i is less than j in this sorted order) 25

Minimizing Lateness: Inversions Def An inversion in schedule S is a pair of jobs i and j such that: i < j but j scheduled before i before swap after swap f' j Claim Swapping two adjacent, inverted jobs reduces the number of inversions by one and does not increase the max lateness Pf Let be the lateness before the swap, and let ' be it afterwards ' k = k for all k i, j ' i i (this is trivially true) If job j is late: Note that this implies that the max lateness can only decrease (or be the same with the swap j i # j inversion = = f # d f f i i i j d d i j j i j (definition) ( j finishes at time f ( i < j) f i (definition) i ) 26

Minimizing Lateness: Analysis of Greedy Algorithm Theorem Greedy schedule S is optimal Pf Define S* to be an optimal schedule that has the fewest number of inversions, and let's see what happens Can assume S* has no idle time If S* has no inversions, then S = S* If S* has an inversion, let i-j be an adjacent inversion swapping i and j does not increase the maximum lateness and strictly decreases the number of inversions this contradicts definition of S* 27

Greedy Analysis Strategies Greedy algorithm stays ahead Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's Exchange argument Gradually transform any solution to the one found by the greedy algorithm without hurting its quality Structural Discover a simple "structural" bound asserting that every possible solution must have a certain value Then show that your algorithm always achieves this bound myopic greed: "at every iteration, the algorithm chooses the best morsel it can swallow, without worrying about the future" Objective function Does not explicitly appear in greedy algorithm Hard, if not impossible, to precisely define "greedy algorithm" 28