0% found this document useful (0 votes)
14 views

Dynamic Programming

The document discusses dynamic programming and how it can be used to solve several problems efficiently. It explains dynamic programming concepts like memoization and tabulation. It also provides examples of applying dynamic programming to problems like Fibonacci sequence, matrix chain multiplication, and longest common subsequence.

Uploaded by

jonieokeloe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Dynamic Programming

The document discusses dynamic programming and how it can be used to solve several problems efficiently. It explains dynamic programming concepts like memoization and tabulation. It also provides examples of applying dynamic programming to problems like Fibonacci sequence, matrix chain multiplication, and longest common subsequence.

Uploaded by

jonieokeloe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DYNAMIC PROGRAMMING

1.INTRODUCTION
Simple Explanation
Dynamic Programming is a problem-solving technique that helps you solve complex problems
by breaking them down into smaller, simpler subproblems. It's like solving a big puzzle by
solving smaller pieces of it first.
Imagine you have a big task to complete, like climbing a tall mountain. Dynamic Programming
tells you to take it step by step. Instead of trying to jump to the top in one giant leap, you start at
the bottom and climb up one small step at a time.
The key idea in Dynamic Programming is to solve each subproblem only once, thus
reducing the number of computations: once the solution to a given subproblem has been
computed, it is stored, the next time the same solution is needed, it is simply looked up.
In computer science, Dynamic Programming is often used to optimize the efficiency of
algorithms by avoiding redundant work. It's particularly useful for solving problems that have
overlapping subproblems, where the same smaller problems are solved multiple times. By
storing and reusing the solutions, Dynamic Programming makes these problems much faster to
solve.

Principle of Optimality
It states that the optimal solution for a larger subproblem contains an optimal solution for a
smaller subproblem.
Optimal substructure means that the solution to a given optimization problem can be obtained by
the combination of optimal solutions to its sub problems.
Overlapping subproblems means that the space of subproblems must be small, that is, any
recursive algorithm solving the problem should solve the same subproblems over and over,
rather than generating new subproblems.

2.THE FIBONACCI SEQUENCE


The Fibonacci sequence is a series of numbers where each number is the sum of the two
preceding ones, usually starting with 0 and 1.
It is defined by the following equation:
Approaches used in Dynamic Programming
1. Memoization (Top-Down approach)
Definition:
In the Top-Down approach, also known as memoization, you start with the original problem and
break it down into smaller subproblems. You then solve these subproblems recursively, and the
solutions are stored in a data structure (typically an array or a dictionary) to avoid recomputation.
This approach is essentially a "recursive" way of solving problems.
Solving Fibonacci sequence using top-down approach

ALGORITHM FibonacciMemoization (n, memo):


{
int fib (int n)
if n<= 1
return n
else
return fib (n-1)+ (n-2)
}

2.Bottom-Up Dynamic Programming (Tabulation):


Definition:
In the Bottom-Up approach, you start with the smallest subproblems and solve them iteratively,
moving towards the original problem. You maintain a table (usually an array or a matrix) to store
the solutions to subproblems and build up to the final solution. This is often done in a loop.
Solving Fibonacci sequence using tabulation approach
In the Tabulation approach, we calculate Fibonacci numbers iteratively, starting from the base
cases and moving up to the desired value. Here's how it works:

ALGORITHM FibonacciTabulation(n):
{
if n <=1:
return n;
else
F[0]=0 ; F[1]=1 ;
For ( int i=2 ; i<=n ; i++) {
F[I]= F[I-1]+F[I-2]
}
Return F[n]
}
Differences between Tabulation and Memoization
2.MATRIX CHAIN MULTIPLICATION
2.1. What's the Problem?
Imagine you have a bunch of matrices, and you want to multiply them together to get a final
result. In the world of computer graphics or scientific simulations, this happens a lot. But here's
the catch: The order in which you multiply these matrices can significantly affect the time it
takes to get the result.

2.2. Why Does the Order Matter?


Well, think of it like this: When you multiply two matrices, the number of multiplications and
additions you need to do depends on their dimensions. If you have a lot of matrices to multiply,
finding the best order can save you a ton of time and computational effort.
2.3. How Does Dynamic Programming Help?
Dynamic Programming comes to the rescue! It helps you find the best order for multiplying
these matrices, which minimizes the number of multiplications and additions needed.
Here's a simple example: Let's say you have three matrices, A, B, and C. You can multiply them
like this:
- (A * B) * C
- A * (B * C)
Dynamic Programming helps you figure out which order is more efficient, so you don't waste
time on unnecessary calculations.
2.4. In a Nutshell:
Matrix Chain Multiplication with Dynamic Programming is like a smart calculator for
multiplying matrices. It figures out the best way to do it, so you get your final result as fast as
possible, saving time and computer power.
3. LONGEST COMMON SUBSEQUENCE
What is it?
The Longest Common Subsequence is a problem that arises in various applications like text
comparison, DNA sequence alignment, and more. It's about finding the longest sequence of
characters or elements that are the same in two given sequences, but not necessarily in the same
order.

How does Dynamic Programming Help?


Dynamic Programming provides an efficient way to find the LCS. It works by comparing the
characters or elements of the two sequences step by step, building a table to store intermediate
results.
Here's a simple explanation of the process:
1. Create a table (often a 2D array) where you'll store information about the common
subsequences of the two sequences.
2. Start comparing the characters or elements of the two sequences from the beginning.
3. If you find a match, you increase the value in the table by 1, based on the previous values in
the table.
4. If there's no match, you carry forward the value from either the cell above or the cell to the left
(whichever is larger).
5. Keep doing this until you've compared all the characters or elements in both sequences.
6. The value in the bottom-right cell of the table will tell you the length of the longest common
subsequence.

Question:

What is a longest common subsequence of X and Y

If X = <A, B, C, B, D, A, B>

Y = <B, D, C, A, B, A>
Solution:

1. We create a table to compare the characters of X and Y.


**Why is it Useful?**
2. Starting from the first character of X and Y, we compare them:
• Is 'A' (from X) the same as 'B' (from Y)? No.
• Is 'A' (fr om X) the same as 'D' (from Y)? No.
LCS3. can
Webekeep
verygoing
handyuntil
in various
we findapplications, like comparing texts to find the difference
matching characters:
between two• 'B' (from X) matches 'B' (from Y). Weto
documents or aligning DNA sequences addunderstand genetic relationships.
it to our LCS.
• We move to the next character in both sequences:
X: A, C, B, D, A, B Y: D, C, A, B, A
4. We continue
In a nutshell, dynamic looking for matching
programming helpscharacters and adding
you efficiently them
find the to the
longest LCS: subsequence
common
between two• sequences,
'C' (from X) matches
even if they'C'
are(from Y).
not identical, and it's a powerful tool in text analysis,
• 'B' (from X)
bioinformatics, and many oth matches 'A' (from Y).
5. We keep going until we've compared all characters:
X: A, D, A Y: D, A
6. We find that 'D' (from X) matches 'D' (from Y), and 'A' (from X) matches 'A' (from Y).
7. Our LCS is: B, C, B, A. and B, D, A, B.

You might also like