AOA Exp10
AOA Exp10
Semester
Subject
Analysis of Algorithms
Lectures
charge
Professor
In-
Practicals
Charge
Professor
In-
Laboratory number
Student Name
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)
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.