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

AOA Exp10

This document describes an experiment on solving the subset sum problem using backtracking. The subset sum problem is to find a subset of elements from a given set that sum to a given number K, where the set contains non-negative integers. The experiment implements a backtracking algorithm in C to find all subsets that sum to the required number. It takes in the number of elements, the elements, and required sum as input. It then uses recursion and backtracking to try all possible subsets and prints any that equal the required sum. The conclusions note this problem arises in practical applications like loading a truck of maximum weight.

Uploaded by

Sakharam Gawade
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)
30 views4 pages

AOA Exp10

This document describes an experiment on solving the subset sum problem using backtracking. The subset sum problem is to find a subset of elements from a given set that sum to a given number K, where the set contains non-negative integers. The experiment implements a backtracking algorithm in C to find all subsets that sum to the required number. It takes in the number of elements, the elements, and required sum as input. It then uses recursion and backtracking to try all possible subsets and prints any that equal the required sum. The conclusions note this problem arises in practical applications like loading a truck of maximum weight.

Uploaded by

Sakharam Gawade
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 ENGINEERING

Semester

S.E. Semester IV Computer Engineering

Subject

Analysis of Algorithms

Lectures
charge

Professor

In-

Practicals
Charge

Professor

In-

Laboratory number
Student Name

Prof. Sanjeev Dwivedi


Prof. Ashwin Ganesan
L07D

Sakharam S. Gawade

Roll Number

14102C2015

Grade

Subject Teachers
Signature

Experiment
Number

10

Experiment
Title

Sum of subset

Resources
Apparatus
Used

Computer, Code::Blocks(IDE)

Objectives
Backtracking approach
(Skill
Set/
Knowledge
tested/imparte
d)

Description of the experiment:


Subset sum problem is to find subset of elements that are selected from a
given set whose sum adds up to a given number K. We are considering the

set contains non-negative values. It is assumed that the input set is unique
(no duplicates are presented).

Program:
#include<stdio.h>

void sumset(int );
int proper(int ,int );
int sum(int);
int v[10];
int x[10];
int n,sumreq;
void main()
{

int i;
printf("Enter no. of elements\n");
scanf("%d",&n);
printf("Enter unique elements in increasing order \n");
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
printf("Enter the sum required \n");
scanf("%d",&sumreq);
printf("Subsets are =\n");
sumset(1);

}
int sum(int n)

{
int s,i;
s=0;
for(i=1;i<=n;i++)
s=s+x[i];
return s;
}
int proper(int k,int i)
{
int j;
for(j=1;j<=k-1;j++)
{
if(x[j]==v[i])
return 0;
}
if(sum(k-1)+v[1]>sumreq)
return 0;
else
return 1;
}

void sumset(int k)
{
int i,s,j;
for(i=k;i<=n;i++)
{
if(proper(k,i))
{
x[k]=v[i];

s=sum(k);
if(s==sumreq)
{
for(j=1;j<=k;j++)
printf("%d ",x[j]);
printf("\n");
}
else
sumset(k+1);
}
}
}

Output run:

Conclusions:
This problem arises in practical applications. Similar to the knapsack problem
we may have a truck that can carry at most t pounds and we have n dierent
boxes to ship and the ith box weighs xi pounds.

You might also like