

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Suppose we put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive? Resulting sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,... where each number is the sum of the two preceding numbers.
This problem was posed by Leonardo Pisano, better known by his nickname Fibonacci (son of Bonacci, born 1170, died 1250). This problem and many others were in posed in his book Liber abaci , published in 1202. The book was based on the arithmetic and algebra that Fibonacci had accumulated during his travels. The book, which went on to be widely copied and imitated, introduced the Hindu-Arabic place-valued decimal system and the use of Arabic numerals into Europe. The rabbits problem in the third section of Liber abaci led to the introduction of the Fibonacci numbers and the Fibonacci sequence for which Fibonacci is best remembered today.
This sequence, in which each number is the sum of the two preceding numbers, has proved extremely fruitful and appears in many different areas of mathematics and science. The Fibonacci Quarterly is a modern journal devoted to studying mathematics related to this sequence. The Fibonacci numbers Fn are defined as follows:
Fn = Fn− 1 + Fn− 2
This gives us our first explicit dynamic programming algorithm.
ITERFIB(n) 1 F[ 0 ] ← 0 2 F[ 1 ] ← 1 3 for i ← 2 to n 4 do 5 F[i] ← F[i − 1 ] + F[i − 2 ] 6 return F[n]
This algorithm clearly takes only O(n) time to compute Fn. By contrast, the original recursive algorithm
takes Θ(φn), φ = 1 +
√ 5 2 ≈^ 1.618. ITERFIB^ achieves an exponential speedup over the original recursive algorithm.
Dynamic programming is essentially recursion without repetition. Developing a dynamic programming algorithm generally involves two separate steps:
Dynamic programming algorithms need to store the results of intermediate subproblems. This is often but not always done with some kind of table. We will now cover a number of examples of problems in which the solution is based on dynamic programming strategy.
The words “computer” and “commuter” are very similar, and a change of just one letter, p-¿m, will change the first word into the second. The word “sport” can be changed into “sort” by the deletion of the ‘p’, or equivalently, ‘sort’ can be changed into ‘sport’ by the insertion of ‘p’. The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of:
For example, the edit distance between FOOD and MONEY is at most four:
FOOD −→ MOOD −→ MONfD −→ MONED −→ MONEY
There are numerous applications of the Edit Distance algorithm. Here are some examples:
Spelling Correction
If a text contains a word that is not in the dictionary, a ‘close’ word, i.e. one with a small edit distance, may be suggested as a correction. Most word processing applications, such as Microsoft Word, have spelling checking and correction facility. When Word, for example, finds an incorrectly spelled word, it makes suggestions of possible replacements.
Plagiarism Detection
If someone copies, say, a C program and makes a few changes here and there, for example, change variable names, add a comment of two, the edit distance between the source and copy may be small. The edit distance provides an indication of similarity that might be too close in some situations.
Computational Molecular Biology DNA is a polymer. The monomer units of DNA are nucleotides, and the polymer is known as a “polynucleotide.” Each nucleotide consists of a 5-carbon sugar (deoxyribose), a nitrogen containing base attached to the sugar, and a phosphate group. There are four different types of nucleotides found in DNA, differing only in the nitrogenous base. The four nucleotides are given one letter abbreviations as shorthand for the four bases.
Double-helix of DNA molecule with nucleotides Figure of Double-helix of DNA molecule with nucleotides goes here
The edit distance like algorithms are used to compute a distance between DNA sequences (strings over A,C,G,T, or protein sequences (over an alphabet of 20 amino acids), for various purposes, e.g.:
If we remove the last column, the edit distance reduces to 5.
We can use the optimal substructure property to devise a recursive formulation of the edit distance problem. There are a couple of obvious base cases:
E(0, j) = j
E(i, 0) = i
There are four possibilities for the last column in the shortest possible edit sequence:
Deletion: Last entry in bottom row is empty.
i= 3 ︷ ︸︸ ︷ A L G O R I T H M A︸ ︷︷L ︸ j= 2
In this case E(i, j) = E(i − 1, j) + 1
Insertion: The last entry in the top row is empty.
︷ i︸︸=^5 ︷ A L G O R I T H M A︸ L (^) ︷︷ T R U︸ j= 5
In this case E(i, j) = E(i, j − 1 ) + 1
Substitution: Both rows have characters in the last column.
︷ i︸︸=^4 ︷ A L G O R I T H M A︸ L (^) ︷︷ T︸ j= 3
If the characters are different, then
E(i, j) = E(i − 1, j − 1 ) + 1
i= 5 ︷ ︸︸ ︷ A L G O R I T H M A︸ L (^) ︷︷ T R︸ j= 4
If characters are same, no substitution is needed:
E(i, j) = E(i − 1, j − 1 )
Thus the edit distance E(i, j) is the smallest of the four possibilities:
E(i, j) = min
E(i − 1, j) + 1 E(i, j − 1 ) + 1 E(i − 1, j − 1 ) + 1 if A[i] 6 = B[j] E(i − 1, j − 1 ) if A[i] = B[j]
Consider the example of edit between the words “ARTS” and “MATHS”:
A R T S M A T H S
The edit distance would be in E(4, 5). If we recursion to compute, we will have
E(4, 5) = min
E(3, 4) + 1 if A[ 4 ] 6 = B[ 5 ] E(3, 4) if A[ 4 ] = B[ 5 ]
Recursion clearly leads to the same repetitive call pattern that we saw in Fibonnaci sequence. To avoid this, we will use the DP approach. We will build the solution bottom-up. We will use the base case E(0, j) to fill first row and the base case E(i, 0) to fill first column. We will fill the remaining E matrix row by row.
Table 6.2: Computing E[1, 1] and E[1, 2]
Table 6.3: Computing E[1, 3] and E[1, 4]
An edit script can be extracted by following a unique path from E[0, 0] to E[4, 5]. There are three possible paths in the current example. Let us follow these paths and compute the edit script. In an actual implementation of the dynamic programming version of the edit distance algorithm, the arrows would be recorded using an appropriate data structure. For example, each cell in the matrix could be a record with fields for the value (numeric) and flags for the three incoming arrows.
Table 6.4: The final table with all E[i, j] entries computed
Solution path 1: 1+ 0+ 1+ 1+ 0 = 3 D M S S M M A T H S A R T S
Solution path 3: 1+ 0+ 1+ 0+ 1+ 0 = 3 D M I M D M M A T H S A R T S
There are Θ(n^2 ) entries in the matrix. Each entry E(i, j) takes Θ( 1 ) time to compute. The total running time is Θ(n^2 ).
Suppose we wish to multiply a series of matrices:
A 1 A 2... An
In what order should the multiplication be done? A p × q matrix A can be multiplied with a q × r matrix B. The result will be a p × r matrix C. In particular, for 1 ≤ i ≤ p and 1 ≤ j ≤ r,
C[i, j] =
∑^ q
k= 1
A[i, k]B[k, j]
There are (p · r) total entries in C and each takes O(q) to compute.
C[i, j] =
∑^ q
k= 1
A[i, k]B[k, j]
Thus the total number of multiplications is p · q · r. Consider the case of 3 matrices: A 1 is 5 × 4 , A 2 is 4 × 6 and A 3 is 6 × 2 The multiplication can be carried out either as ((A 1 A 2 )A 3 ) or (A 1 (A 2 A 3 )). The cost of the two is
((A 1 A 2 )A 3 ) = ( 5 · 4 · 6 ) + ( 5 · 6 · 2 )= 180 (A 1 (A 2 A 3 )) = ( 4 · 6 · 2 ) + ( 5 · 4 · 2 ) = 88
There is considerable savings achieved even for this simple example. In general, however, in what order should we multiply a series of matrices A 1 A 2... An. Matrix multiplication is an associative but not commutative operation. We are free to add parenthesis the above multiplication but the order of matrices can not be changed. The Chain Matrix Multiplication Problem is stated as follows:
Given a sequence A 1 , A 2 ,... , An and dimensions p 0 , p 1 ,... , pn where Ai is of dimension pi− 1 × pi, determine the order of multiplication that minimizes the number of operations.
We could write a procedure that tries all possible parenthesizations. Unfortunately, the number of ways of parenthesizing an expression is very large. If there are n items, there are n − 1 ways in which outer most pair of parentheses can placed.
(A 1 )(A 2 A 3 A 4... An) or (A 1 A 2 )(A 3 A 4... An) or (A 1 A 2 A 3 )(A 4... An)
...... or (A 1 A 2 A 3 A 4... An− 1 )(An)
Once we split just after the kth^ matrix, we create two sublists to be parethesized, one with k and other with n − k matrices. (A 1 A 2... Ak) (Ak+ 1... An)
We could consider all the ways of parenthesizing these two. Since these are independent choices, if there are L ways of parenthesizing the left sublist and R ways to parenthesize the right sublist, then the total is L · R. This suggests the following recurrence for P(n), the number of different ways of parenthesizing n items:
P(n) =
1 if n = 1, ∑n− 1 k= 1 P(k)P(n^ −^ k)^ if^ n^ ≥^2
This is related to a famous function in combinatorics called the Catalan numbers. Catalan numbers are related the number of different binary trees on n nodes. Catalan number is given by the formula:
C(n) =
n + 1
2n n
In particular, P(n) = C(n − 1 ) C(n) ∈ Ω( 4 n/n3/2) The dominating term is the exponential 4 n^ thus P(n) will grow large very quickly. So this approach is not practical.
The dynamic programming solution involves breaking up the problem into subproblems whose solutions can be combined to solve the global problem. Let Ai..j be the result of multiplying matrices i through j. It is easy to see that Ai..j is a pi− 1 × pj matrix.
A 3 4 × 5
5 × 2
2 × 8
8 × 7
4 × 7
Next, we compute cost of multiplication for sequences of three matrices. These are m[1, 3], m[2, 4], m[3, 5],... , m[n − 2, n]. m[1, 3], for example is
m[1, 3] = min
m[1, 1] + m[2, 3] + p 0 · p 1 · p 3 m[1, 2] + m[3, 3] + p 0 · p 2 · p 3
We repeat the process for sequences of four, five and higher number of matrices. The final result will end up in m[1, n].
Example: Let us go through an example. We want to find the optimal multiplication order for
A 1 ( 5 × 4 )
( 4 × 6 )
( 6 × 2 )
( 2 × 7 )
( 7 × 3 )
We will compute the entries of the m matrix starting with the base condition. We first fill that main diagonal:
0 0 0 0 0
Next, we compute the entries in the first super diagonal, i.e., the diagonal above the main diagonal:
m[1, 2] = m[1, 1] + m[2, 2] + p 0 · p 1 · p 2 = 0 + 0 + 5 · 4 · 6 = 120 m[2, 3] = m[2, 2] + m[3, 3] + p 1 · p 2 · p 3 = 0 + 0 + 4 · 6 · 2 = 48 m[3, 4] = m[3, 3] + m[4, 4] + p 2 · p 3 · p 4 = 0 + 0 + 6 · 2 · 7 = 84 m[4, 5] = m[4, 4] + m[5, 5] + p 3 · p 4 · p 5 = 0 + 0 + 2 · 7 · 3 = 42
The matrix m now looks as follows:
We now proceed to the second super diagonal. This time, however, we will need to try two possible values for k. For example, there are two possible splits for computing m[1, 3]; we will choose the split that yields the minimum:
m[1, 3] = m[1, 1] + m[2, 3] + p 0 · p 1 · p 3 == 0 + 48 + 5 · 4 · 2 = 88 m[1, 3] = m[1, 2] + m[3, 3] + p 0 · p 2 · p 3 = 120 + 0 + 5 · 6 · 2 = 180 the minimum m[1, 3] = 88 occurs with k = 1
Similarly, for m[2, 4] and m[3, 5]:
m[2, 4] = m[2, 2] + m[3, 4] + p 1 · p 2 · p 4 = 0 + 84 + 4 · 6 · 7 = 252 m[2, 4] = m[2, 3] + m[4, 4] + p 1 · p 3 · p 4 = 48 + 0 + 4 · 2 · 7 = 104 minimum m[2, 4] = 104 at k = 3
m[3, 5] = m[3, 3] + m[4, 5] + p 2 · p 3 · p 5 = 0 + 42 + 6 · 2 · 3 = 78 m[3, 5] = m[3, 4] + m[5, 5] + p 2 · p 4 · p 5 = 84 + 0 + 6 · 7 · 3 = 210 minimum m[3, 5] = 78 at k = 3
With the second super diagonal computed, the m matrix looks as follow:
We repeat the process for the remaining diagonals. However, the number of possible splits (values of k) increases:
m[1, 4] = m[1, 1] + m[2, 4] + p 0 · p 1 · p 4 = 0 + 104 + 5 · 4 · 7 = 244 m[1, 4] = m[1, 2] + m[3, 4] + p 0 · p 2 · p 4 = 120 + 84 + 5 · 6 · 7 = 414 m[1, 4] = m[1, 3] + m[4, 4] + p 0 · p 3 · p 4 = 88 + 0 + 5 · 2 · 7 = 158 minimum m[1, 4] = 158 at k = 3
m[2, 5] = m[2, 2] + m[3, 5] + p 1 · p 2 · p 5 = 0 + 78 + 4 · 6 · 3 = 150 m[2, 5] = m[2, 3] + m[4, 5] + p 1 · p 3 · p 5 = 48 + 42 + 4 · 2 · 3 = 114 m[2, 5] = m[2, 4] + m[5, 5] + p 1 · p 4 · p 5 = 104 + 0 + 4 · 7 · 3 = 188 minimum m[2, 5] = 114 at k = 3
The matrix m at this stage is:
Based on the computation, the minimum cost for multiplying the five matrices is 160 and the optimal order for multiplication is
This can be represented as a binary tree
Figure 6.2: Optimum matrix multiplication order for the five matrices example
Here is the dynamic programming based algorithm for computing the minimum cost of chain matrix multiplication.
MATRIX-CHAIN(p, N) 1 for i = 1, N 2 do m[i, i] ← 0 3 for L = 2, N 4 do 5 for i = 1, n − L + 1 6 do j ← i + L − 1 7 m[i, j] ← ∞ 8 for k = 1, j − 1 9 do t ← m[i, k] + m[k + 1, j] + pi− 1 · pk · pj 10 if (t < m[i, j]) 11 then m[i, j] ← t; s[i, j] ← k
Analysis: There are three nested loops. Each loop executes a maximum n times. Total time is thus Θ(n^3 ).
The s matrix stores the values k. The s matrix can be used to extracting the order in which matrices are to be multiplied. Here is the algorithm that caries out the matrix multiplication to compute Ai..j:
MULTIPLY(i, j) 1 if (i = j) 2 then return A[i] 3 else k ← s[i, j] 4 X ← MULTIPLY(i, k) 5 Y ← MULTIPLY(k + 1, j) 6 return X · Y
A thief goes into a jewelry store to steal jewelry items. He has a knapsack (a bag) that he would like to fill up. The bag has a limit on the total weight of the objects placed in it. If the total weight exceeds the limit, the bag would tear open. The value of of the jewelry items varies for cheap to expensive. The thief’s goal is to put items in the bag such that the value of the items is maximized and the weight of the items does not exceed the weight limit of the bag. Another limitation is that an item can either be put in the bag or not - fractional items are not allowed. The problem is: what jewelry should the thief choose that satisfy the constraints?
Formally, the problem can be stated as follows: Given a knapsack with maximum capacity W, and a set S consisting of n items Each item i has some weight wi and value value vi (all wi , vi and W are integer values) How to pack the knapsack to achieve maximum total value of packed items? For example, consider the following scenario:
Figure 6.3: Knapsack can hold W = 20
Item i Weight wi Value vi 1 2 3 2 3 4 3 4 5 4 5 8 5 9 10
The knapsack problem belongs to the domain of optimization problems. Mathematically, the problem is
maximize
i∈T
vi
subject to
i∈T
wi ≤ W