CSA Lab 8
CSA Lab 8
LAB # 08
To implement Fibonacci series and algorithm of Matrix Chain Multiplication for Dynamic Programming
Techniques
THEORY:
DYNAMIC PROGRAMMING:
Dynamic Programming is also used in optimization problems. Like divide-and-conquer method, Dynamic
Programming solves problems by combining the solutions of subproblems. Moreover, Dynamic Programming
algorithm solves each sub-problem just once and then saves its answer in a table, thereby avoiding the work of
re-computing the answer every time.
Two main properties of a problem suggest that the given problem can be solved using Dynamic Programming.
Overlapping sub-problems
optimal substructure.
1. Overlapping Sub-Problems:
Similar to Divide-and-Conquer approach, Dynamic Programming also combines solutions to sub
problems. It is mainly used where the solution of one sub-problem is needed repeatedly. The computed
solutions are stored in a table, so that these don’t have to be recomputed. Hence, this technique is needed
where overlapping sub-problem exists. For example, Binary Search does not have overlapping sub-problem.
Whereas recursive program of Fibonacci numbers has many overlapping sub-problems.
2. Optimal Sub-Structure:
A given problem has Optimal Substructure Property, if the optimal solution of the given problem can be
obtained using optimal solutions of its sub-problems. For example, the Shortest Path problem has the
following optimal substructure property.
If a node x lies in the shortest path from a source node u to destination node v, then the shortest path from
u to v is the combination of the shortest path from u to x, and the shortest path from x to v.
The standard All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-Ford are typical
examples of Dynamic Programming.
FIBONACCI SERIES:
In mathematics, the Fibonacci numbers, commonly denoted F ₙ, form a sequence, the Fibonacci sequence, in
which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1,
although some authors omit the initial terms and start the sequence from 1 and 1 or from 1 and 2.
For example:
The Fibonacci sequence of whole numbers is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
1597, 2584, ... The sequence is widely known for its many intriguing properties.
Lab 08: Dynamic Programming Techniques
Name: Tanzeel Ur Rehman 1 Roll no: BMCS22S-
002
Computer System Algorithm (MCS-205) SSUET/QR/114
EXERCISE:
A. Create a file named lab6.py.Point out the errors, if any, in the following Python programs. (also
write the correct program in code box)
1. Code:
def lcs(X, Y, m, n):
if m == 0 or n == 0
return 0
elif X[m-1] == Y[n-1]
return 1 + lcs(X, Y, m-1, n-1);
else:
return max(lcs(X, Y, m, n-1).lcs(X, Y, m-1, n))
X = “Abdul”#"AGGTAB"
Y = “Saamd”#"GXTXAYB"
print ("Length of LCS is ", lCs(X , Y, len(X), len(Y)) )
Corrected code:
2. Code:
def longestRepeatedSubSeq(str): n = len(str) dp =
[[0 for i in range(n+1)] for j in range(n+1)] for i in
range(1, n + 1): for j in range(1, n + 1): if
(str[i-1] == str[j-1] and i != j):
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
res = '' i = n j = n
while (i > 0 and j > 0);
if (dp(i)[j] == dp[i-1][j-1] + 1):
res += str[i-1]
i -= 1 j -= 1 elif
(dp[i][j] == dp[i-1][j]):
i -= 1 else
j -= 1
res = ''.join(reversed(res)) return
res
str = 'AABEBCDD'
print(longestRepeatedSubSeq(int))
Corrected code:
B. Write a program for the implementation of the Matrix Chain Multiplication problem using Dynamic
Programming Code:
C. Write a program to print Fibonacci series and also print the total number of even and odds in that series.
Series should be from 1-15.