Sunteți pe pagina 1din 24

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 32

Greedy strategy: a new algorithm paradigm
Part I
1
Overview of this lecture
In addition to solving a job scheduling problem, this lecture will give you an
outline of how any algorithmic problem is solved. In short, designing an
efficient and provably correct algorithm for a problem involves
Developing familiarity with the problem through some examples.
Examples help you develop some intuition/insight into the problem.
The intuitions guide us towards some strategy to solve the problem.
But intuitions may be sometimes misleading (due to lack of full understanding
about the problem).
So many times you fail, but each failure adds to your understanding of the problem
and further guides you towards the solution of the problem.
If one has positive attitude and lot of perseverance, he/she arrives at the
algorithm through a sequence of milestones of failures ...

I hope the reader will get a glimpse of the above journey in this lecture.
2
Two problems
1. Job scheduling problems
- a set of jobs
- a set of servers
- some optimization criteria
- constraints
Aim: to select an optimal subset of jobs to be executed on servers
such that all constraints are satisfied.

2. Graph problems
- Minimum spanning tree (to be explained in detail in the the next class)
- Shortest paths
3
Problem 1
4
JOB Scheduling
Largest subset of non-overlapping job
A job scheduling problem
Description
INPUT:
A set of jobs {

,,

} where job

is specified by two real numbers


s(): start time of job


f(): finish time of job


A single server

Constraint: Server can execute at most one job at any moment of time.

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

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

5
A job scheduling problem
Developing familiarity with the problem through an example

INPUT: (, ), (. , . ), (. , . ), (. , ), (, ), (. , . ), (. , . ), (. , . )








It makes sense to work with pictures than these numbers
6
0 1 2 3 4 5 6 7
Try to find solution for the above
example.
A job scheduling problem
Developing familiarity with the problem through an example

INPUT: (, ), (. , . ), (. , . ), (. , ), (, ), (. , . ), (. , . ), (. , . )








Seeing the solution (red bars) of this example, what strategy comes to your mind to
solve this problem ?
7
0 1 2 3 4 5 6 7
Designing algorithm for a problem
1. Choose a strategy based on some intuition
2. Transform the strategy into an algorithm.
8
Try to prove
correctness
of the
algorithm
c
Try to design a
conterexample
c
Stop as soon as either of
these goals is reached
Designing algorithm for the problem
strategy 1: Select the earliest start time job
Intuition:
It might be better to assign jobs as early as possible so as to make optimum use of server.
9
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
above example into a counterexample ?
Designing algorithm for the problem
strategy 1: Select the earliest start time job
Intuition:
It might be better to assign jobs as early as possible so as to make optimum use of server.
10
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 1: Select the earliest start time job
Intuition:
It might be better to assign jobs as early as possible so as to make optimum use of server.
11
0 1 2 3 4 5 6 7
This is indeed a counterexample. Instead of
getting disappointed, try to realize that this
counterexample points towards some other
strategy which might work.
Designing algorithm for the problem
strategy 2: Select the job with smallest duration
Intuition:
Such a job will make least use of the server and so might lead to larger number of jobs to be
executed
12
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
above example into a counterexample ?
Designing algorithm for the problem
strategy 2: Select the job with smallest duration
Intuition:
Such a job will make least use of the server and so might lead to larger number of jobs to be
executed
13
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 2: Select the job with smallest duration
Intuition:
Such a job will make least use of the server and so might lead to larger number of jobs to be
executed
14
0 1 2 3 4 5 6 7
This is indeed a counterexample. Instead of
getting disappointed, try to realize that this
counterexample points towards some other
strategy which might work.
Designing algorithm for the problem
strategy 3: Select the job with smallest number of overlaps
Intuition:
Selecting such a job will avoid least number of other jobs to be discarded.
15
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
above example into a counterexample ?
Designing algorithm for the problem
strategy 3: Select the job with smallest number of overlaps
Intuition:
Selecting such a job will avoid least number of other jobs to be discarded.
16
0 1 2 3 4 5 6 7
Counterexample again ! Let us not give up
our perseverance. After all, these strategies
and counterexamples have increased our
understanding about the problem. Think of
some other strategy after studying these
counterexamples.
Designing algorithm for the problem
strategy 4: Select the job with earliest finish time
Intuition:
Selecting such a job will free the server earliest and hence more no. of jobs will get scheduled.
Let us apply this strategy on some reasonably tough example.

17
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 4: Select the job with earliest finish time
Intuition:
Selecting such a job will free the server earliest and hence more no. of jobs will get scheduled.
Let us apply this strategy on some reasonably tough example.

18
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 4: Select the job with earliest finish time
Intuition:
Selecting such a job will free the server earliest and hence more no. of jobs will get scheduled.
Let us apply this strategy on some reasonably tough example.

19
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 4: Select the job with earliest finish time
Intuition:
Selecting such a job will free the server earliest and hence more no. of jobs will get scheduled.
Let us apply this strategy on some reasonably tough example.

20
0 1 2 3 4 5 6 7
Designing algorithm for the problem
strategy 4: Select the job with earliest finish time
Intuition:
Selecting such a job will free the server earliest and hence more no. of jobs will get scheduled.
Let us apply this strategy on some reasonably tough example.

21
0 1 2 3 4 5 6 7
It is indeed a correct solution for
this example. We should try to
prove the correctness of the
algorithm.
But first we give a full description
of the algorithm based on this
strategy.
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(

)

22
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 .
Let be the job from with earliest finish time.
Let be the set obtained by replacing in by .
We claim that is also an optimal solution.
(Suitable arguments were given in the class to show that consists of non-overlapping intervals.
The fact that has earliest finish time played a key role for this. Try to reconstruct these arguments.)
23
0 1 2 3 4 5 6 7


Homework exercises


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

S-ar putea să vă placă și