You are on page 1of 11

CSC 373

Lecture 7
By
Javed Siddique
Dynamic Programming
• Works just like divide and conquer.
• It first solves solution to sub-problems.
• Then it combines the solution to sub-problems.
• In Divide and Conquer, the sub-problems were independent.
• In Dynamic Programming, the sub-problems are not independent.
Sub-problems share sub-problems.
• A dynamic-programming algorithm solves every subsubproblem just
once and then saves its answer in a table.
• Thereby avoiding the work of recomputing the answer every time
the subsubproblem is encountered.
• Typically applied to optimization problems.
• In optimization problem, we like to find a solution to either minimum
or maximum value.
Steps of Dynamic Programming
• The development of a dynamic-programming
algorithm can be broken into a sequence of four
steps.
– Characterize the structure of an optimal solution.
– Recursively define the value of an optimal solution.
– Compute the value of an optimal solution in a bottom-
up fashion.
– Construct an optimal solution from computed
information.
Optimal Substructure
• The first step in solving an optimization problem
by dynamic programming is to characterize the
structure of an optimal solution.
• A problem exhibits optimal substructure if an
optimal solution to the problem contains within it
optimal solutions to subproblems.
• Whenever a problem exhibits optimal
substructure, it is a good clue that dynamic
programming might apply.
Longest Common Subsequence
• S1
=ACCGGTCGAGTGCGCGGAAGCCGGC
CGAA
• S2 =
GTCGTTCGGAATGCCGTTGCTCTGTAA
A
• The longest strand S3 is
GTCGTCGGAAGCCGGCCGAA
Subsequence
• A subsequence of a given sequence is just the given
sequence with zero or more elements left out.
• Formally, given a sequence X = (x1, x2, . . . , xm),
another sequence Z = (z1, z2, . . . , zk) is a
subsequence of X if there exists a strictly increasing
sequence (i1, i2, . . . ,ik) of indices of X such that for all j
= 1, 2, . . ., k, we have xi j = z j .
• For example, Z = (B, C, D, B) is a subsequence of X =
(A, B,C, B, D, A, B) with corresponding index sequence
2, 3, 5, 7.
• Given two sequences X and Y , we say that a sequence
Z is a common subsequence of X and Y if Z is a
subsequence of both X and Y .
Optimal Substructure of LCS
Recursive Solution
Computing the length of LCS
Example
Constructing an LCS

You might also like