Professional Documents
Culture Documents
Data Structures and Algorithms Data Structures and Algorithms
Data Structures and Algorithms 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
3
A motivating example
•
Antaragni 2016
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
•INPUT:
(, ),(, ), (, ), (, ), (, ), (, ), (, ), (, )
0 1 2 3 4 5 6 7
7
Designing algorithm for any problem
8
Designing algorithm for the problem
Strategy 1: Select the earliest start time job
0 1 2 3 4 5 6 7
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
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
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
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 ;
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.
23