lecture-32-cs210-2012.pptx
TRANSCRIPT
-
8/11/2019 Lecture-32-CS210-2012.pptx
1/24
Data Structures and Algorithms
(CS210/ESO207/ESO211)
Lecture 32
Greedy strategy: a new algorithm paradigm
PartI
1
-
8/11/2019 Lecture-32-CS210-2012.pptx
2/24
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 abovejourneyin this lecture.
2
-
8/11/2019 Lecture-32-CS210-2012.pptx
3/24
Two problems
1. Job scheduling problems
- a set of jobs
- a set of servers
- some optimization criteria
- constraintsAim: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
-
8/11/2019 Lecture-32-CS210-2012.pptx
4/24
Problem 1
4
JOB Scheduling
Largest subset of non-overlapping job
-
8/11/2019 Lecture-32-CS210-2012.pptx
5/24
A job scheduling problemDescription
INPUT:
Aset of jobs {,,, } where jobis 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
6/24
A job scheduling problemDeveloping 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.
-
8/11/2019 Lecture-32-CS210-2012.pptx
7/24
A job scheduling problemDeveloping familiarity with the problem through an example
INPUT: (,),(. ,. ), (. ,. ), (. ,), (,), (. ,. ), (. ,. ), (. ,. )
Seeing the solution (red bars) of this example, what strategy comes to your mind tosolve this problem ?
7
0 1 2 3 4 5 6 7
-
8/11/2019 Lecture-32-CS210-2012.pptx
8/24
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
cTry to design a
conterexample
c
Stop as soon as either of
these goals is reached
-
8/11/2019 Lecture-32-CS210-2012.pptx
9/24
Designing algorithm for the problemstrategy 1: Select the earliest start timejob
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 ?
-
8/11/2019 Lecture-32-CS210-2012.pptx
10/24
Designing algorithm for the problemstrategy 1: Select the earliest start timejob
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
-
8/11/2019 Lecture-32-CS210-2012.pptx
11/24
Designing algorithm for the problemstrategy 1: Select the earliest start timejob
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.
-
8/11/2019 Lecture-32-CS210-2012.pptx
12/24
Designing algorithm for the problemstrategy 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 ?
-
8/11/2019 Lecture-32-CS210-2012.pptx
13/24
Designing algorithm for the problemstrategy 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
14/24
Designing algorithm for the problemstrategy 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.
-
8/11/2019 Lecture-32-CS210-2012.pptx
15/24
Designing algorithm for the problemstrategy 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 ?
-
8/11/2019 Lecture-32-CS210-2012.pptx
16/24
Designing algorithm for the problemstrategy 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 upour perseverance. After all, these strategies
and counterexamples have increased our
understanding about the problem. Think of
some other strategy after studying these
counterexamples.
-
8/11/2019 Lecture-32-CS210-2012.pptx
17/24
Designing algorithm for the problemstrategy 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
18/24
Designing algorithm for the problemstrategy 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
19/24
Designing algorithm for the problemstrategy 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
20/24
Designing algorithm for the problemstrategy 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
21/24
Designing algorithm for the problemstrategy 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 forthis 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.
-
8/11/2019 Lecture-32-CS210-2012.pptx
22/24
Algorithmearliest finish timeDescription
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
-
8/11/2019 Lecture-32-CS210-2012.pptx
23/24
Algorithmearliest finish timeCorrectness
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 thatis 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
-
8/11/2019 Lecture-32-CS210-2012.pptx
24/24
Homework exercises
Spend 30 minutes today on the following problems.
1. Use Lemma1to complete the proof of correctness of the algorithm.
2. Design an O(log ) implementation of the algorithm.
24