Translate

Labels

Friday 24 May 2013

KNAPSACK PROBLEM

C PROGRAM TO IMPLEMENT KNAPSACK PROBLEM  USING BACKTRACKING

#include<stdio.h>
#include<conio.h>
float final_profit=-1.0;
intp[9]={0,11,21,31,33,43,53,55,65};
int w[9]={0,1,11,21,23,33,43,45,55};
int m=110;
int n=8;
int temp[9],x[9];
float final_wt=-1.0;
float bound_calculation(int cp,int cw,int k)
{
int ub,c,i;
ub=cp;
c=cw;
for(i=k+1;i<n;i++)
{
c=c+w[i];
if(c<m)
ub=ub+p[i];
else
return(ub+(1-(c-m)/w[i]*p[i]));
}
return ub;
}
void BK(int k,int cp,int cw)
{
int new_k,new_cp,new-cw,j;
//Generate left child
if(cw+w[k]<=m)
{
temp[k]=1;
if(k<n)
{
new_k=k+1;
new_cp=cp+p[k];
new_cw=cw+w[k];
BK(new_k,new_cp,new_cw);
}
if((new_cp>final_profit)&&(k==n))
{
final_profit=new_cp;
final_wt=new_cw;
for(j=1;j<k;j++)
x[j]=temp[j];
}
}

//Generate right child
if(Bound_calculation(cp,cw,k)>==final_profit)
{
temp[k]=0;
if(k<n)
BK(k+1,cp,cw);
if((cp>final_profit)&&(k==n))
{
final_profit=cp;
final_wt=cw;
for(j=1;j<n;j++)
x[j]=temp[j];
}
}
}
void main()
{
int i;
clrscr();
printf("\n\t Knapsack problem using backtracking algorithm \n");
printf("\n Capacity of knapsack=%d",m);
printf("\n\n Profit\t Weight");
printf("\n-------------------------------");
for(i=1;i<=n;i++)
printf("\n%d\t%d",p[i],w[i]);
printf("\n-------------------------------");
BK(1,0,0);
printf("\n Following items are selected from knapsack...");
for(i=1;1<=n;i++)
{
if(x[i]==1)
printf("\n\t\t Item %d",i);
}
printf("\n\n Final Weight =%0.2f",final_wt);
printf("\n Final Profit=%0.2f",final_profit);
getch();
}

OUTPUT:
Knapsack problem using Backtracking algorithm

capacity of knapsack=110

Profit weight
-----------------------------
11     1
21     11
31     21
33     23
43     33
53     43
55     45
65     55
-----------------------------
Following item are selected from knapsack...
Item 1
Item 2
Item 3
item 5
Item 6
Final weight=109.00
final Profit=159.00

Thursday 16 May 2013

DIFFERENCE BETWEEN TSR AND TSO PROGRAM

TSO means terminate but stay outside.It is those programs which release the memory after the execution of the program.Eg VCD cutter,turbo c compiler.TSR means terminate but stay residence.It is those programs which after the execution of the program does not release the RAM(main memory).Eg antivirus 

n-QUEEN'S PROBLEM USING BACKTRACKING

/*This program is to implement n-queen's problem using backtracking*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>

int board[20];
int count;
void main()
{
int n,i,j;
void queen(int row, int n);
clrscr();
printf("\n\t Program for queen's using backtracking");
printf("Enter number of queen's ");
scanf("%d",&n);
queen(1,n);                                      //trace using backtrack
getch();
 }


/*This function is for printing the solution to n-queen's problem*/
void print_board(int n)
{
int i,j;
printf("\n\n solution %d:\n\n",++count);
//number of solution
for(i=1;i<=n;i++)
{
printf("\t%d",i);
}
for(i=1;i<=n;i++)
{
printf("\n\n%d",i);
for(j=1;j<=n;j++)        //for n*n board
{
if(board[i]==j)
printf("\tQ");   //Queen at i,j position
else
printf("\t-");   //empty slot
}
}
printf("\n Press any key to continue.....");
getch();
}
/*This function is for checking for the conflicts.If there is no conflict for the desired position it returns 1 otherwise it returns 0*/
int place(int row ,int column)
{
int i;
for(i=1;i<=row-1;i++)
{
//checking for column and diagonal conflicts 
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
//no conflicts hence Queen can be placed
return 1;
}



Tuesday 7 May 2013

QWERTY QUERY

The layout of the keyboard comes in various styles, such as QWERTY, AZERTY, and DVORAK. QWERTY is the most common layout in English language computer keyboards.It takes its name from first six letters shown on the keyboard's top row of the letters.Similarly,French language keyboard use A and Z in place of Q and W and are known as AZERTY keyboards 

WHAT'S IN THE NAME? CD BURNING

CD-Rs and CD-RWs are made up of a poly carbonate substrate, a thin metal coating, and a protective outer layer.In a CD-R a layer of organic polymer dye between the poly carbonate and the metal layers serves a a recording medium. The laser creates marks(burns) in the dye layer that mimic the reflective properties of the pits and lands(lower and higher areas) of the cd.Due to this CD writing is also known as CD BURNING