Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Subset Sum Problem Implementation using Dynamic Programming - DAA Lab Worksheet, Assignments of Data Structures and Algorithms

Daa worksheet experiment 4 for chandigarh university full document with answer

Typology: Assignments

2022/2023

Uploaded on 10/28/2022

aaditya-ranjan
aaditya-ranjan 🇮🇳

3 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet -2.2
Name: Vivek Kumar
UID: 20BCS5690
Branch: BE-CSE
Semester: 5th
Section/Group: WM-613-B
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].
pf3
pf4

Partial preview of the text

Download Subset Sum Problem Implementation using Dynamic Programming - DAA Lab Worksheet and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Worksheet - 2.

Name: Vivek Kumar UID: 20BCS

Branch: BE-CSE

Semester: 5th

Section/Group: WM- 613 - B

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

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.