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

Dynamic Programming Approach

Uploaded by

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

Dynamic Programming Approach

Uploaded by

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

Dynamic Programming Approach

1. Breakdown Complex problems to sub problems


2. Find the optimal solutions to the sub problems
3. Store the results of sub problems
4. Reuse the results of sub problems to avoid repeated calculations
5. Finally find the result of complex problem

Consider a program to generate Fibonacci series up to n terms using recursive method

def fib(n):

if n <= 1:

return n

else:

return(fib(n-1) + fib(n-2))

n = int(input("Enter n :"))

if n <= 0:

print("Plese enter a positive integer")

else:

print("Fibonacci sequence:")

for i in range(n):

print(fib(i),end = " ")


Consider n = 5 ; We know fib(0) = 0 and fib(1) = 1

Fib(5)

Fib(4) Fib(3)

Fib(2) Fib(1)
Fib(3) Fib(2)

Fib(1) Fib(0)
Fib(2) Fib(1) Fib(1) Fib(0)

Fib(1) Fib(0)

Challenges with Recursive Approach

Even though the recursive approach is intuitive, it often leads to increased time complexity.
This is because the same subproblems are repeatedly calculated, resulting in redundant
computations and inefficient performance.

Approaches in Dynamic Programming

Dynamic Programming can be implemented using two main approaches:

1. Memoization (Top-Down Approach)

 Definition:
Memoization solves the problem recursively while storing the results of subproblems
in a table (often a dictionary or array).
 Process:
o When a subproblem is solved, its result is saved.
o If the same subproblem is encountered again, the solution is retrieved from the
table instead of being recalculated.
2. Tabulation (Bottom-Up Approach)

 Definition:
Tabulation solves the problem iteratively by filling a table (usually an array) in a
bottom-up manner.
 Process:
o Starts by solving the smallest subproblems.
o Uses their solutions to iteratively build solutions for larger subproblems.
 Advantages:
o Eliminates recursion overhead.
o Suitable for problems where all subproblems need to be solved.

Solution using Tabulation :

def fib(n):

array = [0] * (n)


array[0] = 0 # First Fibonacci number
if n > 1:
array[1] = 1 # Second Fibonacci number

for i in range(2, n):


array[i] = array[i - 1] + array[i - 2]

return array

n = int(input("Enter the number of terms: "))


if n <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
print(fib(n))
Compare Recursion and Dynamic Programming

Features Recursion Dynamic Programming

By breaking a difficulty down


It is a technique by breaks
into smaller problems of the
them into smaller problems
same problem, a function calls
and stores the results of these
itself to solve the problem
subproblems to avoid
until a specific condition is
repeated calculations.
Definition met.

Using a bottom-up
Recursion frequently employs
methodology, dynamic
a top-down method in which
programming starts by
the primary problem is broken
resolving the smallest
down into more manageable
subproblems before moving
subproblems.
Approach on to the primary issue.

To avoid infinite loops, it is Although dynamic


necessary to have a base case programming also needs a
(termination condition) that base case, it focuses mostly on
stops the recursion when a iteratively addressing
Base Case certain condition is satisfied. subproblems.

Recursion might be slower due Dynamic programming is often


to the overhead of function faster due to optimized
calls and redundant subproblem solving and
Performance calculations. memoization.

Dynamic programming require


additional memory to record
intermediate results.
It does not require extra
memory, only requires stack
space

Memory Usage
Features Recursion Dynamic Programming

Higher, often exponential for


problems with overlapping
subproblems. Lower
Time Complexity

Slower for problems with Faster due to elimination of


Performance overlapping subproblems. redundant calculations.

You might also like