UNIT III
UNIT III DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE : Dynamic
programming – Principle of optimality - Coin changing problem, Longest Common
Subsequence, Computing a Binomial Coefficient – Floyd‘s algorithm – Multi stage graph -
Optimal Binary Search Trees – Knapsack Problem and Memory functions. Greedy Technique -
Prim‘s algorithm, Kruskal's Algorithm, and Dijkstra’s Algorithm - Huffman Trees and Codes
3.1 Introduction to Dynamic Programming
3.1.1 Difference between Divide and Conquer and Dynamic Programming
Longest Common Subsequence
Here longest means that the subsequence should be the biggest one. The common means that
some of the characters are common between the two strings. The subsequence means that some
of the characters are taken from the string that is written in increasing order to form a
subsequence.
Let's understand the subsequence through an example.
Suppose we have a string 'w'.
W1 = abcd
The following are the subsequences that can be created from the above string:
o ab
o bd
o ac
o ad
o acd
o bcd
The above are the subsequences as all the characters in a sub-string are written in increasing
order with respect to their position. If we write ca or da then it would be a wrong subsequence as
characters are not appearing in the increasing order. The total number of subsequences that
would be possible is 2n, where n is the number of characters in a string. In the above string, the
value of 'n' is 4 so the total number of subsequences would be 16.
W2= bcd
By simply looking at both the strings w1 and w2, we can say that bcd is the longest common
subsequence. If the strings are long, then it won't be possible to find the subsequence of both the
string and compare them to find the longest common subsequence.
Finding LCS using dynamic programming with the help of a table.
Consider two strings:
X= a b a a b a
Y= b a b b a b
(a, b)
For index i=1, j=1
Since both the characters are different so we consider the maximum value. Both contain the same
value, i.e., 0 so put 0 in (a,b). Suppose we are taking the 0 value from 'X' string, so we put arrow
towards 'a' as shown in the above table.
(a, a)
For index i=1, j=2
In this way, we will find the complete table. The final table would be:
In the above table, we can observe that all the entries are filled. Now we are at the last
cell having 4 value. This cell moves at the left which contains 4 value.; therefore, the first
character of the LCS is 'a'. The left cell moves upwards diagonally whose value is 3;
therefore, the next character is 'b' and it becomes 'ba'. Now the cell has 2 value that
moves on the left. The next cell also has 2 value which is moving upwards; therefore, the
next character is 'a' and it becomes 'aba'. The next cell is having a value 1 that moves
upwards. Now we reach the cell (b, b) having value which is moving diagonally upwards;
therefore, the next character is 'b'. The final string of longest common subsequence is
'baba'.
Algorithm of Longest Common Subsequence
Suppose X and Y are the two given sequences
Initialize a table of LCS having a dimension of [Link] * [Link]
[Link] = X
[Link] = Y
LCS[0][] = 0
LCS[][0] = 0
Loop starts from the LCS[1][1]
Now we will compare X[i] and Y[j]
if X[i] is equal to Y[j] then
LCS[i][j] = 1 + LCS[i-1][j-1]
Point an arrow LCS[i][j]
Else LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])
3.5 Floyd’s Algorithm
Solved Examples:
1. Solve All-Pair Shortest Problem for the weight matrix given below
2) Solve the all-pairs shortest path problem for the digraph with the following weight matrix
3.8
Huffman Trees and Code