You are on page 1of 5

DynamicProgramming|Set1

(OverlappingSubproblems
Property)
DynamicProgrammingisanalgorithmicparadigmthatsolvesa
givencomplexproblembybreakingitintosubproblemsandstores
theresultsofsubproblemstoavoidcomputingthesameresults
again.Followingarethetwomainpropertiesofaproblemthat
suggestthatthegivenproblemcanbesolvedusingDynamic
programming.
1)OverlappingSubproblems
2)OptimalSubstructure
1)OverlappingSubproblems:
LikeDivideandConquer,DynamicProgrammingcombinessolutions
tosubproblems.DynamicProgrammingismainlyusedwhen
solutionsofsamesubproblemsareneededagainandagain.In
dynamicprogramming,computedsolutionstosubproblemsare
storedinatablesothatthesedonthavetorecomputed.So
DynamicProgrammingisnotusefulwhentherearenocommon
(overlapping)subproblemsbecausethereisnopointstoringthe
solutionsiftheyarenotneededagain.Forexample,Binary
Searchdoesnthavecommonsubproblems.Ifwetakeexampleof
followingrecursiveprogramforFibonacciNumbers,therearemany
subproblemswhicharesolvedagainandagain.
/*simplerecursiveprogramforFibonaccinumbers*/
intfib(intn)
{
if(n<=1)
returnn;
returnfib(n1)+fib(n2);
}
Recursiontreeforexecutionoffib(5)


fib(5)
/\
fib(4)fib(3)
/\/\
fib(3)fib(2)fib(2)fib(1)
/\/\/\
fib(2)fib(1)fib(1)fib(0)fib(1)fib(0)
/\
fib(1)fib(0)

Wecanseethatthefunctionf(3)isbeingcalled2times.Ifwewould
havestoredthevalueoff(3),theninsteadofcomputingitagain,we
wouldhavereusedtheoldstoredvalue.Therearefollowingtwo
differentwaystostorethevaluessothatthesevaluescanbe
reused.
a)Memoization(TopDown):
b)Tabulation(BottomUp):
a)Memoization(TopDown):Thememoizedprogramforaproblem
issimilartotherecursiveversionwithasmallmodificationthatit
looksintoalookuptablebeforecomputingsolutions.Weinitializea
lookuparraywithallinitialvaluesasNIL.Wheneverweneed
solutiontoasubproblem,wefirstlookintothelookuptable.Ifthe
precomputedvalueistherethenwereturnthatvalue,otherwisewe
calculatethevalueandputtheresultinlookuptablesothatitcanbe
reusedlater.
FollowingisthememoizedversionfornthFibonacciNumber.
/*MemoizedversionfornthFibonaccinumber*/
#include<stdio.h>
#defineNIL1
#defineMAX100

intlookup[MAX];

/*FunctiontoinitializeNILvaluesinlookuptable*/
void_initialize()
{
inti;
for(i=0;i<MAX;i++)
lookup[i]=NIL;
}

/*functionfornthFibonaccinumber*/
intfib(intn)
{
if(lookup[n]==NIL)
{
if(n<=1)
lookup[n]=n;
else
lookup[n]=fib(n1)+fib(n2);
}

returnlookup[n];
}

intmain()
{
intn=40;
_initialize();
printf("Fibonaccinumberis%d",fib(n));
getchar();
return0;
}
b)Tabulation(BottomUp):Thetabulatedprogramforagiven
problembuildsatableinbottomupfashionandreturnsthelastentry
fromtable.
/*tabulatedversion*/
#include<stdio.h>
intfib(intn)
{
intf[n+1];
inti;
f[0]=0;f[1]=1;
for(i=2;i<=n;i++)
f[i]=f[i1]+f[i2];


returnf[n];
}

intmain()
{
intn=9;
printf("Fibonaccinumberis%d",fib(n));
getchar();
return0;
}
BothtabulatedandMemoizedstorethesolutionsofsubproblems.In
Memoizedversion,tableisfilledondemandwhileintabulated
version,startingfromthefirstentry,allentriesarefilledonebyone.
Unlikethetabulatedversion,allentriesofthelookuptablearenot
necessarilyfilledinmemoizedversion.Forexample,memoized
solutionofLCSproblemdoesntnecessarilyfillallentries.
Toseetheoptimizationachievedbymemoizedandtabulated
versionsoverthebasicrecursiveversion,seethetimetakenby
followingrunsfor40thFibonaccinumber.
Simplerecursiveprogram
Memoizedversion
tabulatedversion
Alsoseemethod2ofUglyNumberpostforonemoresimple
examplewherewehaveoverlappingsubproblemsandwestorethe
resultsofsubproblems.
WewillbecoveringOptimalSubstructurePropertyandsomemore
exampleproblemsinfuturepostsonDynamicProgramming.
Tryfollowingquestionsasanexerciseofthispost.
1)WriteamemoizedversionforLCSproblem.Notethatthetabular
versionisgivenintheCLRSbook.
2)HowwouldyouchoosebetweenMemoizationandTabulation?

Pleasewritecommentsifyoufindanythingincorrect,oryouwantto
sharemoreinformationaboutthetopicdiscussedabove.
References:
http://www.youtube.com/watch?v=V5hZoJ6uKs

You might also like