Algorithms-I CSC 302 (July-Dec2022 IIITK) L33-L34
Algorithms-I CSC 302 (July-Dec2022 IIITK) L33-L34
CSC 302
SK Hafizul Islam
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 1 / 21
Agenda I
2 Suggested Readings
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 2 / 21
Unit-time Task Scheduling Problem
The problem of scheduling unit-time tasks with deadlines and penalties for a single
processor has the following inputs:
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 3 / 21
Unit-time Task Scheduling Problem
The problem of scheduling unit-time tasks with deadlines and penalties for a single
processor has the following inputs:
a set T = {a1 , a2 , · · · , an } of n unit-time tasks;
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 3 / 21
Unit-time Task Scheduling Problem
The problem of scheduling unit-time tasks with deadlines and penalties for a single
processor has the following inputs:
a set T = {a1 , a2 , · · · , an } of n unit-time tasks;
a set of n deadlines D = {d1 , d2 , · · · , dn }, such that each di satisfies 1 ≤ di ≤ n and task ai
is supposed to finish by time di ; and
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 3 / 21
Unit-time Task Scheduling Problem
The problem of scheduling unit-time tasks with deadlines and penalties for a single
processor has the following inputs:
a set T = {a1 , a2 , · · · , an } of n unit-time tasks;
a set of n deadlines D = {d1 , d2 , · · · , dn }, such that each di satisfies 1 ≤ di ≤ n and task ai
is supposed to finish by time di ; and
a set of n nonnegative weights or penalties P = {p1 , p2 , · · · , pn } such that we incur a
penalty of pi if task ai is not finished by time di , and we incur no penalty if a task finishes by
its deadline.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 3 / 21
Unit-time Task Scheduling Problem
The problem of scheduling unit-time tasks with deadlines and penalties for a single
processor has the following inputs:
a set T = {a1 , a2 , · · · , an } of n unit-time tasks;
a set of n deadlines D = {d1 , d2 , · · · , dn }, such that each di satisfies 1 ≤ di ≤ n and task ai
is supposed to finish by time di ; and
a set of n nonnegative weights or penalties P = {p1 , p2 , · · · , pn } such that we incur a
penalty of pi if task ai is not finished by time di , and we incur no penalty if a task finishes by
its deadline.
We wish to find a schedule for T that minimizes the total penalty incurred for missed
deadlines.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 3 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
Given a finite set T of unit-time tasks, a schedule for T is a permutation of T specifying
the order in which to perform these tasks.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
Given a finite set T of unit-time tasks, a schedule for T is a permutation of T specifying
the order in which to perform these tasks.
The first task in the schedule begins at time 0 and finishes at time 1, the second task
begins at time 1 and finishes at time 2, and so on.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
Given a finite set T of unit-time tasks, a schedule for T is a permutation of T specifying
the order in which to perform these tasks.
The first task in the schedule begins at time 0 and finishes at time 1, the second task
begins at time 1 and finishes at time 2, and so on.
A task is early in T if it finishes before its deadline.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
Given a finite set T of unit-time tasks, a schedule for T is a permutation of T specifying
the order in which to perform these tasks.
The first task in the schedule begins at time 0 and finishes at time 1, the second task
begins at time 1 and finishes at time 2, and so on.
A task is early in T if it finishes before its deadline.
A task is late in T if it finishes after its deadline.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Definition
A unit-time task is a job, such as a program to be run on a computer, that requires exactly
one unit of time to complete.
Given a finite set T of unit-time tasks, a schedule for T is a permutation of T specifying
the order in which to perform these tasks.
The first task in the schedule begins at time 0 and finishes at time 1, the second task
begins at time 1 and finishes at time 2, and so on.
A task is early in T if it finishes before its deadline.
A task is late in T if it finishes after its deadline.
T is in the early-first form if the early tasks precede the late tasks.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 4 / 21
Properties of a Schedule
An arbitrary schedule can always be put into early-first form without changing its
penalty value.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 5 / 21
Properties of a Schedule
An arbitrary schedule can always be put into early-first form without changing its
penalty value.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 5 / 21
*LYHQ D VFKHGXOH 6 'H¿QH
&
Properties of a(DUO\
Schedule
D WDVN LV HDUO\ LQ 6 LI LW ¿QLVKHV EHIRUH LWV GHDGOLQH L
/DWH D WDVN LV ODWH LQ 6 LI LW ¿QLVKHV DIWHU LWV GHDGOLQH D
0DWURLG
8QLW7LPH 7DVN 6FKHGXOLQJ
*UHHG\ $OJRULWKP RQ 0DWURLG
*UHHG\ $SSURDFK
7DVN 6FKHGXOLQJ 3UREOHP
$Q ([DPSOH 5HG
$UELWUDU\
)RUP
G G G G
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 5 / 21
*LYHQ D VFKHGXOH 6 'H¿QH
&
Properties of a(DUO\
Schedule
D WDVN LV HDUO\ LQ 6 LI LW ¿QLVKHV EHIRUH LWV GHDGOLQH L
/DWH D WDVN LV ODWH LQ 6 LI LW ¿QLVKHV DIWHU LWV GHDGOLQH D
Task a
;$OJRULWKP#6-78 i a1 ;LDRIHQJ
a2 *DR a3 *UHHG\a4$OJRULWKPa5DQG 0DWURLG
a6 a7 ;
Deadline d*UHHG\
i 4 20DWURLG 4 8QLW7LPH
$OJRULWKP RQ 0DWURLG
3 7DVN 6FKHGXOLQJ
1 4 6
*UHHG\ $SSURDFK
Penalties pi 7DVN70
6FKHGXOLQJ 3UREOHP
60 50 40 30 20 10
$Q ([DPSOH 5HG
$UELWUDU\
)RUP
G G G G
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 5 / 21
*LYHQ D VFKHGXOH 6 'H¿QH
&
Properties of a(DUO\
Schedule
D WDVN LV HDUO\ LQ 6 LI LW ¿QLVKHV EHIRUH LWV GHDGOLQH L
/DWH D WDVN LV ODWH LQ 6 LI LW ¿QLVKHV DIWHU LWV GHDGOLQH D
Task a
;$OJRULWKP#6-78 i a1 ;LDRIHQJ
a2 *DR a3 *UHHG\a4$OJRULWKPa5DQG 0DWURLG
a6 a7 ;
Deadline d*UHHG\
i 4 20DWURLG 4 8QLW7LPH
$OJRULWKP RQ 0DWURLG
3 7DVN 6FKHGXOLQJ
1 4 6
*UHHG\ $SSURDFK
Penalties pi 7DVN70
6FKHGXOLQJ 3UREOHP
60 50 40 30 20 10
$Q ([DPSOH 5HG
A schedule T = {a3 , a2 } can also be schedule as T = {a2 , a3 }.
$UELWUDU\
)RUP
G G G G
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 5 / 21
Properties of a Schedule
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
monotonically increasing deadlines.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Properties of a Schedule
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
monotonically increasing deadlines.
First put the schedule T into early-first form.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Properties of a Schedule
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
monotonically increasing deadlines.
First put the schedule T into early-first form.
If there exist two early tasks ai and aj finishing at respective times k and k + 1 in the schedule
such that dj < di , then swap the positions of ai and aj .
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Properties of a Schedule
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
monotonically increasing deadlines.
First put the schedule T into early-first form.
If there exist two early tasks ai and aj finishing at respective times k and k + 1 in the schedule
such that dj < di , then swap the positions of ai and aj .
Since aj is early before the swap, k + 1 ≤ dj . Therefore, k + 1 < di , and so ai is still early after
the swap. Because task aj is moved earlier in the schedule, it remains early after the swap.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Properties of a Schedule
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
monotonically increasing deadlines.
First put the schedule T into early-first form.
If there exist two early tasks ai and aj finishing at respective times k and k + 1 in the schedule
such that dj < di , then swap the positions of ai and aj .
Since aj is early before the swap, k + 1 ≤ dj . Therefore, k + 1 < di , and so ai is still early after
the swap. Because task aj is moved earlier in the schedule, it remains early after the swap.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Properties of a Schedule 0DWURLG
8QLW7LPH 7DVN 6FKHGXOLQJ
*UHHG\ $OJRULWKP RQ 0DWURLG
*UHHG\ $SSURDFK
7DVN 6FKHGXOLQJ 3UREOHP
3URSHUWLHV RI D 6FKHGXOH
Canonical Form: An arbitrary schedule can always be transformed into canonical form,
in which (i) the early tasks precede the late tasks, and (ii) are scheduled in order of
&DQRQLFDOdeadlines.
monotonically increasing )RUP $Q DUELWUDU\ VFKHGXOH FDQ DOZD\V EH WUDQVIRUPHG
First put theLQWR FDQRQLFDO
schedule IRUPearly-first
T into LQ ZKLFK WKH HDUO\ WDVNV SUHFHGH WKH ODWH WDVNV
form.
If there exist DQG
twoDUH VFKHGXOHG
early tasks aLQ RUGHU
i and aj RIfinishing
PRQRWRQLFDOO\ LQFUHDVLQJ
at respective GHDGOLQHV
times k and k + 1 in the schedule
such that dj < di , then swap the positions of ai and aj .
V
Since aj )LUVW SXW before
is early WKH VFKHGXOH LQWRkHDUO\¿UVW
the swap, + 1 ≤ dj .IRUP
Therefore, k + 1 < di , and so ai is still early after
the swap. Because
7KHQ taskSRVLWLRQ
VZDS WKH aj is moved
RI DQ\earlier in the schedule,
FRQVHFXWLYH HDUO\ WDVNVit Dremains
DQG D early
LI after the swap.
L M
W IRUP GM > GL EXW DM DSSHDUV EHIRUH DL
GHDGOLQHRIDM GHDGOLQHRIDM
DM DL DL DM
WLPH WLPH
GHDGOLQHRIDL GHDGOLQHRIDL
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 6 / 21
Reduction
The search for an optimal schedule T thus reduces to finding a set of tasks A ⊆ T that
we assign to be early in the optimal schedule.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 7 / 21
Reduction
The search for an optimal schedule T thus reduces to finding a set of tasks A ⊆ T that
we assign to be early in the optimal schedule.
To determine A, we can create the actual schedule by listing the elements of A in order of
monotonically increasing deadlines, then listing the late tasks (i.e., T − A) in any
order, producing a canonical ordering of the optimal schedule.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 7 / 21
Independence
Independent task: A set of tasks A is independent if there exists a schedule for these
tasks without penalty.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 8 / 21
Independence
Independent task: A set of tasks A is independent if there exists a schedule for these
tasks without penalty.
Clearly, the set of early tasks for a schedule forms an independent set of tasks.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 8 / 21
Independence
Independent task: A set of tasks A is independent if there exists a schedule for these
tasks without penalty.
Clearly, the set of early tasks for a schedule forms an independent set of tasks.
Let A denote the set of all independent sets of tasks. For t = 0, 1, 2, · · · , n, let Nt (A)
denote the number of tasks in A whose deadline is t or earlier.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 8 / 21
Independence
Independent task: A set of tasks A is independent if there exists a schedule for these
tasks without penalty.
Clearly, the set of early tasks for a schedule forms an independent set of tasks.
Let A denote the set of all independent sets of tasks. For t = 0, 1, 2, · · · , n, let Nt (A)
denote the number of tasks in A whose deadline is t or earlier.
Note that N0 (A) = 0 for any set A.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 8 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
3 If the tasks in A are scheduled in order of monotonically increasing deadlines, then no task is late.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
3 If the tasks in A are scheduled in order of monotonically increasing deadlines, then no task is late.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
3 If the tasks in A are scheduled in order of monotonically increasing deadlines, then no task is late.
Proof.
¬(2) ⇒ ¬(1) : If Nt (A) > t for some t, then there is no way to make a schedule with no late tasks
for set A, because more than t tasks must finish before time t. Therefore, (1) ⇒ (2).
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
3 If the tasks in A are scheduled in order of monotonically increasing deadlines, then no task is late.
Proof.
¬(2) ⇒ ¬(1) : If Nt (A) > t for some t, then there is no way to make a schedule with no late tasks
for set A, because more than t tasks must finish before time t. Therefore, (1) ⇒ (2).
(2) ⇒ (3) : If (2) holds, then (3) must follow: there is no way to “get stuck” when scheduling the
tasks in order of monotonically increasing deadlines, since (2) implies that the ith largest deadline
is at least i.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Independence
Lemma 1
For any set of tasks A, the following statements are equivalent.
1 The set A is independent.
2 For t = 0, 1, 2, · · · , n, we have Nt (A) ≤ t.
3 If the tasks in A are scheduled in order of monotonically increasing deadlines, then no task is late.
Proof.
¬(2) ⇒ ¬(1) : If Nt (A) > t for some t, then there is no way to make a schedule with no late tasks
for set A, because more than t tasks must finish before time t. Therefore, (1) ⇒ (2).
(2) ⇒ (3) : If (2) holds, then (3) must follow: there is no way to “get stuck” when scheduling the
tasks in order of monotonically increasing deadlines, since (2) implies that the ith largest deadline
is at least i.
(3) ⇒ (1) is trivial.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 9 / 21
Greedy Approach
Use the previous lemma, we can easily compute whether or not a given set of tasks is
independent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 10 / 21
Greedy Approach
Use the previous lemma, we can easily compute whether or not a given set of tasks is
independent.
The problem of minimizing the sum of the penalties of the late tasks is the same as the
problem of maximizing the sum of the profits of the early tasks.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 10 / 21
Matroid Theorem
Theorem 1
Let T be a set of unit-time tasks with deadlines and I the set of all independent tasks of T .
Then (T , I) is a matroid.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 11 / 21
Matroid Theorem
Proof.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
Let ai ∈ B − A with deadline k + 1.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
Let ai ∈ B − A with deadline k + 1.
Let A′ = A ∪ {ai }.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
Let ai ∈ B − A with deadline k + 1.
Let A′ = A ∪ {ai }.
For 0 ≤ t ≤ k, we have Nt (A′ ) = Nt (A) ≤ t, since A is independent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
Let ai ∈ B − A with deadline k + 1.
Let A′ = A ∪ {ai }.
For 0 ≤ t ≤ k, we have Nt (A′ ) = Nt (A) ≤ t, since A is independent.
For k < t ≤ n, we have Nt (A′ ) ≤ Nt (B) ≤ t, since B is independent.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Matroid Theorem
Proof.
Hereditary: Every subset of an independent set of tasks is certainly independent.
Exchange Property: Consider two independent sets A, B ∈ I with |A| < |B|
Let k be the largest t such that Nk (A) ≥ Nk (B).
Since Nn (A) = |A| and Nn (B) = |B|, but |A| < |B|, we must have that k < n and Nj (A) <
Nj (B) for k + 1 ≤ j ≤ n.
Therefore, B contains more tasks with deadline k + 1 than A does.
Let ai ∈ B − A with deadline k + 1.
Let A′ = A ∪ {ai }.
For 0 ≤ t ≤ k, we have Nt (A′ ) = Nt (A) ≤ t, since A is independent.
For k < t ≤ n, we have Nt (A′ ) ≤ Nt (B) ≤ t, since B is independent.
Therefore, A′ is independent, and thus (T , I) is a matroid.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 12 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
4 Find the latest available index k ≤ di ▷ use any searching algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
4 Find the latest available index k ≤ di ▷ use any searching algorithm
5 if (k ≥ 0) then
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
4 Find the latest available index k ≤ di ▷ use any searching algorithm
5 if (k ≥ 0) then
6 A := A ∪ {ai }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
4 Find the latest available index k ≤ di ▷ use any searching algorithm
5 if (k ≥ 0) then
6 A := A ∪ {ai }
7 Mark index k unavailable ▷ task ak is scheduled
8 end
9 end
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
Algorithm: GREEDY-MAX-TASK(T , d, p)
1 A := ϕ
2 Sort the elements of T into monotonically decreasing order by penalties. Assume that
|T | = n and the ordering is p1′ ≥ p2′ ≥ · · · ≥ pn′
3 for i ← 1 to n do
4 Find the latest available index k ≤ di ▷ use any searching algorithm
5 if (k ≥ 0) then
6 A := A ∪ {ai }
7 Mark index k unavailable ▷ task ak is scheduled
8 end
9 end
10 Return(A) ▷ the set of early tasks
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 13 / 21
Greedy Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 14 / 21
Greedy Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 14 / 21
Greedy Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 14 / 21
Greedy Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 14 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
Sort the elements of T into monotonically decreasing order by penalties p.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
Sort the elements of T into monotonically decreasing order by penalties p.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
An instance of the problem of scheduling unit-time tasks with deadlines and penalties for
a single processor.
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
Sort the elements of T into monotonically decreasing order by penalties p.
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 15 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 16 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 1, d1 = 4, schedule a1
1a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 16 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 1, d1 = 4, schedule a1
1a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d1 = 4, so we can put a1 in early-first form, and k = 3 , i.e., k ≤ di
1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a1 }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 16 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 1, d1 = 4, schedule a1
1a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d1 = 4, so we can put a1 in early-first form, and k = 3 , i.e., k ≤ di
1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a1 }
For i = 2, d2 = 2, schedule a2
2a 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 16 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 1, d1 = 4, schedule a1
1a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d1 = 4, so we can put a1 in early-first form, and k = 3 , i.e., k ≤ di
1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a1 }
For i = 2, d2 = 2, schedule a2
2a 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d2 = 2, so we can put a2 in early-first form, and k = 1 , i.e., k ≤ di .
2 a 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a2 , a1 }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 16 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 17 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 3, d3 = 4, schedule a3
3a a
2 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 17 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 3, d3 = 4, schedule a3
3a 2 a 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d3 = 4, so we can put a3 in early-first form, and k = 2 , i.e., k ≤ di
2 a 3 a
1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a2 , a3 , a1 }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 17 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
For i = 3, d3 = 4, schedule a3
a
3 2 a 1 a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7
But d3 = 4, so we can put a3 in early-first form, and k = 2 , i.e., k ≤ di
2 a
3 1 a a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a2 , a3 , a1 }
For i = 4, d4 = 3, and k = 0 , k ≤ di , schedule a4
4a 2 a3 1 a a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a4 , a2 , a3 , a1 }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 17 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 18 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 19 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
The final schedule
4a 2 a3 1 a 7 a5 a 6 a a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a4 , a2 , a3 , a1 , a7 , a5 , a6 }
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 19 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
The final schedule
4a 2 a3 1 a 7 a5 a 6 a a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a4 , a2 , a3 , a1 , a7 , a5 , a6 }
The optimal (maximum) profit is
p1 + p2 + p3 + p4 + p7 = 70 + 60 + 50 + 40 + 10 = 230
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 19 / 21
An Example
Task ai a1 a2 a3 a4 a5 a6 a7
Deadline di 4 2 4 3 1 4 6
Penalties pi 70 60 50 40 30 20 10
The final schedule
4a 2 a3 1 a 7 a5 a 6 a a
0 7−→ 1 7−→ 2 7−→ 3 7−→ 4 7−→ 5 7−→ 6 7−→ 7 A = {a4 , a2 , a3 , a1 , a7 , a5 , a6 }
The optimal (maximum) profit is
p1 + p2 + p3 + p4 + p7 = 70 + 60 + 50 + 40 + 10 = 230
The optimal (minimum) penalty is
p5 + p6 = 50
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 19 / 21
Suggested Readings
Chapter 16, Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest and Clifford Stein,
Introduction to Algorithms, 3rd Edition, MIT Press/McGraw-Hill, 2009.
Chapter 04, E. Harowitz, S. Sahani, and S. Rajasekaran, Fundamentals of Computer
Algorithms, 2nd Edition, University Press, 2008.
Chapter 10, Michael T. Goodrich and Roberto Tamassia, Algorithm Design and Appli-
cations, Wiley, 2014.
https://www.youtube.com/watch?v=a5W3oQ8gdtw
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 20 / 21
Thank You
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I September 26, 2022 21 / 21