Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Data Structures and Algorithms

(CS210A)

Lecture 34
• A new algorithm design paradigm: Greedy strategy
part I

1
Path to the solution of a problem

Examples

intuition/insight

Strategy

Have perseverance
Failure  Better Insight 

No formula

But there is a systematic approach which usually works 


2
Success 
Today’s lecture will demonstrate this approach 

Problem : JOB Scheduling


Largest subset of non-overlapping job

3
A motivating example

•  
Antaragni 2016

• There are large-scale activities to be performed in Auditorium.


• Each large scale activity has a start time and finish time.
• There is overlap among various activities.

Aim: What is the largest subset of activities that can be performed ?

Can you formulate the problem


theoretically through this
example ?

4
Formal Description
A job scheduling problem
•  
INPUT:
• A set of jobs {, ,…, }
• job is specified by two real numbers
s(): start time of job
f(): finish time of job
• A single server

Constraints:
• Server can execute at most one job at any moment of time.
• Job , if scheduled, has to be scheduled during [s(), f()] only.

Aim:
To select the largest subset of non-overlapping jobs which can be executed by the server.

5
Example

•INPUT:
  (, ),(, ), (, ), (, ), (, ), (, ), (, ), (, )

0 1 2 3 4 5 6 7

It makes sense to work with pictures than these numbers

job is said to be non-overlapping with job if [s(), f()] ∩[s(), f()] = ∅

Try to find solution for the above


example. 6
Example

•INPUT:
  (, ),(, ), (, ), (, ), (, ), (, ), (, ), (, )

0 1 2 3 4 5 6 7

What strategy come


job is said to be non-overlapping with job if [s(),
to your f()] ∩[s(), f()] = ∅
mind?

7
Designing algorithm for any problem

1. Choose a strategy based on some intuition


2. Transform the strategy into an algorithm.
Try to prove
correctness Try to design a
of the c conterexample
algorithm

Stop as soon as either of


these goals is reached

8
Designing algorithm for the problem
Strategy 1: Select the earliest start time job

0 1 2 3 4 5 6 7

When one fails to prove the correctness of this strategy,


one should search for a counterexample. Can you
Intuition: transform the above example into a counterexample ?
It might be better to assign jobs as early as possible so as to make optimum use of server.

9
Designing algorithm for the problem
Strategy 1: Select the earliest start time job

0 1 2 3 4 5 6 7

Intuition:
It might be better to assign jobs as early as possible so as to make optimum use of server.

10
Designing algorithm for the problem
Strategy 1: Select the earliest start time job

0 1 2 3 4 5 6 7

Instead ofcounterexample.
getting disappointed, try to realize
Intuition: 
that this counterexample points towards
some other strategy which might work.
It might be better to assign jobs as early as possible so as to make optimum use of server.

11
Designing algorithm for the problem
Strategy 2: Select the job with smallest duration

0 1 2 3 4 5 6 7

When one fails to prove the correctness of this strategy, one


should search for a counterexample. Can you transform the
Intuition: above example into a counterexample ?
Such a job will make least use of the server
 this might lead to larger number of jobs to be executed

12
Designing algorithm for the problem
Strategy 2: Select the job with smallest duration

0 1 2 3 4 5 6 7

Instead of counterexample.
getting disappointed, try to realize
that this counterexample points towards
Intuition:  which might work.
some other strategy
Such a job will make least use of the server
 this might lead to larger number of jobs to be executed

13
Designing algorithm for the problem
Strategy 3: Select the job with smallest no. of overlaps

0 1 2 3 4 5 6 7

When one fails to prove the correctness of this strategy,


one should search for a counterexample. Can you
Intuition: transform the above example into a counterexample ?
Selecting such a job will result in least number of other jobs to be discarded.

14
Designing algorithm for the problem
Strategy 3: Select the job with smallest no. of overlaps

0 1 2 3 4 5 6 7

Have perseverance.
counterexample.
Think fresh.
Intuition:

Selecting such a job will result in least number of other jobs to be discarded.

15
Designing algorithm for the problem
Strategy 4: Select the job with earliest finish time

0 1 2 3 4 5 6 7

Intuition:
Selecting such a job will free the server earliest
 hence more no. of jobs might get scheduled.

16
Designing algorithm for the problem
Strategy 4: Select the job with earliest finish time

0 1 2 3 4 5 6 7

Intuition:
Selecting such a job will free the server earliest
 hence more no. of jobs might get scheduled.

17
Designing algorithm for the problem
Strategy 4: Select the job with earliest finish time

0 1 2 3 4 5 6 7

Intuition:
Selecting such a job will free the server earliest
 hence more no. of jobs might get scheduled.

18
Designing algorithm for the problem
Strategy 4: Select the job with earliest finish time

0 1 2 3 4 5 6 7

It is indeed a correct solution for this example. We should try


Intuition: to prove the correctness of the algorithm.But first we give a
full description of the algorithm based on this strategy.
Selecting such a job will free the server earliest
 hence more no. of jobs might get scheduled.

19
Algorithm “earliest finish time”
Description

•  
Algorithm (Input : set of jobs.)
1. Define  ∅;
2. While <>∅ do
{ Let be the job from with earliest finish time;

 U {};
Remove and all jobs that overlap with from set ;
}
3. Return ;

Running time for a trivial implementation of the above algorithm: O()

20
Algorithm “earliest finish time”
Correctness
•  
Let be the job with earliest finish time.
Lemma1: is present in the optimal solution for .

Wrong

0 1 2 3 4 5 6 7
Too strong a claim !

21
Algorithm “earliest finish time”
Correctness
•  
Let be the job with earliest finish time.
Lemma1: There exists an optimal solution for in which is present.
Proof: Consider any optimal solution for . Let us suppose ∉ .   f() f()
Let be the job from with earliest finish time.
Let    f() < s()   f() < s()
is also an optimal solution.
Reason: has no overlapping intervals. Give arguments.
 𝒙   Hence does not overlap
With any interval of .
 𝒚 We are done 

0 1 2 3 4 5 6 7

22
Homework
•  
Spend 30 minutes today on the following problems.

1. Use Lemma1 to complete the proof of correctness of the algorithm.

2. Design an O(log ) implementation of the algorithm.

23

You might also like