Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Lecture 14

1. The document discusses various scheduling problems including activity selection, interval partitioning, minimizing time in a system, and scheduling with deadlines. 2. It describes the greedy algorithm Earliest Deadline First for scheduling jobs to minimize lateness, where jobs have processing times and deadlines. 3. The document also covers Huffman coding, an optimal variable length coding technique that encodes characters using binary codewords. It uses a greedy approach of repeatedly merging the two lowest frequency characters.

Uploaded by

f20201862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture 14

1. The document discusses various scheduling problems including activity selection, interval partitioning, minimizing time in a system, and scheduling with deadlines. 2. It describes the greedy algorithm Earliest Deadline First for scheduling jobs to minimize lateness, where jobs have processing times and deadlines. 3. The document also covers Huffman coding, an optimal variable length coding technique that encodes characters using binary codewords. It uses a greedy approach of repeatedly merging the two lowest frequency characters.

Uploaded by

f20201862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

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

A binary code encodes each character as a binary string


or code word.
Goal: We would like to find a binary code that encodes
the file using as few bits as possible
Fixed Length coding
In a fixed-length code each code word has the same
length.
For our example, a fixed-length code must have at least
3 bits per code word. One possible coding is as follows:
a b c d e f
Frequency in thousands 45 13 12 16 9 5
Fixed length coding 000 001 010 011 100 101

The fixed length code requires 300000 bits to store the


file
variable-length code
In a variable-length code, code words may have
different lengths.
One possible coding for our example is as follows:
a b c d e f
Frequency in thousands 45 13 12 16 9 5
Variable length coding 0 101 100 111 1101 1100

The variable-length code uses only


(45x1+13x3+12x3+16x3+9x4+5x4)x1000= 224000 bits
Much better than fixed length code
Can we do better?
Key Property - Encoding
Key Property
Given an encoded message, decoding is the process of
turning it back into the original message. A message is
uniquely decodable, if it can only be decoded in one way.
Encoding should be done so that message is uniquely
decodable.
Note:
1. Fixed-length codes are always uniquely decodable
2. May not be so for variable length encoding
Example : C = {a= 1; b= 110; c= 10; d= 111}
1101111 is not uniquely decipherable since it could have
encoded either bad or acad.
Variable-length codes may not be uniquely decodable
Prefix Codes
Prefix Code: A code is called a prefix code if no code word
is a prefix of another one.
Example:
{a= 0; b= 110; c= 10; d= 111} is a prefix code.
Important Fact:
Every message encoded by a prefix code is uniquely
decipherable.
Since no code-word is a prefix of any other we can always
find the first code word in a message, peel it off, and
continue decoding.
Example:
0110100 = 0110100 = abca
We are therefore interested in finding good prefix codes.
Representation – Binary Encoding
Representation
Prefix codes (in general any binary encoding) can be
represented as a binary tree.
Left edge is labeled 0 and the right edge is labeled 1
Label of leaf is frequency of character.
Path from root to leaf is code word associated with
character.
Representation – Binary Tree
Fixed-length prefix code from the example
earlier represented as a binary tree (the
numbers inside the nodes are the sum of
frequencies for characters in each subtree):

An ‘optimal’ prefix code from the


example earlier represented as a binary
tree
Cost of Tree
• For each character c in the set C let freq.c denote the
frequency of c in the file
• Given a tree T corresponding to a prefix code
• dT(c) denote the depth of c’s leaf in the tree
• dT(c) is also the length of the codeword for character c
• The number of bits required to encode a file is

𝐵 𝑇 = ෍ 𝑐. 𝑓𝑟𝑒𝑞. 𝑑 𝑇 (𝑐)
𝑐 ∈𝐶
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

Illustration of the proof:


Greedy Choice Property
Since T is optimum
Optimal Substructure Property
Optimal Substructure Property
Lemma:
Let T be a full binary tree representing an optimal
prefix code over an alphabet C, where frequency
f[c] is define for each character c belongs to set C.
Consider any two characters x and y that appear as
sibling leaves in the tree T and let z be their parent.
Then, considering character z with frequency f[z] =
f[x] + f[y], tree T` = T - {x, y} represents an optimal
code for the alphabet C` = C - {x, y}U{z}.
Proof : See CLRS
Huffman Coding
Step1:
Pick two letters x; y from alphabet C with the smallest
frequencies and create a subtree that has these two
characters as leaves.
Label the root of this subtree as z
Step2:
Set frequency f(z) = f(x) +f(y).
Remove x; y and add z creating new alphabet
A* =A ∪ {z} – {x, y}, Note that |A*|= |A| - 1
Repeat this procedure, called merge, with new alpha-
bet A* until an alphabet with only one symbol is left.
Huffman Code Algorithm
Huffman Code Algorithm
Huffman Code - Example
Example:
Huffman Code - Example

Example Continued
Huffman Code - Example

Example Continued

You might also like