0% found this document useful (0 votes)
45 views32 pages

Dynamic Programing: Damascus University

The document discusses dynamic programming and provides examples of classic dynamic programming problems. It explains the recursive and iterative (top-down and bottom-up) approaches to solving problems using dynamic programming. Examples provided include the Fibonacci sequence, coin change problem, longest increasing subsequence (LIS) problem, and the 0/1 knapsack problem. Pseudocode solutions using both top-down and bottom-up implementations are given for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views32 pages

Dynamic Programing: Damascus University

The document discusses dynamic programming and provides examples of classic dynamic programming problems. It explains the recursive and iterative (top-down and bottom-up) approaches to solving problems using dynamic programming. Examples provided include the Fibonacci sequence, coin change problem, longest increasing subsequence (LIS) problem, and the 0/1 knapsack problem. Pseudocode solutions using both top-down and bottom-up implementations are given for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 32

1

Dynamic Programing
Damascus University
2 - Fibonacci Sequence
  Recursive Solution:

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

Complexity:
3 - Fibonacci Sequence
 Recursive tree:

Many function calls are repeated.


4 - Fibonacci Sequence
 DP Idea:
5 - Steps for Finding a DP Solution
 Representing
  the given problem as states, where the answer of each
state depends on one or more smaller states.
Ex: In the Fibonacci sequence, the state is the number (n), and the
answer
for this state depends on states (n-1) and (n-2)
 Finding the base cases. i.e. the states that have obvious answers.
Ex: In the Fibonacci sequence, the base cases are (0) and (1).
 Writing the DP formula.
Ex: In the Fibonacci sequence, the formula is:
Implementing DP Solutions:
6

1) Top-Down (recursion + memoization)


 Recursive function.
 Starts from a large state and breaks it up into smaller states.
 Calls the function for each one of the smaller states.
 Calculates the answer using the answers of the smaller states.
Implementing DP Solutions:
7

1) Top-Down (recursion + memoization)


int TopDown(state n)
{
if (state already calculated)
return DP[n];
else if (base case)
return base_case_answer;
else
{
c1=TopDown(a); // a,b,... are the states which state n depends on.
c2=TopDown(b);
...
return DP[n]= ... ; // DP formula
// Ex1: DP[n]=c1+2*c2+...
// Ex2: DP[n]=min(c1,c2,...)
}
}
Implementing DP Solutions:
8

1) Top-Down (recursion + memoization)


Ex: Fibonacci Sequence:
int DP[100];
int TopDownFib(int n)
{
if (DP[n]!=-1) // state already calculated
return DP[n];
else if (n<=1) // base cases (0) and (1)
return 1;
else
{
int c1=TopDownFib(n-1); // state (n) depends on (n-1) and (n-2)
int c2=TopDownFib(n-2);

return DP[n]=c1+c2; // DP formula : F(n)=F(n-1)+F(n-2)


}
}
Implementing DP Solutions:
9

2) Bottom-up:
 Iterative.
 Starts from the base case.
 If state X depends on state Y, then state Y must be calculated
before state X.
Implementing DP Solutions:
10

2) Bottom-up:
int BottomUp(state final_state)
{
DP[base_case_1]=base_case_answer_1;
DP[base_case_2]=base_case_answer_2;
...

for (state n) // for loop over all states from smaller to larger states.
{
int c1=DP[a]; // a,b,... are the states which state n depends on.
int c2=DP[b];
...

DP[n]= ...; // DP formula


// Ex1: DP[n]=c1+2*c2+...
// Ex2: DP[n]=min(c1,c2,...)
}

return DP[final_state];
}
Implementing DP Solutions:
11

