Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Design and Analysis Algorithm

Lecture ( )
Dynamic Programming
Dynamic Programming definition

Dynamic Programming is also used in optimization problems. Like divide-and-


conquer method, Dynamic Programming solves problems by combining the
solutions of sub problems. 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. These properties are overlapping sub-problems and
optimal substructure.
The dynamic programming is working like this

For example, if we have a problem represented in the following figure

Main problem

Then this problem is divided to group of sub problem


Main problem

problem1 problem2 problem3 ... Problem N

Then solved the first sub problem, take the result of first sub problem and solved the second sub
problem … etc. until solved all the problem

Main problem

Solve p1 Solve p2 Solve p3 ... Solve N


For example: the Fibonacci series is a series of numbers generating according to
the following

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , . . .

each number in Fibonacci series is the product of the addition of the two previous
numbers.

for example : 89 product of 55 + 34 “ this two previous number “

and : 55 product of 34+ 21“ also two previous number “

and : 34 product of 21+ 13 “ also two previous number

If we need to predict the new number in Fibonacci series then addition the two
previous number. the new number = 89+55 = 144
Dynamic programming complexity analysis
1. using recursive function
Fin ( int x ) E (T) = 0
{
if ( x = = 0 ) E (T) = N
return 0; E (T) = N-1
if ( x = = 1 ) E (T) = N

return 1; E (T) = N-1


else E (T) = N

return fib( x-1 ) + fib ( x-2 ) E; (T) = N E (T) = N-1 E (T) = N-2

}
The result of time complexity = O(N) = n!
1. using Dynamic Programming strategy
In this strategy we divided the problem to small sub problem and solved the first sub problem, take
the result of first sub problem and solved the second sub problem … etc. until solved all the
problem

Fib[ ] E (T) = 0

Fib[ 0 ] = 0 ; E (T) = 1

Fib[ 1 ] = 1 ; E (T) = 1

For( int i=2 ; I < n ; i++ ) E (T) = n

Fib[ i ] = fib[ i-1 ] + fib[ i- 2 ] ; E (T) = n-1

The result of time complexity = O(N) = 0 + 1 + 1 + n + n-1 = 2n + 1  O(N) = N


Dynamic programming is similar to the divide and rule model, in that it integrates
solutions of sub-problems together. Dynamic programming is generally used
when the solutions to a particular set of sub-problems arise over and over again.
So that solutions to sub-problems in dynamic programming are stored in a table in
order to avoid calculating them again. This means that dynamic programming is
not useful when there are no overlapping sub problems; There is no point in
storing solutions if the algorithm does not need them again

For example, Binary Search does not have overlapping sub-problem. Whereas
recursive program of Fibonacci numbers have many overlapping sub-problems.
Steps of Dynamic Programming Approach

Dynamic Programming algorithm is designed using the following four steps −


Characterize the structure of an optimal solution.

Recursively define the value of an optimal solution.

Compute the value of an optimal solution, typically in a bottom-up fashion.

Construct an optimal solution from the computed information.


Applications of Dynamic Programming Approach

Matrix Chain Multiplication

Longest Common Subsequence

Travelling Salesman Problem

You might also like