0% found this document useful (0 votes)
7 views3 pages

DAA Expt-6

The document outlines an experiment for a computer science course where students implement the subset-sum problem using Dynamic Programming. The aim is to determine if a subset of integers adds up to a target sum while improving efficiency over brute-force methods. It includes code, time complexity analysis, and learning outcomes related to problem-solving and complexity analysis.

Uploaded by

goyalsak001
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)
7 views3 pages

DAA Expt-6

The document outlines an experiment for a computer science course where students implement the subset-sum problem using Dynamic Programming. The aim is to determine if a subset of integers adds up to a target sum while improving efficiency over brute-force methods. It includes code, time complexity analysis, and learning outcomes related to problem-solving and complexity analysis.

Uploaded by

goyalsak001
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-6
Student Name: AMIT KUMAR UID: 22BCS10936
Branch: BE-CSE Section/Group: 22BCS_IOT-639-A
Semester: 5th Date of Performance: 11.09.2024
Subject Name: DAA Subject Code: 22CSH-311

1. Aim
Develop a program and analyze complexity to implement subset-sum problem using
Dynamic Programming.
2. Objective
• The main objective is to find if there is a subset of the given set of integers that adds up
to a target sum.
• Implement the solution using Dynamic Programming to reduce the time complexity
compared to a brute-force approach (which would take exponential time).
3. Code
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Subset {


public static List<Integer> isSubsetSum(int[] set, int n, int target) {
boolean[][] dp = new boolean[n + 1][target + 1];

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


dp[i][0] = true;

for (int j = 1; j <= target; j++)


dp[0][j] = false;

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


for (int j = 1; j <= target; j++) {
if (set[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - set[i - 1]];
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
}
if (!dp[n][target]) {
return null;
}

List<Integer> subset = new ArrayList<>();


int i = n, j = target;
while (i > 0 && j > 0) {
if (dp[i - 1][j]) {
i--;
} else {
subset.add(set[i - 1]);
j -= set[i - 1];
i--;
}
}

return subset;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements in the set: ");
int n = sc.nextInt();
int[] set = new int[n];

System.out.println("Enter the elements of the set: ");


for (int i = 0; i < n; i++) {
set[i] = sc.nextInt();
}
System.out.print("Enter the target sum: ");
int target = sc.nextInt();

List<Integer> subset = isSubsetSum(set, n, target);

if (subset != null) {
System.out.println("A subset with the target sum exists: " + subset);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

} else {
System.out.println("No subset with the target sum exists.");
}
}
}
4. Output
amit

5. Time Complexity
Time complexity is: O(n * target)

6. Learning Outcomes
• Learn how to break a problem into smaller subproblems.
• Learn to analyze the time and space complexity of dynamin programming.
• Understand how to trace back from the DP table to extract the actual subset that
contributes to the target sum.

You might also like