2) Bottom-up:
Ex: Fibonacci Sequence:
int DP[100];
int BottomUpFib(int n)
{
DP[0]=1; // base cases
DP[1]=1;

for (int i=2;i<=n;i++) // for loop over all states from smaller to larger states.
{
int c1=DP[i-1]; // state (i) depends on (i-1) and (i-2)
int c2=DP[i-2];

DP[n]= c1+c2; // DP formula F(i)=F(i-1)+F(i-2)


}

return DP[n];
}
12 Classic DP Problems
 Coin Change Problem:
Given a set of coins, for example {1,5,7}.
What is the minimum number of coins needed to achieve a value X ?
Ex: for X=10 the answer is 2 (5+5).

 Steps for finding a solution:


1) The
Representing
state is the
thevalue
given(X)
problem
, whereas the
states,
answerwhere
of this
thestate
answer
depends
of each
on
state
the answer
depends
of states
on one(X-1),
or more
(X-5),
smaller
(X-7). states.
2) The
Finding
basethe
case
base
is cases.
when X=0,
i.e. the
thestates
answerthat
is 0.
have obvious answers.
3) Writing the DP formula.
13 Classic DP Problems
 Coin Change Problem:
Top-Down Implementation:
int DP[1000];
int TopDownCC(int X)
{
if (DP[X]!=-1)
return DP[X];
else if (X==0)
return 0;
else
{
DP[x]=TopDownCC(X-1)+1;
if (X>=5)
DP[x]=min(DP[x],TopDownCC(X-5)+1);
if (X>=7)
DP[x]=min(DP[x],TopDownCC(X-7)+1);
return DP[x];
}
}
14 Classic DP Problems
 Coin Change Problem:
Bottom-Up Implementation:
int DP[1000];
int BottomUpCC(int X)
{
DP[0]=0;
for (int i=1;i<=X;i++)
{
DP[i]=DP[i-1]+1;
if (X>=5)
DP[i]=min(DP[i],DP[i-5]+1);
if (X>=7)
DP[i]=min(DP[i],DP[i-7]+1);
}
return DP[X];
}
15 Classic DP Problems
 LIS Problem:
Given n numbers, find the longest increasing subsequence.
Ex: 1 5 3 1 6 , the answer is 3 (1,5,6) or (1,3,6)
 Steps for finding a solution:
1) The
Representing
state is the
thenumber
given problem
of processed
as states,
elements
where
i, state
the answer
(i) depends
of each
on all
state depends
states from (0)onto one
(i-1) or more smaller states.
2) The
Finding
basethe
case
base
is cases.
(0), thei.e.
answer
the states
is 0. that have obvious answers.
3) Writing the DP formula.
16
Classic DP Problems
 LIS Problem:
Top-Down Implementation:
int DP[1000];
int a[1000];
int TopDownLIS(int i)
{
if (DP[i]!=-1)
return DP[i];
else if (i==0)
return 0;
else
{
DP[i]=0;
for (int j=0;j<i;j++)
if (a[j]<a[i])
DP[i]=max(DP[i],TopDownLIS(j)+1);
return DP[i];
}
}
17
Classic DP Problems
 LIS Problem:
Bottom-Up Implementation:
int DP[1000];
int a[1000];
int BottomUpLIS(int n)
{
DP[0]=0;
for (int i=1;i<=n;i++)
for (int j=0;j<i;j++)
if (a[j]<a[i])
DP[i]=max(DP[i],DP[j]+1);
return DP[n];
}
18
Classic DP Problems
 0/1 Knapsack Problem:
Given n items, each item has a weight and a value. What is the maximum
possible value we can get by taking items with total weight of W or less.
Ex: W=6 , weight={2,4,5}, value={2,5,6}, the answer is 7 (2+5).

 Steps for finding a solution:


1) The
Representing
state is (number
the givenof problem
processedasitems
states,
i, weight
where the
of chosen
answeritems
of each
w),state
state (i,w
(i,w) depends
depends on states
oneonor
states
(i-1,w)
more(i-1,w)
smaller
and (i-1,w-weight[i]).
andstates.
(i-1,w-weight[i]).
2) The
Finding
basethe
cases
baseare
cases.
(0,0…W),
i.e. the
the
states
answer
thatishave
0. obvious answers.
3) Writing the DP Formula
19
Classic DP Problems
 0/1 Knapsack Problem:
