doubt in Dynamic programming.
what is meant by dynamic programming?explain with example?And what is its advantage over divide and conquer algorithm?
😁
Member • May 1, 2010
Member • May 1, 2010
we can solve overlapping problemswhat are overlapping problems?
Member • May 1, 2010
What i have studied that a recursive algorithm for the problem solves the same subproblem again and again ,rather than generating the new subproblems.So, when a recursive algorithm revisit again and again then it is said that the optimization problem has overlapping subproblems.goyal420what are overlapping problems?
Member • May 2, 2010
Member • May 3, 2010
Optimal substructure is about the optimal solutions which we will get.sushant005Hi Sahithi pallavi,
Thank you.
I have cleared most of my doubts but still have some doubts.
We know that dynamic programming is applicable to those problem that posses two properties ;-
1) Optimal substructure property.
2) Overlapping Subproblems property.
please make me clear about what is Optimal substructure.
what are overlapping problems?goyal,
Member • May 3, 2010
Member • May 3, 2010
what is botton -up fashion.Starting from solution of sub problems ---------------------------> to reach solution of main problems
Member • May 4, 2010
Member • May 4, 2010
[FONT="]#include<iostream.h>[/FONT] [FONT="]#include<conio.h>[/FONT] [FONT="]#define infinite 100000[/FONT] [FONT="]int check(int store[],int d[],int,int);[/FONT] [FONT="]int min(int,int);[/FONT] [FONT="]void main()[/FONT] [FONT="]{[/FONT] [FONT="]int last,key,*a[3],d[3],i,amount,j,no,dk,*store,amt,amp;[/FONT] [FONT="]cout<<"Enter amount"<<endl;[/FONT] [FONT="]cin>>amount;[/FONT] [FONT="]amt=amount;[/FONT] [FONT="]amp=amount;[/FONT] [FONT="]for(i=0;i<3;i++)[/FONT] [FONT="]{[/FONT] [FONT="]a[i]=new int[amount+1];[/FONT] [FONT="]}[/FONT] [FONT="]cout<<"Please enter the type of coins available in increasing order"<<endl;[/FONT] [FONT="]for(i=0;i<3;i++)[/FONT] [FONT="]{[/FONT] [FONT="]cin>>d[i];[/FONT] [FONT="]}[/FONT] [FONT="]for(i=0;i<3;i++)[/FONT] [FONT="]{[/FONT] [FONT="] for(j=0;j<=amount;j++)[/FONT] [FONT="] {[/FONT] [FONT="] a[i][0]=0;[/FONT] [FONT="] if(i==0&j<d[i])[/FONT] [FONT="] {[/FONT] [FONT="] a[i][j]=infinite;[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] if(i==0)[/FONT] [FONT="] {[/FONT] [FONT="] a[i][j]=1+a[i][j-d[i]];[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] {[/FONT] [FONT="] if(j<d[i])[/FONT] [FONT="] {[/FONT] [FONT="] a[i][j]=a[i-1][j];[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] {[/FONT] [FONT="] a[i][j]=min(a[i-1][j],1+a[i][j-d[i]]);[/FONT] [FONT="] }[/FONT] [FONT="] }[/FONT] [FONT="] }[/FONT] [FONT="]}[/FONT] [FONT="]cout<<"Required table is"<<endl;[/FONT] [FONT="]for(i=0;i<3;i++)[/FONT] [FONT="]{[/FONT] [FONT="]cout<<endl;[/FONT] [FONT="]for(j=0;j<=amount;j++)[/FONT] [FONT="]{[/FONT] [FONT="]cout<<a[i][j]<<" ";[/FONT] [FONT="]}[/FONT] [FONT="]}[/FONT] [FONT="]for(i=0;i<3;i++)[/FONT] [FONT="]{[/FONT] [FONT="]for(j=0;j<=amount;j++)[/FONT] [FONT="]{[/FONT] [FONT="]if(!(i==2))[/FONT] [FONT="] {[/FONT] [FONT="] if(a[i][amount]>a[i+1][amount])[/FONT] [FONT="] {[/FONT] [FONT="] dk=i+1;[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] {[/FONT] [FONT="] dk=i;[/FONT] [FONT="] }[/FONT] [FONT="]}[/FONT] [FONT="]}[/FONT] [FONT="]}[/FONT] [FONT="]cout<<"Value of dk"<<dk<<endl;[/FONT] [FONT="]key=dk;[/FONT] [FONT="]store=new int[dk+1];[/FONT] [FONT="]for(int m=0;m<=dk;m++)[/FONT] [FONT="]{[/FONT] [FONT="]store[m]=0;[/FONT] [FONT="]}[/FONT] [FONT="]for(i=dk;i>=0;i--)[/FONT] [FONT="]{[/FONT] [FONT="]for(j=0;j<=amount;j++)[/FONT] [FONT="]{[/FONT] [FONT="] if(a[i][j]*d[i]>amt)[/FONT] [FONT="] { [/FONT] [FONT="] store[i]=a[i][j-1];[/FONT] [FONT="] cout<<a[i][j-1]<<" "<<"mine"<<endl;[/FONT] [FONT="] amt=amt-d[i]*store[i];[/FONT] [FONT="] break;[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] {[/FONT] [FONT="] if(a[i][j]*d[i]==amt)[/FONT] [FONT="] {[/FONT] [FONT="] store[i]=a[i][j];[/FONT] [FONT="] cout<<a[i][j]<<" "<<"mine"<<endl;[/FONT] [FONT="] amt=amt-d[i]*store[i];[/FONT] [FONT="] break;[/FONT] [FONT="] }[/FONT] [FONT="] }[/FONT] [FONT="] }[/FONT] [FONT="]}[/FONT] [FONT="]cout<<endl;[/FONT] [FONT="]cout<<"coins are"<<endl;[/FONT] [FONT="]int chk=check(store,d,amp,dk);[/FONT] [FONT="]if(chk==0)[/FONT] [FONT="]{[/FONT] [FONT="]cout<<"You can't get a change with these coins sorry"<<endl;[/FONT] [FONT="]}[/FONT] [FONT="]else[/FONT] [FONT="]{[/FONT] [FONT="] for(i=0;i<=dk;i++)[/FONT] [FONT="] {[/FONT] [FONT="] cout<<"we want "<<store[i]<<"--"<<d[i]<<"type of coin"<<endl;[/FONT] [FONT="] }[/FONT] [FONT="]}[/FONT] [FONT="]}[/FONT] [FONT="]int min(int a,int b)[/FONT] [FONT="]{[/FONT] [FONT="] if(a>b)[/FONT] [FONT="] {[/FONT] [FONT="] return b;[/FONT] [FONT="] }[/FONT] [FONT="] else[/FONT] [FONT="] {[/FONT] [FONT="] return a;[/FONT] [FONT="] }[/FONT] [FONT="]}[/FONT] [FONT="]int check(int store[],int d[],int amount,int dk)[/FONT] [FONT="]{[/FONT] [FONT="]int i,sum=0;[/FONT] [FONT="]for(i=0;i<=dk;i++)[/FONT] [FONT="]{[/FONT] [FONT="]sum=sum+store[i]*d[i];[/FONT] [FONT="]}[/FONT] [FONT="]if(sum==amount)[/FONT] [FONT="]{[/FONT] [FONT="]return 1;[/FONT] [FONT="]}[/FONT] [FONT="]else[/FONT] [FONT="]{[/FONT] [FONT="]return 0;[/FONT] [FONT="]}[/FONT]You can also follow Introduction to algorithm by cormen .it is best
Member • May 4, 2010
Member • May 4, 2010
Member • May 5, 2010
Member • May 5, 2010
Member • May 5, 2010
Member • May 5, 2010
Member • May 5, 2010
Member • May 5, 2010
Yes exactly! The name itself indicates that the method should be greedy. So, its just a try to get an optimal solution. If we get the solution, it will be the best. Otherwise, we will opt for another method. So, It doesn't guarantee the optimal solution.sushant005It means that greedy approach doesnot guarantee the optimal solution.
Member • May 5, 2010
In case of divide and conquer solve the eachBingo very right 😀
subproblem individually and recursively but here the subproblems are independent but in case of DP the subproblems are not independent, that means subproblems share subsubproblems.
Member • May 5, 2010
The DP supports the memoization concept.mohit007kumar003."in DP does not take place any repetition" then how we calculate the solution in just once execution?
Member • May 5, 2010
Member • May 6, 2010
In dynamic programming we have to calculate all the solution of a particular problem.
then i think that dynamic programming is slowest among all the tech of algorithm.
but after first execution it stores the data of every execution.
so i think it,s not slower when we execute it for 2nd time.
Member • May 6, 2010
Member • Dec 16, 2010