0% found this document useful (0 votes)
24 views17 pages

Lect18 19

The document discusses two algorithms: job sequencing with deadlines and Huffman coding. It provides details on how the greedy algorithm works for job sequencing problems with deadlines. It also explains how Huffman coding achieves data compression by assigning shorter binary codes to more frequent symbols and constructing the Huffman tree. Applications of Huffman coding in file formats like MP3 and JPEG are also mentioned.

Uploaded by

Hare Krishna Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

Lect18 19

The document discusses two algorithms: job sequencing with deadlines and Huffman coding. It provides details on how the greedy algorithm works for job sequencing problems with deadlines. It also explains how Huffman coding achieves data compression by assigning shorter binary codes to more frequent symbols and constructing the Huffman tree. Applications of Huffman coding in file formats like MP3 and JPEG are also mentioned.

Uploaded by

Hare Krishna Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

05/25/2024 DISCOVER . LEARN . EMPOWER


1
Content

Greedy algorithms for Job Sequencing Problems with the deadline

Huffman Coding

2
JOB SEQUENCING WITH DEADLINES

There is set of n-jobs. For any job i, is a integer deadling di≥0 and profit Pi>0, the
profit Pi is earned iff the job completed by its deadline.
•To complete a job one had to process the job on a machine for one unit of time.
Only one machine is available for processing jobs.
•A feasible solution for this problem is a subset J of jobs such that each job in this
subset can be completed by its deadline.
•The value of a feasible solution J is the sum of the profits of the jobs in J, i.e.,
∑ i ∈ jP i
•An optimal solution is a feasible solution with maximum value.
•The problem involves identification of a subset of jobs which can be completed by
its deadline. Therefore the problem suites the subset methodology and can be
solved by the greedy method.

3
JOB SEQUENCING WITH DEADLINES

algorithm js(d, j, n)
//d=dead line, j=subset of jobs ,n🡪=total number of jobs
// d[i]≥1 1 ≤ i ≤ n are the dead lines,
// the jobs are ordered such that p[1]≥p[2]≥---≥p[n]
//j[i] is the ith job in the optimal solution 1 ≤ i ≤ k, k🡪 subset range
{
d[0]=j[0]=0;
j[1]=1;
k=1;
for i=2 to n do{
r=k;
while((d[j[r]]>d[i]) and [d[j[r]]≠r)) do
r=r-1;
if((d[j[r]]≤d[i]) and (d[i]> r)) then
{
for q:=k to (r+1) setp-1 do j[q+1]= j[q];
j[r+1]=i;
k=k+1;
}}
return k; 4
}
Huffman Coding

 Huffman’s algorithm achieves data compression by finding the


best variable length binary encoding scheme for the symbols
that occur in the file to be compressed.
 The more frequently a symbol occurs, the shorter should be the
Huffman binary word representing it.

 The Huffman code is a prefix-free code.


 No prefix of a code word is equal to another codeword.

5
Huffman Coding

 Huffman codes: compressing data (savings of 20% to 90%)


 Huffman’s greedy algorithm uses a table of the frequencies of occurrence of
each character to build up an optimal way of representing each character as a
binary string

6
Huffman Coding

 Assume we are given a data file that contains only 6 symbols, namely a, b, c, d, e, f With
the following frequency table:

 Find a variable length prefix-free encoding scheme that compresses this data file as
much as possible?

7
Huffman Coding

 Left tree represents a fixed length encoding scheme


 Right tree represents a Huffman encoding scheme

8
Huffman Coding

9
Constructing A Huffman Code

// C is a set of n characters

// Q is implemented as a binary min-heap O(n)


Total computation time = O(n lg n)

O(lg n)

O(lg n)

O(lg n)

10
Cost of a Tree T

 For each character c in the alphabet C


 let f(c) be the frequency of c in the file
 let dT(c) be the depth of c in the tree
 It is also the length of the codeword. Why?
 Let B(T) be the number of bits required to
encode the file (called the cost of T)

B(T )   f (c)dT (c)


cC

11
Running time of Huffman's algorithm

 The running time of Huffman's algorithm assumes that Q is


implemented as a binary min-heap.
 For a set C of n characters, the initialization of Q in line 2 can
be performed in O(n) time using the BUILD-MINHEAP
 The for loop in lines 3-8 is executed exactly n - 1 times, and
since each heap operation requires time O(lg n), the loop
contributes O(n lg n) to the running time. Thus, the total
running time of HUFFMAN on a set of n characters is O(n lg
n).

12
Prefix Code

 Prefix(-free) code: no codeword is also a prefix of some other codewords (Un-


ambiguous)
 An optimal data compression achievable by a character code can always be achieved

with a prefix code


 Simplify the encoding (compression) and decoding

 Encoding: abc  0 . 101. 100 = 0101100


 Decoding: 001011101 = 0. 0. 101. 1101  aabe
 Use binary tree to represent prefix codes for easy decoding
 An optimal code is always represented by a full binary tree, in which every non-leaf node
has two children
 |C| leaves and |C|-1 internal nodes Cost:

B (T )   f (c)dT (c) Depth of c (length of the codeword)


cC
Frequency of c

13
Huffman Code

 Reduce size of data by 20%-90% in general

 If no characters occur more frequently than others, then no


advantage over ASCII

 Encoding:
 Given the characters and their frequencies, perform the algorithm and
generate a code. Write the characters using the code

 Decoding:
 Given the Huffman tree, figure out what each character is (possible
because of prefix property)

14
Application on Huffman code

 Both the .mp3 and .jpg file formats use Huffman


coding at one stage of the compression

15
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd edition 2012.
problem, Graph coloring.

Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
THANK YOU

You might also like