Unit Ii Aa CH-1
Unit Ii Aa CH-1
The greedy method is one of the strategies like Divide and conquer used to solve the
problems. This method is used for solving optimization problems. An optimization problem
is a problem that demands either maximum or minimum results. Let's understand through
some terms.
The Greedy method is the simplest and straightforward approach. It is not an algorithm, but
it is a technique. The main function of this approach is that the decision is taken on the
basis of the currently available information. Whatever the current information is present, the
decision is made without worrying about the effect of the current decision in future.
This technique is basically used to determine the feasible solution that may or may not be
optimal. The feasible solution is a subset that satisfies the given criteria. The optimal
solution is the solution which is the best and the most favorable solution in the subset. In the
case of feasible, if more than one solution satisfies the given criteria then those solutions
will be considered as the feasible, whereas the optimal solution is the best solution among
all the solutions.
o To construct the solution in an optimal way, this algorithm creates two sets where
one set contains all the chosen items, and another set contains the rejected
items.
o A Greedy algorithm makes good local choices in the hope that the solution
should be either feasible or optimal.
o Candidate set: A solution that is created from the set is known as a candidate
set.
o Selection function: This function is used to choose the candidate or subset
which can be added in the solution.
o Feasibility function: A function that is used to determine whether the candidate
or subset can be used to contribute to the solution or not.
o Objective function: A function is used to assign the value to the solution or the
partial solution.
o Solution function: This function is used to intimate whether the complete
function has been reached or not.
Huffman Coding
Huffman Coding is a technique of compressing data to reduce its size without
losing any of the details. It was first developed by David Huffman.
Huffman Coding is generally useful to compress the data in which there are
frequently occurring characters.
Initial string
Huffman coding first creates a tree using the frequencies of the character and
then generates code for each character.
Once the data is encoded, it has to be decoded. Decoding is done using the
same tree.
Huffman Coding prevents any ambiguity in the decoding process using the
concept of prefix code ie. a code associated with a character should not be
present in the prefix of any other code. The tree created above helps in
maintaining the property.
Huffman coding is done with the help of the following steps.
Frequency of string
2. Sort the characters in increasing order of the frequency. These are stored in a
4. Create an empty node z . Assign the minimum frequency to the left child of z
and assign the second minimum frequency to the right child of z . Set the
value of the z as the sum of the above two minimum frequencies.
For sending the above string over a network, we have to send the tree as well
as the above compressed-code. The total size is given by the table below.
A 5 11 5*2 = 10
B 1 100 1*3 = 3
C 6 0 6*1 = 6
D 3 101 3*3 = 9
Without encoding, the total size of the string was 120 bits. After encoding the
size is reduced to 32 + 15 + 28 = 75 .
Decoding the code
For decoding the code, we can take the code and traverse through the tree to
find the character.
Let 101 is to be decoded, we can traverse from the root as in the figure below.
Decoding
Let's consider that you have n activities with their start and
finish times, the objective is to find solution set
having maximum number of non-conflicting
activities that can be executed in a single time frame,
assuming that only one person or machine is available for
execution.
Step 2: Select the first activity from sorted array act[] and
add it to sol[] array.
Step 3: Repeat steps 4 and 5 for the remaining activities
in act[].
5 9 a1
1 2 a2
3 4 a3
0 6 a4
5 7 a5
8 9 a6
1 2 a2
3 4 a3
0 6 a4
5 7 a5
5 9 a1
8 9 a6
Step 2: Select the first activity from sorted array act[] and
add it to the sol[] array, thus sol = {a2}.
(3,4)
(5,7)
(8,9)
In the above diagram, the selected activities have been
highlighted in grey.