Ex P e R Ime N T4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

EXPERIMENT 4

DATE:OBJECTIVE:- Implement 0/1 Knapsack Problem using Dynamic Programming method.


SOFTWARE USED:- Turbo C
PROCEDURE:#include<stdio.h>
#include<conio.h>
#define MAX 20
void knapsackDP(int,int);
int max(int,int);
void backtracking();
int weight[MAX],value[MAX],W,no,*x;
int v[MAX][MAX];
void main()
{
int i,j;
clrscr();
printf("\n Student name:- Rohit Singh");
printf("\n Enter number of Object :");
scanf("%d",&no);
printf("\n Enter weight and values in ascending order ");
for(i=1;i<=no;i++)
{
printf("\n Enter Weight and Value for Object %d:",i);
scanf("%d %d",&weight[i],&value[i]);
}
printf("\n Enter knapsack Capacity:");

scanf("%d",&W);
knapsackDP(no,W);
backtracking();
getch();
}
void knapsackDP(int no,int W)
{
int i,j;
for(i=0;i<= W ;i++)
v[0][i]=0;
for(i=0;i<= no;i++)
v[i][0]=0;
for(i=1;i<= no;i++)
{
for(j=1;j<= W;j++)
{
if((j-weight[i])< 0)
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-weight[i]]+value[i]);
}
}
printf("\n \t

");

for(i=0;i<= W;i++)
printf("%2d ",i);

printf("\n-----------------------------------------------------------------------------");
for(i=0;i<=no;i++)

{
printf("\n w%d=%2d v%d=%2d |",i,weight[i],i,value[i]);
for(j=0;j<= W;j++)
printf("%2d ",v[i][j]);
}
printf("\n-----------------------------------------------------------------------------");
printf("\n Maximum value carry by knapsack is:%2d",v[no][W]);
printf("\n-----------------------------------------------------------------------------");
}
int max(int a,int b)
{
return (a >b)?a:b;
}
void backtracking()
{
int j1,i;
j1=W;
printf("\nIncluded Object \t weight \t value");
printf("\n-----------------------------------------------------------------------------");
for(i=no;i >=0;i--)
{

if(v[i][j1]!=v[i-1][j1] && (v[i][j1]==v[i-1][j1-weight[i]]+value[i]))


{
printf("\n%2d \t\t\t %2d \t\t %2d",i,weight[i],value[i]);
j1=j1-weight[i];
}
}}

OUTPUT

SIGNATURE:-

EXPERIMENT 8
DATE:
OBJECTIVE: Consider the problem of N queen on an (NxN) chessboard. Two queens are said to
attack each other if they are on the same row, column, or diagonal. WAP to implement
backtracking algorithm to solve the problem i.e. place N non-attacking queens on the board.
SOFTWARE USED: Turbo C
PROCEDURE:
#include<stdio.h>
#include<math.h>
#include<conio.h>
char a[10][10];
int n;
void printmatrix()
{
int i, j;
printf("\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf("%c\t", a[i][j]);
printf("\n\n");
}
}
int getmarkedcol(int row)
{

int i;
for (i = 0; i < n; i++)
{
if (a[row][i] == 'Q')
{
return (i);
break;
}
}
}
int feasible(int row, int col)
{
int i, tcol;
for (i = 0; i < n; i++)
{
tcol = getmarkedcol(i);
if (col == tcol || abs(row - i) == abs(col - tcol))
return 0;
}
return 1;
}
void nqueen(int row)
{
int i, j;
if (row < n)
{
for (i = 0; i < n; i++)
{
if (feasible(row, i))

{
a[row][i] = 'Q';
nqueen(row + 1);
a[row][i] = '.';
}
}
}
else
{
printf("\nThe solution is:- ");
printmatrix();
}
}
void main()
{ clrscr(
); int i,
j;
printf("Student Name: Rohit Singh");
printf("\nEnter the no. of queens:- ");
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] = '.';
nqueen(0);
getch();
}

OUTPUT:-

SIGNATURE:

EXPERIMENT 9
DATE:
OBJECTIVE: WAP to implement Knapsack Problem based on Backtracking algorithm.
SOFTWARE USED: Turbo C
PROCEDURE:
#include <stdio.h>
#include <conio.h>
#define max 10
int w[max],i,j,p[max];
int n,m;
float unit[max];
int y[max],x[max],fp=-1,fw;
void get()
{
printf("\n Enter total number of items: ");
scanf("%d",&n);
printf("\n Enter the Maximum capacity of the Sack:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("\n Enter the weight of the item # %d : ",i+1);
scanf("%d",&w[i]);
printf("\n Enter the profit of the item # %d : ", i+1);
scanf("%d", &p[i]);
}
}
void show()

{p[i] = p[j];
float s=0.0;
printf("\n\tItem\tWeight\tCost\tUnit Profit\tSelected ");
for(i=0;i<n;i++) printf("\n\t%d\t%d\t%d\t%f\t
%d",i+1,w[i],p[i],unit[i],x[i]); printf("\n\n The Sack now
holds following items : "); for(i=0;i<n;i++)
if(x[i]==1)
{
printf("%d\t",i+1);
s += (float) p[i] * (float) x[i];
}
printf("\n Maximum Profit: %f ",s);
}
void sort()
{
int t,t1; float t2;
for(i=0;i<n;i++)
unit[i] = (float) p[i] / (float) w[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(unit[i] < unit[j])
{
t2 = unit[i];
unit[i] = unit[j];
unit[j] = t2;
t = p[i];

p[j] = t;
t1 = w[i];
w[i] = w[j];
w[j] =t1;
}
}
}
}
float bound(float cp,float cw,int k)
{
float b = cp; float
c = cw;
for(i=k;i<=n;i++)
{
c = c+w[i];
if( c < m)
b = b +p[i];
else
return (b+(1-(c-m)/ (float)w[i])*p[i]);
}
return b;
}
void knapsack(int k,float cp,float cw)
{
if(cw+w[k] <= m)
{
y[k] = 1; if(k <= n)
knapsack(k+1,cp+p[k],cw+w[k]);
if(((cp+p[k]) > fp) && ( k == n))

{OUTPUT:fp = cp+p[k]; fw
= cw+w[k];
for(j=0;j<=k;j++)
x[j] = y[j];
}
}
if(bound(cp,cw,k) >= fp)
{
y[k] = 0; if( k <= n)
knapsack(k+1,cp,cw);
if((cp > fp) && (k == n))
{
fp = cp; fw = cw;
for(j=0;j<=k;j++)
x[j] = y[j];
}
}
}
void main()
{
clrscr();
printf("Student Name: Rohit Singh\n");
get(); sort();
knapsack(0,0.0,0.0);
show();
getch();
}

SIGNATURE:

EXPERIMENT 10
DATE:
OBJECTIVE: WAP to implement Traveling Salesman problem based on Branch and Bound
technique.
SOFTWARE USED: Turbo C
PROCEDURE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("\nEnter Number of Cities: ");
scanf("%d",&n);
printf("\nEnter Cost Matrix: \n");
for( i=0;i<n;i++)
{
printf("\n Enter Elements of Row # : %d\n",i+1);
for( j=0;j<n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}
printf("Student Name: Rohit Singh");
printf("\n\nThe Cost Matrix:\n");
for( i=0;i<n;i++)

{
printf("\n\n");
for( j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
}
void mincost(int city)
{
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("\n\nMinimum cost:");
printf("%d",cost);
}
void main()
{ clrscr(
); get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
getch();
}

OUTPUT:-

SIGNATURE:

You might also like