Adsa-4 Unit
Adsa-4 Unit
Adsa-4 Unit
Dynamic Programming:
Greedy Algorithms
Greedy Algorithm
Examples
Most networking algorithms use the greedy approach. Here is a list of few of
them −
Knapsack Problem
There are lots of similar problems that uses the greedy approach to find an
optimum solution.
1. Algorithm Greedy(a, n)
2. {
3. for i = 1 to n do
4. {
5. x = Select(a);
6. if feasible(x) then
MC4101
7. solution = solution + x;
8. }
9. }
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.
MC4101
It might not be possible to complete all the activities, since their timings
can collapse.
sol[] array refering to the solution set containing the maximum number
of non-conflicting activities.
Following are the steps we will be following to solve the activity selection
problem,
Step 1: Sort the given activities in ascending order according to their finishing
time.
Step 2: Select the first activity from sorted array act[] and add it to sol[] array.
MC4101
Step 4: If the start time of the currently selected activity is greater than or
equal to the finish time of previously selected activity, then add it to
the sol[] array.
Example: Given 6 activities along with their start and end time as
MC4101
Step 2: Select the first activity from sorted array act[] and add it to
the sol[] array, thus sol = {a2}.
Step 3: Repeat the steps 4 and 5 for the remaining activities in act[].
Step 4: If the start time of the currently selected activity is greater than
or equal to the finish time of the previously selected activity, then add it
to sol[].
Step 5: Select the next activity in act[]
For the data given in the above table,
Select activity a3. Since the start time of a3 is greater than the finish
time of a2 (i.e. s(a3) > f(a2)), we add a3 to the solution set. Thus sol =
{a2, a3}.
Select a4. Since s(a4) < f(a3), it is not added to the solution set.
Select a5. Since s(a5) > f(a3), a5 gets added to solution set. Thus sol =
{a2, a3, a5}
Select a1. Since s(a1) < f(a5), a1 is not added to the solution set.
Select a6. a6 is added to the solution set since s(a6) > f(a5). Thus sol =
{a2, a3, a5, a6}.
MC4101
(1,2)
(3,4)
(5,7)
(8,9)
In the above diagram, the selected activities have been highlighted in grey.
MC4101
Huffman Coding.
Prefix Rule-
Huffman Coding implements a rule known as a prefix rule.
This is to prevent the ambiguities while decoding.
It ensures that the code assigned to any character is not a prefix of the code
assigned to any other character.
Major Steps in Huffman Coding-
There are two major steps in Huffman Coding-
Make the first node as a left child and the other node as a right child of the
newly created node.
Step-04:
Keep repeating Step-02 and Step-03 until all the nodes form a single tree.
The tree finally obtained is the desired Huffman Tree.
Time Complexity-
Formula-02:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code length per
character
= ∑ ( frequencyi x Code lengthi )
Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
Solution-
First let us construct the Huffman Tree.
Huffman Tree is constructed in the following steps-
MC4101
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
MC4101
Step-06:
Step-07:
MC4101
Now,
We assign weight to all the edges of the constructed Huffman Tree.
Let us assign weight ‘0’ to the left edges and weight ‘1’ to the right edges.
Rule
If you assign weight ‘0’ to the left edges, then assign weight ‘1’ to the right
edges.
If you assign weight ‘1’ to the left edges, then assign weight ‘0’ to the right
edges.
Any of the above two conventions may be followed.
MC4101
But follow the same convention at the time of decoding that is adopted at the
time of encoding.
After assigning weight to all the edges, the modified Huffman Tree is-
Now, let us answer each part of the given problem one by one-
To write Huffman Code for any character, traverse the Huffman Tree from root
node to the leaf node of that character.
Following this rule, the Huffman Code for each character is-
MC4101
a = 111
e = 10
i = 00
o = 11001
u = 1101
s = 01
t = 11000
Definition It is used to obtain the optimum It is also used to obtain the optimum
solution. solution.
Principle of It guarantees that it will generate It does not guarantee that it will
optimality the optimum solution using the generate the optimum solution.
principle of optimality.
Memoization It creates the lookup table to store It is more efficient in terms of memory
the results of the subproblems as it does not create any table to store
that occupy the memory space. the previous states.
Method The dynamic programming uses The greedy method always computes
the bottom-up or top-down the solution in a sequence manner,
approach by breaking down a and it does not look at the previous
complex problem into simpler states.
problems.
S1 = {B, C, D, A, A, C, D}
If
S1 = {B, C, D, A, A, C, D}
S2 = {A, C, D, B, A, C}
Then, common subsequences are {B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A,
C}, {C, D}, ...
Among these subsequences, {C, D, A, C} is the longest common
subsequence. We are going to find this longest common subsequence using
dynamic programming.
Before proceeding further, if you do not already know about dynamic
programming, please go through dynamic programming.
MC4101
Second Sequence
The following steps are followed for finding the longest common
subsequence.
Initialise a table
2. Fill each cell of the table using the following logic.
3. If the character corresponding to the current row and current column are
matching, then fill the current cell by adding one to the diagonal element. Point
an arrow to the diagonal cell.
MC4101
4. Else take the maximum value from the previous column and previous row
element for filling the current cell. Point an arrow to the cell with maximum
value. If they are equal, point to any of them.
6. The value in the last row and the last column is the length of the longest
common subsequence. The bottom right corner is the length of the LCS
7. In order to find the longest common subsequence, start from the last element
and follow the direction of the arrow. The elements corresponding to () symbol
form the longest common subsequence.
LCS
X.label = X
Y.label = Y
LCS[0][] = 0
LCS[][0] = 0
If X[i] = Y[j]
Else