Lecture 11

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

Lecture No. 11

1
Activity Selection Problem
• Input: A set of activities S = {a1,…, an}
• Each activity ai has start time si and a finish time fi
such that 0 ≤ si < fi < ∞
• An activity ai takes place in the half-open interval [si
, fi )
• Two activities are compatible if and only if their
interval does not overlap
i.e., ai and aj are compatible if [si , fi ) and
[sj , fj ) do not overlap (i.e. si ≥ fj or sj ≥ fi )
• Output: A maximum-size subset of mutually
compatible activities
• Also Called Interval Scheduling Problem
Activity Selection Problem
• Brute Force Solution
All possible subsets
Running time: O(2n)
• “Greedy” Strategies:
Greedy 1: Pick the shortest activity, eliminate all activities
that conflict with it, and recurse.
Greedy 2: Pick the activity that starts first, eliminate all the
activities that conflict with it, and recurse.
Greedy 3: Pick the activity that ends first, eliminate all the
activities that conflict with it, and recurse.
Observe
• Greedy1 and Greedy2 does not work
• Greedy3 seems to work (Why?)
Notation
Notation

= subset of activities that start after activity ai finishes


and finish before Activity aj starts

Let c[i,j] be the number of activities in a maximum-size


subset of mutually compatible activities in Sij.
We Show this is a Greedy Problem
Theorem:
Consider any nonempty subproblem Sij, and let am
be the activity in Sij with earliest finish time: fm=
min{fk : ak  Sij}, then
1. Activity am is used in some maximum-size subset
of mutually compatible activities of Sij.
2. The subproblem Sim is empty, so that choosing am
leaves Smj as the only one that may be nonempty.
Solution
Greedy Solution
• Repeatedly choose the activity that finishes
first
• Remove activities that are incompatible with
it, and
• Repeat on the remaining activities until no
activities remain.
We Show this is a Greedy Problem
Consequences of the Theorem?
1. Normally we have to inspect all subproblems,
here we only have to choose one subproblem
2. What this theorem says is that we only have to find
the first subproblem with the smallest finishing time
3. This means we can solve the problem top down by
selecting the optimal solution to the local subproblem
rather than bottom-up as in DP
Example

To solve the Si,j:


1. Choose the activity am with the earliest finish time.
2. Solution of Si,j = {am} U Solution of subproblem Sm,j
To solve S0,12, we select a1 that will finish earliest, and solve for
S1,12.
To solve S1,12, we select a4 that will finish earliest, and solve for
S4,12.
To solve S4,12, we select a8 that will finish earliest, and solve for
S8,12
And so on (Solve the problem in a top-down fashion)
Elements of greedy strategy
• Determine the optimal substructure
• Develop the recursive solution
• Prove one of the optimal choices is the greedy choice yet safe
• Show that all but one of subproblems are empty after greedy
choice
• Develop a recursive algorithm that implements the greedy
strategy
• Convert the recursive algorithm to an iterative one.

This was designed to show the similarities and differences


between dynamic programming and the Greedy approach;
These steps can be simplified if we apply the Greedy approach
directly
Applying Greedy Strategy
Steps in designing a greedy algorithm
The optimal substructure property holds
(same as dynamic programming)
Other Greedy Strategies
Other Greedy Strategies:
Greedy 4: Pick the activity that has the fewest conflicts,
eliminate all the activities that conflict with it, and
recurse.
Any More???
Weighted Interval Scheduling
Weighted interval scheduling problem
• Each job ai has start time si and a finish time fi
such that 0 ≤ si < fi < ∞
• and has weight or value vi .
• Two jobs are compatible if they don't overlap.
• Goal: find maximum weight subset of
mutually compatible jobs.
Weighted Interval Scheduling
Recall
• Greedy algorithm works if all weights are 1.
• Consider jobs in ascending order of finish
time.
• Add job to subset if it is compatible with
previously chosen jobs.
Observation
• Greedy algorithm can fail if arbitrary weights
are allowed.
Weighted Interval Scheduling
Dynamic Programming
Label jobs by finishing time: f1 ≤ f2 ≤ . . . ≤ fn
Define
p(j) = largest index i < j such that job i is
compatible with j
Notation
S[j] = value of optimal solution to the problem
consisting of job requests 1, 2, ..., j.
Weighted Interval Scheduling
Case 1: j is not selected
– must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., j-1
Case2: j is selected.
- must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., p(j)

0, 𝑖𝑓 𝑗 = 0
𝑆𝑗 = ൝
max {𝑣𝑗 + 𝑆 𝑝 𝑗 , 𝑆 𝑗 − 1 }, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Interval Partitioning
Interval Partitioning
Lecture i starts at si and finishes at fi.
Goal:
Find minimum number of classrooms to schedule all
lectures so that no two occur at the same time in
the same room.
Answer
• Depth - Given a set of intervals, the depth of this
set is the maximum number of open intervals that
contain a time t.
Interval Partitioning
Lemma
In any instance of interval partitioning we need at
least depth many classrooms to schedule these
courses.
Proof
This is simply because by definition of depth there is
a time t and depth many courses that are all running
at time t. That means that these courses are mutually
in-compatible, i.e., no two of them can be scheduled
at the same classroom. So, in any schedule we would
need depth many classrooms.
Greedy Strategies
Greedy Strategies:
Greedy 1: Pick the shortest activity
- ascending order of fj - sj
Greedy 2: Pick the activity that starts first,
ascending order of sj
Greedy 3: Pick the activity that ends first
ascending order of fj
Greedy algorithm
Greedy algorithm
Consider lectures in increasing order of start time: assign
lecture to any compatible classroom.

Implementation : O(n log n)


• For each classroom k, maintain
the finish time of the last job
added.
• Keep the classrooms in a priority
queue.
Greedy Theorem
Theorem: Greedy algorithm is optimal.
Proof:
Suppose that the greedy algorithm allocates d
classrooms.
• Enough to prove that d ≤ depth.
By the lemma, depth ≤ OPT. So, putting these
together we get d ≤ OPT.
On the other hand, by definition of OPT, we know
OPT ≤ d.
So, we must have d = OPT.
Proof
To show d ≤ depth, by definition of depth, it is enough to find a
time t* such that more than d open intervals contain t*.
Let t be the time that we allocate the d-th classroom. At this time
we were suppose to schedule, say j-th, course but all classrooms
were already occupied so greedy had to allocate the d-th
classroom.
Observe that, by description of the algorithm, every course we
have schedule so far must start before s(j).
Furthermore, all classrooms are occupied at time t there must be
d − 1 courses which are still running.
Now, let t* := t+ where  > 0 is chosen small enough such that
none of those d−1 jobs together with job j end before or at t*.
But then we have d running courses at time t* and this implies
depth ≥ d.
Graph Problems
Interval scheduling and Interval partitioning can be
seen as graph problems
Input
➢ Graph 𝐺 = (𝑉, 𝐸)
➢ Vertices 𝑉 = jobs/lectures
➢ Edge 𝑖,𝑗 ∈ 𝐸 if jobs/lectures 𝑖 and 𝑗 are incompatible
Interval scheduling
 Maximum independent set?
Interval partitioning
 Graph Colouring?

You might also like