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



Similar documents
Dynamic programming formulation

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

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

Scheduling Single Machine Scheduling. Tim Nieberg

Near Optimal Solutions

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Branch-and-Price Approach to the Vehicle Routing Problem with Time Windows

Approximation Algorithms

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Classification - Examples

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Analysis of Algorithms I: Optimal Binary Search Trees

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

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

Scheduling Home Health Care with Separating Benders Cuts in Decision Diagrams

The Problem of Scheduling Technicians and Interventions in a Telecommunications Company

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Data Structures and Algorithms

6.852: Distributed Algorithms Fall, Class 2

Offline sorting buffers on Line

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Ordered Lists and Binary Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

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

CS170 Lab 11 Abstract Data Types & Objects

24. The Branch and Bound Method

An optimisation framework for determination of capacity in railway networks

Network File Storage with Graceful Performance Degradation

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...

HYBRID GENETIC ALGORITHMS FOR SCHEDULING ADVERTISEMENTS ON A WEB PAGE

Part I. Multiple Choice Questions (2 points each):

INTEGER PROGRAMMING. Integer Programming. Prototype example. BIP model. BIP models

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

International Journal of Advanced Research in Computer Science and Software Engineering

Real Time Scheduling Basic Concepts. Radek Pelánek

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

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

Scheduling Shop Scheduling. Tim Nieberg

Java CPD (I) Frans Coenen Department of Computer Science

6.231 Dynamic Programming and Stochastic Control Fall 2008

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Solutions to Homework 6

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

MATHEMATICAL ENGINEERING TECHNICAL REPORTS. The Best-fit Heuristic for the Rectangular Strip Packing Problem: An Efficient Implementation

Discrete Optimization

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

Introduction to Java

Building Java Programs

The Interface Concept

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

LOOPS CHAPTER CHAPTER GOALS

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Sample CSE8A midterm Multiple Choice (circle one)

J a v a Quiz (Unit 3, Test 0 Practice)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

A Constraint Programming based Column Generation Approach to Nurse Rostering Problems

Union-Find Algorithms. network connectivity quick find quick union improvements applications

Load Balancing. Load Balancing 1 / 24

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Applied Algorithm Design Lecture 5

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

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

Building Java Programs

Sample Questions Csci 1112 A. Bellaachia

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

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

OPTIMAL BINARY SEARCH TREES

Exponential time algorithms for graph coloring

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

Data Structures and Algorithms Written Examination

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

Ant Colony Optimization and Constraint Programming

AP Computer Science Static Methods, Strings, User Input

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Stacks. Data Structures and Data Types. Collections

Searching Algorithms

Introduction to Scheduling Theory

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Binary Heap Algorithms

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Dynamic Programming. Lecture Overview Introduction

Building Java Programs

CS/COE

Batch Production Scheduling in the Process Industries. By Prashanthi Ravi

5.1 Bipartite Matching

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

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,

Arithmetic Coding: Introduction

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Unit 6. Loop statements

Transcription:

1.204 Lecture 10 Greedy algorithms: Knapsack (capital budgeting) Job scheduling Greedy method Local improvement method Does not look at problem globally Takes best immediate step to find a solution Useful in many cases where Objectives or constraints are uncertain, or An approximate answer is all that s required Generally O(n) complexity, easy to implement and interpret results Often requires sorting the data first, which is O(n lg n) In some cases, greedy algorithms provide optimal solutions (shortest paths, spanning trees, some job scheduling problems) In most cases they are approximate algorithms Sometimes used as a part of an exact algorithm (e.g., as a relaxation in an integer programming algorithm) 1

General greedy algorithm // Pseudocode public solution greedy(problem) { solution= empty set; problem.sort(); // Usually place elements in order for (element: problem) { if (element feasible and appears optimal) solution= union(solution, element); return solution; Some greedy algorithms sort, some use a heap, some don t need to sort at all. Greedy knapsack problem We have n objects, each with weight w i and profit p i. The knapsack has capacity M. max s. t. 0 i< n 0 x p i 0 i< n w x i i i 0, w pix i M <= 1 i 0, 0 i < n 2

