0% found this document useful (0 votes)
79 views5 pages

DAA Worksheet Exp-2.2

The document is a lab report submitted by Anant Kumar Mathur for their DAA lab course. It details an experiment implementing the subset sum problem using dynamic programming. A boolean 2D array is used to store solutions in a bottom-up manner, with the value at subset[i][j] being true if there is a subset of the first j elements summing to i. The algorithm runs in O(n*sum) time and O(sum) space. When run on a sample input array, the program outputs whether a subset with the given sum exists or not.

Uploaded by

amal suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views5 pages

DAA Worksheet Exp-2.2

The document is a lab report submitted by Anant Kumar Mathur for their DAA lab course. It details an experiment implementing the subset sum problem using dynamic programming. A boolean 2D array is used to store solutions in a bottom-up manner, with the value at subset[i][j] being true if there is a subset of the first j elements summing to i. The algorithm runs in O(n*sum) time and O(sum) space. When run on a sample input array, the program outputs whether a subset with the given sum exists or not.

Uploaded by

amal suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIVERSITY INSTITUTE OF ENGINEERING

Department of Computer Science & Engineering

Subject Name: DAA Lab


Subject Code: 20ITP-312

Submitted to: Submitted by:


Er. Smita Agrawal Name: Anant Kumar Mathur

(E13323) UID: 20BET1071

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

1. Aim/Overview of the practical:

To implement subset-sum problem using Dynamic Programming .

2. Task to be done/ Which logistics used:

find whether or not there exists any subset of the given set .

3. Algorithm/Flowchart:

i. We create a boolean subset[][] and fill it in bottom up manner.

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.

iii. subset[i][j] = true if there is a subset with:

iv. the i-th element as the last element * sum equal to j

v. subset[i][0] = true as sum of {} = 0

vi. subset[0][j] = false as with no elements we can get no sum

vii. subset[i][j] = subset[i-1][j-E1]; where E1 = array[i-1]

viii. Finally, we return subset[n][sum].


4. Steps for experiment/practical/Code:
#include<iostream>
using namespace std;

bool subsetsum_DP(int a[],int n, int sum)


{
bool dp[n+1][sum+1];
int i,j;

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;
}

5. Observations/Discussions/ Complexity Analysis:

• Worst case time complexity: Θ(n*sum)

• Space complexity: Θ(sum)

6. Result/Output/Writing Summary:
Learning Outcomes:-
1. Create a program keeping in mind the time complexity

2. Create a program keeping in mind the space complexity

3. Steps to make optimal algorithm

4. Learnt about how to implement subset sum problem using dynamic programming.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy