Greedy Algorithms - II
Huffman Codes & Data Compression
Huffman Codes & Data Compression
Variable length encoding schemes
Huffman Codes & Data Compression
Pre x Code :
Pre x Code for a set S of letters is a function
n
γ : S → {0,1} such that for all x, y ∈ S , x ≠ y, γ(x) is
not a pre x of γ(y).
fi
fi
fi
Huffman Codes & Data Compression
Optimal Pre x Code :
For a letter x ∈ S , let fx represent its frequency, the
fraction of the letters in the text that is equal to x.
∑
fx = 1
x∈S
fi
Huffman Codes & Data Compression
Optimal Pre x Code :
For a letter x ∈ S, let fx represent its frequency, the fraction of the letters
in the text that is equal to x.
∑
fx = 1
x∈S
∑
encoding length = nfx | γ(x) |
x∈S
Average number of bits per letter
∑
ABL(γ) = f(x) | γ(x) |
x∈S
An optimal Pre x Code is one that minimises ABL
fi
fi
Huffman Codes & Data Compression
Optimal Pre x Code and binary trees :
fi
Huffman Codes & Data Compression
Optimal Pre x Code and binary trees :
Optimal Pre x Code ==>
∑
binary tree that minimises f(x)depth(x)
x∈S
fi
fi
Huffman Codes & Data Compression
The binary tree corresponding to the optimal pre x code
is full.
fi
Huffman Codes & Data Compression
Let u, v ∈ S.
Let depth(u) < depth(v). Then fu ≥ fv .
Huffman Codes & Data Compression
There is an optimal pre x code, with corresponding tree
T*, in which the two lowest frequency letters are assigned
to leaves that are siblings in T*.
fi
Huffman_Code(S)
If |S|=2
encode one using 0 and other using 1
else
Let y*, z* - lowest frequency letters
S’ = S \{y*,z*} U {w}
fw = fy* + fz*
T’ = Huffman_Code(S’)
In T’, take the leaf labeled with w and add two children
labelled y*, z*
proof of optimality:
ABL(T’) = ABL(T) +fw
proof of optimality:
Running Time :
Scheduling to minimise lateness
• A single resource and a set of n requests to use the resource
• Each request has a deadline di and needs contiguous time
interval of time ti
• Assign non-overlapping intervals for requests
• A request i is assigned the interval [si, fi]
• i is said to be late if fi > d
• lateness = fi − d
• Schedule all intervals such that the maximum lateness is
minimised
Scheduling to minimise lateness
• Greedy choices
Scheduling to minimise lateness
• Earliest deadline rst
fi
Scheduling to minimise lateness
• Earliest deadline rst
Order the jobs in order of their deadlines
f=s
For i=1 to n
si = f
fi = f + ti
f = fi
fi
Scheduling to minimise lateness
• Earliest deadline rst
Order the jobs in order of their deadlines
f=s
For i=1 to n
si = f
fi = f + ti
f = fi
Running Time?
fi
Scheduling to minimise lateness
There is an optimal schedule with no idle
time.
Scheduling to minimise lateness
Inversion in a schedule A’ : If a job i is scheduled before a
job j with dj < di
Scheduling to minimise lateness
Inversion in a schedule A’ : If a job i is scheduled before a
job j with dj < di
The greedy schedule has no inversions.
Scheduling to minimise lateness
Inversion in a schedule A’ : If a job i is scheduled before a job j
with dj < di
The greedy schedule has no inversions.
Proof by Exchange : We’ll convert an optimal schedule into a
schedule without inversions and not increasing the maximum
lateness.