0% found this document useful (0 votes)
24 views2 pages

Dynamic Programming2edited

Uploaded by

Suneel Ramireddy
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)
24 views2 pages

Dynamic Programming2edited

Uploaded by

Suneel Ramireddy
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/ 2

Q: Explain General Method of Dynamic Programming (Or) Build any one application of Dynamic

Program With One Example/

Dynamic Programming (DP) Introduction

Dynamic Programming is a commonly used algorithmic technique used to optimize recursive


solutions when same subproblems are called again.

 DP is to store solutions to subproblems so that each is solved only once.

 To solve DP problems, we first write a recursive solution in a way that there are overlapping
subproblems in the recursion tree (the recursive function is called with the same parameters
multiple times)

 To make sure that a recursive value is computed only once (to improve time taken by
algorithm), we store results of the recursive calls.

 There are two ways to store the results, one is top down (or memoization) and other is
bottom up (or tabulation).

When to Use Dynamic Programming (DP)?

Dynamic programming is used for solving problems that consists of the following characteristics:

1. Optimal Substructure:

The property Optimal substructure means that we use the optimal results of subproblems to achieve
the optimal result of the bigger problem.

Example:

finding the minimum cost path in a weighted graph from a source node to a destination node.

2. Overlapping Subproblems:

The same subproblems are solved repeatedly in different parts of the problem refer to Overlapping
Subproblems Property in Dynamic Programming.

Example:

Fibonacci series.

Approaches of Dynamic Programming (DP)

Dynamic programming can be achieved using two approaches:

1. Top-Down Approach (Memoization):

In the top-down approach, also known as memoization, we keep the solution recursive and add a
memoization table to avoid repeated calls of same subproblems.

2. Bottom-Up Approach (Tabulation):

In the bottom-up approach, also known as tabulation, we start with the smallest subproblems and
gradually build up to the final solution.
Example of Dynamic Programming (DP)

Example 1: Consider the problem of finding the Fibonacci sequence:

Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

To find the nth Fibonacci number using a brute force approach, you would simply add the (n-
1)th and (n-2)th Fibonacci numbers.

The time complexity of the above approach is exponential and upper bounded by O(2n) as we make
two recursive calls in every function.

How will Dynamic Programming (DP) Work?

Let us now see the recursion tree with overlapping subproblems highlighted with same color. We can
clearly see that that recursive solution is doing a lot work again and again which is causing the time
complexity to be exponential. Imagine time taken for computing a large Fibonacci number.

 Identify Subproblems: Divide the main problem into smaller, independent subproblems, i.e.,
F(n-1) and F(n-2)

 Store Solutions: Solve each subproblem and store the solution in a table or array so that we
do not have to recompute the same again.

 Build Up Solutions: Use the stored solutions to build up the solution to the main problem.
For F(n), look up F(n-1) and F(n-2) in the table and add them.

 Avoid Recomputation: By storing solutions, DP ensures that each subproblem (for example,
F(2)) is solved only once, reducing computation time.

Using Memoization Approach - O(n) Time and O(n) Space

You might also like