Top-Down Implementation:
int DP[1000][1000];
int weight[1000];
int value[1000];
int TopDownKnapsack(int i,int w)
{
if (DP[i][w]!=-1)
return DP[i][w];
else if (i==0)
return 0;
else
{
DP[i][w]=TopDownKnapsack(i-1,w);
if (weight[i]<=w)
DP[i][w]=max(DP[i][w],TopDownKnapsack(i-1,w-weight[i])+value[i]);
return DP[i][w];
}
}
20
Classic DP Problems
 0/1 Knapsack Problem:
Bottom-Up Implementation:
int DP[1000][1000];
int weight[1000];
int value[1000];
int BottomUpKnapsack(int n,int W)
{
for (int i=0;i<=W;i++)
DP[0][i]=0;
for (int i=1;i<=n;i++)
{
for (int j=0;j<=W;j++)
{
DP[i][j]=DP[i-1][j];
if (weight[i]<=j)
DP[i][j]=max(DP[i][j],DP[i-1][j-weight[i]]+value[i]);
}
}
return DP[n][W];
}
21
Classic DP Problems
 LCS Problem:
Given two strings a,b. what is the longest common subsequence of a and b.
Ex: a=“abcde” b=“dabce”, the answer is

 Steps
Steps for
for finding
finding a
a solution:
solution:
1)
1) The
Representing
The state
state is
is (number
(number
the given
of
of problem
processed
processed ascharacters
characters
states, wherefrom
fromthe
a,
a, answer
number
numberof of
ofeach
processed
state
processed
depends
characters
characters
on one from
from
or more
b),
b), (i,j)
(i,j)
smaller
depends
dependsstates.
on
on (i-1,j)
(i-1,j) and
and (i,j-1)
(i,j-1) and
and (i-1,j-
(i-1,j-
1).
1).
2) Finding the base cases. i.e. the states that have obvious answers.
2)
2) The
Finding
The base
basethe
case
case
base
is
is (0,0),
cases.
(0,0), the
the
i.e.answer
answer
the states
is
is 0.
0.that have obvious answers.
3) Writing the DP formula.
3)
3) Writing the DP formula.
22
Classic DP Problems
 LCS Problem:
Top-Down Implementation:
string a,b;
int DP[1000][1000];
int TopDownLCS(int i,int j)
{
if (DP[i][j]!=-1)
return DP[i][j];
else if (i==0 && j==0)
return 0;
else
{
DP[i][j]=0;
if (i>0 && j>0 && a[i-1]==b[j-1])
DP[i][j]=TopDownLCS(i-1,j-1)+1;
else
{
if (i>0)
DP[i][j]=max(DP[i][j],TopDownLCS(i-1,j));
if (j>0)
DP[i][j]=max(DP[i][j],TopDownLCS(i,j-1));
}
}
}
23
Classic DP Problems
 LCS Problem:
Bottom-Up Implementation:
string a,b;
int DP[1000][1000];
int BottomUpLCS(int n,int m)
{
DP[0][0]=0;
for (int i=0;i<=n;i++)
{
for (int j=0;j<=m;j++)
{
if (i==0 && j==0)
continue;
if (i>0 && j>0 && a[i-1]==a[j-1])
DP[i][j]=DP[i-1][j-1]+1;
else
{
if (i>0)
DP[i][j]=max(DP[i][j],DP[i-1][j]);
if (j>0)
DP[i][j]=max(DP[i][j],DP[i][j-1]);
}
}
}
return DP[n][m];
}
Training Problems:
24
Training Problems:
25
Training Problems:
26
Training Problems:
27
Training Problems:
28
Training Problems:
29
Training Problems:
30
Training Problems:
31
Training Problems:
32

You might also like