DAA Worksheet Exp-2.2
DAA Worksheet Exp-2.2
Section: 20BET-WM-601
Group: B
Worksheet Experiment – 2.2
Name: Anant Kumar Mathur UID: 20BET1071
Branch: BE-IT Section/Group: 20BET_WM-601-B
Semester: 5th Subject: DAA Lab
find whether or not there exists any subset of the given set .
3. Algorithm/Flowchart:
ii. The value of subset[i][j] will be true if there is a subset of set[0..j-1] with sum equal to i.,
otherwise false.
for(i=0;i<=n;i++)
dp[i][0]=true;
for(j=1;j<=sum;j++)
dp[0][j]=false;
for(i=1;i<=n;i++)
{
for(j=1;j<=sum;j++)
{
if(dp[i-1][j]==true)
dp[i][j]=true;
else
{
if(a[i-1]>j)
dp[i][j]=false;
else
dp[i][j]=dp[i-1][j-a[i-1]];
}
}
}
return dp[n][sum];
}
int main()
{
int set[] = { 3, 34, 4, 12, 5, 2 };
int sum = 9;
int n = sizeof(set) / sizeof(set[0]);
if (subsetsum_DP(set, n, sum) == true)
cout <<"Found a subset with given sum";
else
cout <<"No subset with given sum";
return 0;
}
6. Result/Output/Writing Summary:
Learning Outcomes:-
1. Create a program keeping in mind the time complexity
4. Learnt about how to implement subset sum problem using dynamic programming.