daa lab
DESCRIPTION
DAA LABTRANSCRIPT
![Page 1: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/1.jpg)
EX:NO:1 QUICK SORT
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
![Page 2: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/2.jpg)
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
![Page 3: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/3.jpg)
EX:NO:2 BINARY SEARCH
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
![Page 4: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/4.jpg)
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}
EX:NO:3 BINARY TREE TRAVERSAL
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *right, *left;
}*root,*p,*q;
![Page 5: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/5.jpg)
struct node *make(int y)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r,int x)
{
if(r->left!=NULL)
printf("\n Invalid !");
else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
printf("\n Invalid !");
else
r->right=make(x);
![Page 6: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/6.jpg)
}
void inorder(struct node *r)
{
if(r!=NULL)
{
inorder(r->left);
printf("\t %d",r->data);
inorder(r->right);
}
}
void preorder(struct node *r)
{
if(r!=NULL)
{
printf("\t %d",r->data);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r)
{
if(r!=NULL)
![Page 7: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/7.jpg)
{
postorder(r->left);
postorder(r->right);
printf("\t %d",r->data);
}
}
void main()
{
int no;
int choice;
clrscr();
printf("\n Enter the root:");
scanf("%d",& no);
root=make(no);
p=root;
while(1)
{
printf("\n Enter another number:");
scanf("%d",& no);
if(no==-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
![Page 8: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/8.jpg)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf("\n Left branch of %d is %d",p->data,no);
left(p,no);
}
else
{
right(p,no);
printf("\n Right Branch of %d is %d",p->data,no);
}
}
while(1)
{
printf("\n 1.Inorder Traversal \n 2.Preorder Traversal \n 3.Postorder Traversal \n 4.Exit");
printf("\n Enter choice:");
scanf("%d",&choice);
switch(choice)
{
![Page 9: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/9.jpg)
case 1 :inorder(root);
break;
case 2 :preorder(root);
break;
case 3 :postorder(root);
break;
case 4 :
default:printf("Error ! Invalid Choice ");
break;
}
getch();
}
}
EX:NO:4 WARSHALL ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
![Page 10: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/10.jpg)
for(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{ ;
if(a>b)
return(a);
else
return(b);
}
void main()
{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
![Page 11: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/11.jpg)
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}
EX:NO:5 DIJKSTRA ALGORITHM
#include "stdio.h"
#include "conio.h"
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{
![Page 12: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/12.jpg)
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
![Page 13: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/13.jpg)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
EX:NO:6 PRIMS ALGORITHM
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
![Page 14: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/14.jpg)
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
![Page 15: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/15.jpg)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
EX:NO:7 KNAPSACK-DYNAMIC PROGRAMMING
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
![Page 16: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/16.jpg)
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
![Page 17: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/17.jpg)
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("Total profit = %d\n",profit);
getch();
}
![Page 18: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/18.jpg)
EX:NO:8 SUBSET PROBLEM
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
void main()
{
int i,j,n,temp,total=0;
clrscr();
printf("\n Enter how many numbers:\n");
scanf("%d",&n);
printf("\n Enter %d numbers to th set:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];
}
![Page 19: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/19.jpg)
printf("\n Input the sum value to create sub set:\n");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("\n The given %d numbers in ascending order:\n",n);
for(i=0;i<n;i++)
printf("%d \t",w[i]);
if((total<sum))
printf("\n Subset construction is not possible");
else
{
for(i=0;i<n;i++)
inc[i]=0;
printf("\n The solution using backtracking is:\n");
sumset(-1,0,total);
}
getch();
}
void sumset(int i,int wt,int total)
![Page 20: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/20.jpg)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("\n{\t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%d\t",w[j]);
printf("}\n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
EX:NO:9 TRAVELLING SALESMAN PROBLEM
include<stdio.h>
![Page 21: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/21.jpg)
#include<conio.h>
#include<stdlib.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("----------------------------------------------------------------------\n");
printf("-------------------made by C code champ ------------------------------\n");
printf("----------------------------------------------------------------------\n");
printf("\n\n\t TRAVELLING SALESMAN PROBLEM SOLUTION IN C\n");
printf("\n\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("\n\nThe Cost Matrix is:\n");
for( i=0;i<n;i++)
![Page 22: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/22.jpg)
{
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);
![Page 23: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/23.jpg)
}
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:");
![Page 24: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/24.jpg)
printf("%d",cost);
}
int main()
{
get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
getch();
}
EX:NO:10 STRASSEN PROBLEM
#include<stdio.h>
int main(){
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements of second matrix: ");
for(i=0;i<2;i++)
![Page 25: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/25.jpg)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
![Page 26: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/26.jpg)
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
EX:NO:11 KNAPSACK –BRANCH AND BOUND
#include
#include
#include
int fnBound(int iCp,int iCw,int iK,int iMax,int aiP[9],int iItem,int aiW[9])
{
float iUb;
![Page 27: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/27.jpg)
if(iK+1 iUb=iCp+(iMax - iCw)*(aiP[iK+1]/(float)aiW[iK+1]);
else
iUb=iCp;
return iUb;
}
void fnBranch_bound(int iK,int iCp,int iCw,int iMax,int aiW[9],int aiPr[9],int iItem)
{
static int sSol[9];
static int sFp,sFw;
int iI,iJ;
if(iCw+aiW[iK]<=iMax)
{
sSol[iK]=1;
if(iK fnBranch_bound(iK+1,iCp+aiPr[iK],iCw+aiW[iK],iMax,aiW,aiPr,iItem);
if(iK==iItem && (iCp+aiPr[iK])>sFp)
{
printf("\nSolution vectors are : ");
for(iI=0;iI printf(" %d ",sSol[iI]);
sFp=iCp+aiPr[iK];
sFw=iCw+aiW[iK-1];
printf("\nProfit is : %d",sFp);
printf("\nWeight is : %d",sFw);
}
}
![Page 28: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/28.jpg)
if (fnBound(iCp,iCw,iK,iMax,aiPr,iItem,aiW)>sFp)
{
sSol[iK]=0;
if(iK fnBranch_bound(iK+1,iCp,iCw,iMax,aiW,aiPr,iItem);
if(iK==iItem && iCp>sFp)
{
sFp=iCp;
sFw=iCw;
for(iJ=0;iJ printf(" %d ",sSol[iJ]);
printf("\nProfit is : %d",sFp);
printf("\nWeight is : %d",iCw);
}
}
}
void main()
{
int iItem,aiWeight[9],aiProfit[9],iI,iJ,iMax;
void fnBranch_bound(int iK,int iCp,int iCw,int iMax,int aiW[9],int aiPr[9],int iItem);
clrscr();
printf("Knap Sack Problem Solving using branch and bound method.\n");
printf("How many item do you want : ");
scanf("%d",&iItem);
![Page 29: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/29.jpg)
printf("Enter the weight and profit for each item \n");
printf("Weight Profit\n");
for(iJ=0;iJ scanf("%d%d",&aiWeight[iJ],&aiProfit[iJ]);
printf("Enter the maximum capacity of knapsack : ");
scanf("%d",&iMax);
fnBranch_bound(0,0,0,iMax,aiWeight,aiProfit,iItem);
getch();
}
EX:NO:12 HAMILTONIAN
#include<stdio.h>
#define V 5
void printSolution(int path[]);
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
if (graph [ path[pos-1] ][ v ] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
![Page 30: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/30.jpg)
return true;
}
bool hamCycleUtil(bool graph[V][V], int path[], int pos)
{
if (pos == V)
{
if ( graph[ path[pos-1] ][ path[0] ] == 1 )
return true;
else
return false;
}
for (int v = 1; v < V; v++)
{
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
![Page 31: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/31.jpg)
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
bool hamCycle(bool graph[V][V])
{
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if ( hamCycleUtil(graph, path, 1) == false )
{
printf("\nSolution does not exist");
return false;
![Page 32: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/32.jpg)
}
printSolution(path);
return true;
}
void printSolution(int path[])
{
printf ("Solution Exists:"
" Following is one Hamiltonian Cycle \n");
for (int i = 0; i < V; i++)
printf(" %d ", path[i]);
printf(" %d ", path[0]);
printf("\n");
}
int main()
{
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
![Page 33: DAA LAB](https://reader036.vdocument.in/reader036/viewer/2022070400/55cf9c87550346d033aa22a4/html5/thumbnails/33.jpg)
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
hamCycle(graph1);
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}