0% found this document useful (0 votes)
48 views4 pages

CSA Lab 8

This document discusses dynamic programming techniques and provides examples. It describes dynamic programming as a method for solving optimization problems by breaking them down into subproblems and storing the results of already solved subproblems. It gives two properties that characterize problems suitable for dynamic programming: overlapping subproblems and optimal substructure. The document then provides examples of applying dynamic programming to the Fibonacci sequence and matrix chain multiplication problems. Students are assigned exercises to write Python programs implementing longest common subsequence, matrix chain multiplication, and calculating statistics of the Fibonacci sequence.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

CSA Lab 8

This document discusses dynamic programming techniques and provides examples. It describes dynamic programming as a method for solving optimization problems by breaking them down into subproblems and storing the results of already solved subproblems. It gives two properties that characterize problems suitable for dynamic programming: overlapping subproblems and optimal substructure. The document then provides examples of applying dynamic programming to the Fibonacci sequence and matrix chain multiplication problems. Students are assigned exercises to write Python programs implementing longest common subsequence, matrix chain multiplication, and calculating statistics of the Fibonacci sequence.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer System Algorithm (MCS-205) SSUET/QR/114

LAB # 08

Dynamic Programming Techniques


OBJECTIVE:

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

Time Complexity: T(n) = T(n) which is linear.

Matrix Chain Multiplication:


Matrix chain multiplication (or the matrix chain ordering problem) is an optimization problem concerning the
most efficient way to multiply a given sequence of matrices. The problem is not actually to perform the
multiplications, but merely to decide the sequence of the matrix multiplications involved.

Time Complexity: O(n3 )

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):

Lab 08: Dynamic Programming Techniques


Name: Tanzeel Ur Rehman 2 Roll no: BMCS22S-
002
Computer System Algorithm (MCS-205) SSUET/QR/114

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:

Source Code & Output:

Lab 08: Dynamic Programming Techniques


Name: Tanzeel Ur Rehman 3 Roll no: BMCS22S-
002
Computer System Algorithm (MCS-205) SSUET/QR/114

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.

Source Code & Output:

Lab 08: Dynamic Programming Techniques


Name: Tanzeel Ur Rehman 4 Roll no: BMCS22S-
002

You might also like