Professional Documents
Culture Documents
No: 9
IMPLEMENT AND ANALYZE SUM OF SUBSETS USING
Dt: BACKTRACKING APPROACH
AIM:
To develop a c program to implement Sum of subsets using Back tracking
methodology.
ALGORITHM:
sum of subset(s,k,r)
{
X[k]=1;
if(s+W[k]=m) then write(X[1:k]);
else if (s+W[k]+W[k+1]<=m)
then sumofsubset(s+W[k],k+1,r-W[k]);
if((s=r-W[k]>=m)and(s+W[k+1]<=m)) then
{
X[k]=0;
sumofsubset(s,k+1,r-W[k]);
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
int s[10],x[10],d;
void sumofsub(int,int,int);
void main()
{
int n,sum=0;
int i;
clrscr();
printf("\nEnter the number of the subset: ");
scanf("%d",&n);
printf("Enter the number of subset increasing order\n");
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
}
printf("\n Enter the value of d:");
scanf("%d",&d);
for(i=1;i<=n;i++)
{
sum=sum+s[i];
if(sum<d&&s[1]>d)
{
printf("no subset is possible: ");
}
else
{
sumofsub(0,1,sum);
}
getch();
}
}
else
{
if(m+s[k]+s[k+1]<=d)
sumofsub(m+s[k],k+1,r-s[k]);
if((m+r-s[k]>=d)&&(m+s[k+1]<=d))
{
x[k]=0;
sumofsub(m,k+1,r-s[k]);
}
}
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement Sum of Subsets using Back Tracking approach
was executed and verified.
Ex. No: 10
IMPLEMENT AND ANALYZE TRAVELLING
Dt: SALESMAN PROBLEM USING BRANCH AND BOUND
AIM:
To develop a c program for the implementation travelling salesman problem using
branch and bound.
ALGORITHM:
For each city i, 1 <= i <=n, find the sum of the distances from city i to the two
nearest cities.
Compute the sum s of these n numbers
Divide the result by 2
If all the distances are integers, round up the result to the nearest Integer
lb = [ s/2 ]
PROGRAM:
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
int lease(int);
void get()
int i,j;
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
for(i=0;i<n;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
int i,ncity;
visited[city]=1;
printf("%d->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=a[city][ncity];
return;
mincost(ncity);
int least(int c)
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
if(min!=999)
cost+=kmin;
return nc;
void put()
printf("%d",cost);
void main()
clrscr();
get();
mincost(0);
put();
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement the travelling salesman problem using branch and
bound has been executed and verified.