Unite 4-Greedy Method - CSE
Unite 4-Greedy Method - CSE
Algorithms(UCBC0501)
Prepared By
Mrs Madhuri V. Thorwat
Asst. Prof - Dept Of CSBE - KITCOEK
Unit 4: Greedy Method
General method
Knapsack Problem
Coin Change Problem
Knapsack Problem,
Job sequencing with deadlines.
Optimal Storage on tape, Optimal Merge Pattern.
Optimal Tree problem: Huffman Trees and Codes.
Introduction
Coin Change Problem
Example
Knapsack Problem
Problem 1
In Method 2 Only change step 3 as follows:
Solution
Q3. Consider following instance of Knapsack Problem : n=3,
m= 20, (p1,p2,p3)= (25,24,15) and (w1, w2, w3)=(18,15,10).
Find Out optimal solution using greedy Approach.
Optimal
Storage On
Tape
34
Minimum
Retrieval Time
Optimal Merge Pattern
• Refer Problems from
Notebook
Refer Examples Given in Notebook
Huffman Code
(Kindly refer Notes and Problems Given in Notebook.)
Suppose we have to encode a text that comprises symbols from some n-
symbol alphabet by assigning to each of the text’s symbols some sequence of bits
called the codeword. For example, we can use a fixed-length encoding that
assigns to each symbol a bit string of the same length m (m ≥log2 n). This is
exactly what the standard ASCII code does. One way of getting a coding scheme
that yields a shorter bit string on the average is based on the old idea of assigning
shorter code words to more frequent symbols and longer codewords to less
frequent symbols. This idea was used, in particular, in the telegraph code
invented in the mid-19th century by Samuel Morse. In that code, frequent letters
such as e(.) and a(.−) are assigned short sequences of dots and dashes while
infrequent letters such as q (−−.−)andz(−−..) have longer ones
• Variable-length encoding, which assigns codewords of different lengths to
different symbols, introduces a problem that fixed-length encoding does not
have. Namely, how can we tell how many bits of an encoded text represent
the first (or, moregenerally,the ith)symbol? To avoid this complication, we
can limit our selves to the so-called prefix-free (or simply prefix) codes. In a
prefix code, no codeword is a prefix of a codeword of another symbol.
Hence, with such an encoding, we can simply scan a bit string until we get
the first group of bits that is a codeword for some symbol, replace these
bits by this symbol, and repeat this operation until the bit string’s end is
reached. If we want to create a binary prefix code for some alphabet, it is
natural to associate the alphabet’s symbols with leaves of a binary tree in
which all the left edges are labeled by 0 and all the right edges are labeled
by 1. The codeword of a symbol can then be obtained by recording the
labels on the simple path from the root to the symbol’s leaf. Since there is
no simple path to a leaf that continues to another leaf, no codeword can be
a prefix of another codeword; hence, any such tree yields a prefix code.
Huffman’s algorithm
• Step 1
Initialize n one-node trees and label them with the symbols of the
alphabet given. Record the frequency of each symbol in its tree’s
root to indicate the tree’s weight. (More generally, the weight of a
tree will be equal to the sum of the frequencies in the tree’s leaves.)
• Step 2
Repeat the following operation until a single tree is obtained. Find
two trees with the smallest weight (ties can be broken arbitrarily,
but see Problem2inthissection’sexercises). Make them the left and
right subtree of a new tree and record the sum of their weights in
the root of the new tree as its weight.
A tree constructed by the above algorithm is called a Huffman tree.
It defines—in the manner described above—a Huffman code
• Example- Consider the five-symbol alphabet {A, B, C, D,_} with the following occurrence
frequencies in a text made up of these symbols:
The Huffman
tree
construction
for this input
is shown:
• lengths obtained, the average number of bits per symbol in this code is
= 2 . 0.35+3.0.1+2.0.2+2.0.2+3.0.15=2.25
Huffman's encoding of the text will use 25% less memory than its fixed-length encoding.
Huffman’s encoding is one of the most important file-compression methods. In addition to its simplicity and versatility, it yields
an optimal, i.e., minimal-length, encoding (provided the frequencies of symbol occurrences are independent and known in advance).
The simplest version of Huffman compression calls, in fact, for a preliminary scanning of a given text to count the frequencies of
symbol occurrences in it. Then these frequencies are used to construct a Huffman coding tree and encode the text as described above.
A Huffman tree is a binary tree that minimizes the weighted path length from the root to the leaves of predefined weights.
The most important application of Huffman trees is Huffman codes.
A Huffman code is an optimal prefix-free variable-length encoding scheme that assigns bit strings to symbols based on their
frequencies in a given text. This is accomplished by a greedy construction of a binary tree whose leaves represent the alphabet
symbols and whose edges are labeled with 0’s and 1’s
• Practice Example 1.
a. Construct a Huffman code for the following data:
symbol - A B C D _
frequency - 0.4 ,0.1, 0.2 ,0.15 ,0.15
b. Encode ABACABAD using the code of question (a).
c. Decode 100010111001010 using the code of question (a)