Lecture 14
Lecture 14
Lecture No. 14
1
Scheduling Problems
1. Activity Selection Problem/Interval Scheduling
Problem
2. Interval Partitioning
3. Minimize time in the system
4. Scheduling with deadlines
5. Weighted interval scheduling problem
Note:
• Greedy Algorithm for Problems 1 – 4
• DP Solution for Problem 5
Scheduling to minimizing lateness
Scheduling to minimizing lateness:
• Single resource processes one job at a time.
• Job j requires tj units of processing time and is
due at time dj
• If j starts at time sj , it finishes at time fj = sj + tj
Lateness: lj = max {0, fj – dj }
Goal:
Schedule all jobs to minimize maximum lateness
𝐿 = 𝑚𝑎𝑥𝑗 𝑙𝑗
Example
Example:
Greedy Algorithm
EARLIEST-DEADLINE-FIRST
SORT jobs by due times and renumber so that
d1 ≤ d2 ≤ … ≤ dn
Theorem: The earliest-deadline-first schedule is optimal.
Claims:
1. There exists an optimal schedule with no idle time
2. The earliest-deadline-first schedule has no idle time
Def. Given a schedule S, an inversion is a pair of jobs i and j such that: i < j but
j is scheduled before i.
3. The earliest-deadline-first schedule is the unique idle-free schedule
with no inversions.
4. If an idle-free schedule has an inversion, then it has an adjacent
inversion.
5. Exchanging two adjacent, inverted jobs i and j reduces the number
of inversions by 1 and does not increase the max lateness.
Greedy - Exercise
Maximize the payoff
Suppose you are given two sets A and B, each
containing n positive integers. You can choose to
reorder each set however you like. After reordering,
let ai be the ith element of set A, and let bi be
the ith element of set B. You then receive a payoff
ς𝒏𝒊=𝟏 𝒂𝒊 𝒃𝒊 . Give an algorithm that will maximize
your payoff. Prove that your algorithm maximizes
the payoff, and state its running time.
Coding
Suppose that we have a 100000 character data file that
we wish to store. The file contains only 6 characters,
appearing with the following frequencies:
a b c d e f
Frequency in thousands 45 13 12 16 9 5
𝐵 𝑇 = 𝑐. 𝑓𝑟𝑒𝑞. 𝑑 𝑇 (𝑐)
𝑐 ∈𝐶
which is defined as cost of the tree T
Full Binary Tree
Key Idea:
An optimal code for a file is always represented by a
full binary tree.
Full binary tree is a binary tree in which every non-leaf
node has two children
Proof: If some internal node had only one child then
we could simply get rid of this node and replace it with
its unique child. This would decrease the total cost of
the encoding.
Greedy Choice Property
Lemma:
Consider the two letters, x and y with the
smallest frequencies. Then there exists an
optimal code tree in which these two letters are
sibling leaves in the tree at the lowest level
Proof (Idea)
Take a tree T representing arbitrary optimal
prefix code and modify it to make a tree
representing another prefix code such that the
resulting tree has the required greedy property.
Greedy Choice Property
Let T be an optimum prefix code tree, and let b and c
be two siblings at the maximum depth of the tree
(must exist because T is full).
Assume without loss of generality that f(b) ≤ f(c) and
f(x) ≤ f(y)
Since x and y have the two smallest frequencies it
follows that f(x) ≤ f(b) and f(y) ≤ f(c)
Since b & c are at the deepest level of the tree d(b) ≥
d(x) and d(c) ≥ d(y)
Now switch the positions of x and b in the tree
resulting in new tree T’
Proof
Example Continued
Huffman Code - Example
Example Continued