Daa 6
Daa 6
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>
if (j == 0) {
cout << "{ ";
for (int num : subset) {
cout << num << " ";
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (i == 0) return;
if (dp[i-1][j]) {
printSubsets(arr, dp, i-1, j, subset);
}
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.