0% found this document useful (0 votes)
18 views4 pages

Daa 6

Uploaded by

Yash
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)
18 views4 pages

Daa 6

Uploaded by

Yash
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-6
Student Name: Yash Pandey UID:22BCS15020
Branch: CSE Section/Group:619(A)
Semester: 5th Date of Performance:
Subject Name: DAA LAB Subject Code: 22CSH-311

1. Aim:
Develop a program and analyze complexity to implement subset-sum
problem using Dynamic Programming.
2. Objective:
The objective of this program is to determine whether a subset of a given
set of integers sums to a specified value. This problem is a classic example
of dynamic programming where we use a table to store intermediate results
and build the solution incrementally.
3. Implementation/Code:
#include <iostream>
#include <vector>

using namespace std;

void printSubsets(vector<int>& arr, vector<vector<bool>>& dp, int i, int


j, vector<int>& subset) {

if (j == 0) {
cout << "{ ";
for (int num : subset) {
cout << num << " ";
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cout << "}" << endl;


return;
}

if (i == 0) return;

if (dp[i-1][j]) {
printSubsets(arr, dp, i-1, j, subset);
}

if (j >= arr[i-1] && dp[i-1][j - arr[i-1]]) {


subset.push_back(arr[i-1]);
printSubsets(arr, dp, i-1, j - arr[i-1], subset);
subset.pop_back();
}
}

bool subsetSum(vector<int>& arr, int sum) {


int n = arr.size();
vector<vector<bool>> dp(n + 1, vector<bool>(sum + 1, false));

for (int i = 0; i <= n; ++i) {


dp[i][0] = true;
}

for (int i = 1; i <= n; ++i) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for (int j = 1; j <= sum; ++j) {


if (arr[i - 1] <= j) {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - arr[i - 1]];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}

if (!dp[n][sum]) {
return false;
}

vector<int> subset;
cout << "Subsets with the given sum are:" << endl;
printSubsets(arr, dp, n, sum, subset);

return true;
}

int main() {
vector<int> arr = {3, 34, 4, 12, 5, 2};
int sum = 9;

if (!subsetSum(arr, sum)) {
cout << "No subset with the given sum" << endl;
}

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output

5. Time Complexity :
O(n * sum) + O(2^n)
6. Learning Outcome
• Dynamic Programming concepts and how to apply them to solve problems.
• The Subset Sum Problem and how to determine if a subset with a given sum
exists within a set.
• How to analyze the time and space complexity of a dynamic programming
algorithm.
• Improved understanding of two-dimensional arrays in C++ and how to use
them for dynamic programming tables.

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