Greedy knapsack algorithm Algorithm chooses element with highest value/weight ratio first, the next highest second, and so on until it reaches the capacity of the knapsack. This is the same as a gradient or derivative method. Knapsack: integer or not? Let M= 1. Integer solution is {2, 3, an unexpected result in some contexts. Greedy solution is {1, 98% of 2. If problem has hard constraints, need integer solution. If constraints are fuzzy, greedy solution may be better. 3

Knapsack problems Truck packing: integer knapsack Packing problem in 2 and 3 dimensions is extension Investment program: Greedy knapsack at high level Can be integer knapsack at individual transaction level (Highway investment or telecom capital investment programs often handled as integer problem, with occasionally hard-tointerpret results) Used to train telecom execs for spectrum auction Interactions between projects: Greedy can be extended to handle interactions between small numbers of projects (that can be enumerated) Integer program handles this explicitly Greedy knapsack code, p.1 public class Knapsack { private static class Item implements Comparable { public double ratio; // Profit/weight ratio public int weight; public Item(double r, int w) { ratio = r; weight = w; public int compareto(object o) { Item other = (Item) o; if (ratio > other.ratio) ratio) // Descending sort return -1; else if (ratio < other.ratio) return 1; else return 0; 4

Greedy knapsack code, p.2 public static double[] knapsack(item[] e, int m) { int upper = m; // Knapsack capacity // 0-1 answer array: 1 if item in knapsack, 0 if not double[] x= new double[e.length]; int i; for (i= 0; i < e.length; i++) { if (e[i].weight > upper) break; x[i]= 1.0; upper -= e[i].weight; if (i < e.length) // If all items not in knapsack x[i]= (double) upper/ e[i].weight; // Fractional item return x; Greedy knapsack code, p.3 public static void main(string[] args) { Item a = new Item(2.0, 2); Item b = new Item(1.5, 4); Item c = new Item(2.5, 2); Item d = new Item(1.66667, 3); Item[] e = { a, b, c, d ; Arrays.sort(e); int m = 7; System.out.println("Capacity: " + m); double[] projectset= knapsack(e, m); double cumprofit= 0.0; for (int i= 0; i < e.length; i++) { System.out.println( ); // See Java code cumprofit+= projectset[i]*e[i].weight*e[i].ratio; System.out.println("Cumulative benefit: " + cumprofit); 5

Greedy knapsack output Capacity: 7 i: ratio: 2.5 wgt: 2 profit: 5.0 in? 1.0 i: ratio: 2.0 wgt: 2 profit: 4.0 in? 1.0 i: ratio: 1.67 wgt: 3 profit: 5.0 in? 1.0 i: ratio: 1.5 wgt: 4 profit: 6.0 in? 0.0 Cumulative benefit: 14.0 (Roundoff errors omitted) This greedy example yields an integer solution. Most don t: Run knapsack() with m= 6 or 8 or Greedy job scheduling We have a set of n jobs to run on a processor (CPU) or machine Each job i has a deadline d i >=1 and profit p i >=0 There is one processor or machine Each job takes 1 unit of time (simplification) We earn the profit if and only if the job is completed by its deadline Profit can be the priority of the task in a real time system that discards tasks that cannot be completed by their deadline We want to find the subset of jobs that maximizes our profit This is a restricted version of a general job scheduling problem, which is an integer programming problem Example use in telecom engineering and construction scheduling Many small jobs, profit proportional to customers served This is then combined with integer programming solution for big jobs Greedy also used in how many machines/people problems (hw 1) Buy versus contract 6

Greedy job scheduling example Number of jobs n=5. Time slots 1, 2, 3. (Slot 0 is sentinel) Job (i) Profit Deadline Profit/Time A 100 2 100 B 19 1 19 C 27 2 27 D 25 1 25 E 15 3 15 Greedy job scheduling algorithm Sort jobs by profit/time ratio (slope or derivative): A (deadline 2), C (2), D (1), B (1), E (3) Place each job at latest time that meets its deadline Nothing is gained by scheduling it earlier, and scheduling it earlier could prevent another more profitable job from being done Solution is {C, A, E with profit of 142 C (27) A (100) E (15) 0 1 2 3 Time D, B infeasible This can be subproblem: how many machines/people needed 7

Greedy job data structure Simple greedy job algorithm spends much time looking for latest slot a job can use, especially as algorithm progresses and many slots are filled. n jobs would, on average, search n/2 slots This would be an O(n 2 ) algorithm By using our set data structure, it becomes nearly O(n) Recall set find and union are O(Ackermann s function), which is nearly O(1) We invoke n set finds and unions in our greedy algorithm Simple job scheduling: O(n 2 ) public static int[] simplejobsched(item[] jobs) { int n= jobs.length; int[] jobset= new int[n]; boolean[] slot= new boolean[n]; for (int i= 1; i < n; i++) { for (int j= jobs[i].deadline; j > 0; j--) { if (!slot[j]) { slot[j]= true; jobset[j]= i; break; return jobset; 8

Fast job scheduling (almost O(n)) We use i to denote time slot i At the start of the method, each time slot i is its own set There are b time slots, where b= min{n, max(d i ) Usually b= max(d i ), the latest deadline Each set k of slots has a value F(k) for all slots i in set k This stores the highest free slot before this time F(k) is defined only for root nodes in sets Job scheduling algorithm Initially all slots are free We have b+1 sets corresponding to b+1 time slots i, 0 i b Slot 0 is a sentinel Initially F(i)= i for all i We will use parent p[i] to link slot i into its set Initially p[i]= -1 for all i Parent of root is negative of number of nodes in set To schedule job i with deadline d i : Find root of tree containing slot min(n, d i ) Usually this is just slot d i If root of i s set is j, then F(j) is latest free slot, provided F(j) 0 After using slot F(j), we combine ( set union ) set having root j with set having slot F(j) -1 9

Job scheduling example Free Free F(j), j is root x x x x x x x= used j j j j j any node can be root of set All in same set Job scheduling algorithm operation 5 10

Job sequence code, p.1 public class JobSeqFast { private static class Item implements Comparable { private int profit; private int deadline; e; private String name; public Item(int p, int d, String n) { profit= p; deadline= d; name= n; public int compareto(object o) { Item other = (Item) o; if (profit > other.profit) // Descending sort return -1; else if (profit < other.profit) return 1; else return 0; // Add getxxx() and setxxx() methods for completeness Job sequence code, p.2 public static int[] fjs(item[] jobs, int b) { int n= jobs.length; int[] j= new int[n]; // Profit max jobs, in time order Set jobset = new Set(b); int[] f = new int[b]; // Highest free slot, job due at i for (int i = 0; i < b; i++) f[i] = i; // Sentinel at jobs[0] for (int i = 1; i < n; i++) { // Jobs in profit order int q = jobset.collapsingfind(math.min(n, jobs[i].deadline)); if (f[q]!= 0) { // If free slot exists j[q] = i; // Add job in that slot int m = jobset.collapsingfind(f[q] - 1); // Find earlier slot jobset.weightedunion(m, q); // Unite sets f[q] = f[m]; // In case q is root, not m return j; // Jobs in optimal set // More comments in download code 11

Job sequence code, p.3 public static void main(string args[]) { Item sentinel= new Item(0, 0, s );// Don t sort-leave in place Item a = new Item(100, 2, a ); // Also create b, c, d, e Item[] jobs = { sentinel, a, b, c, d, e ; Arrays.sort(jobs, sort(jobs 1, jobs.length-1); // Sort descending int maxd= -1; // Maximum deadline for (Item i: jobs) if (i.deadline > maxd) maxd= i.deadline; maxd++; int bb= Math.min(maxD, jobs.length); int[] j= fjs(jobs, bb); System.out.println("Jobs done:"); for (int i= 1; i < maxd; i++) { if (j[i]>0) { System.out.println(" Job "+ jobs[j[i]].name + " at time "+ i); // And compute and output total jobs, total profit Job sequence example output Jobs done: Job c at time 1 Job a at time 2 Job e at time 3 Number of jobs done: 3, total profit: 142 12

Summary This job scheduling special case solvable with greedy algorithm We revisit more general version with dynamic programming Capital planning problems often solvable with greedy algorithm Other greedy algorithms Spanning trees (next time) Shortest paths (in two lectures) Other job scheduling problems (e.g. min time schedule) Graph coloring heuristic Traveling salesperson heuristic (2-opt, 3-opt) Used as part of simulated annealing Greedy algorithms are fast and relatively simple Consider them as parts of more complex solutions, or As approximate solutions 13

MIT OpenCourseWare http://ocw.mit.edu 1.204 Computer Algorithms in Systems Engineering Spring 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.