You are on page 1of 46

EN.

NO:080410107037

SORTING ALGORITHM AIM: Implementation Of Bucket Sort. Program:


#include<stdio.h> #include<time.h> #include<unistd.h> #define MILLION 1000000 typedef struct node { float value; struct node *link; } node; main() { node counter[10], *n2,*n1; float fa[10],temp; int i,j,k=0; float n,a; clrscr(); for(i=0;i<10;i++) { counter[i].value = 0; counter[i].link = 0; } for(i=0;i<10;i++) { n = ar[i]; j = n * 100; j = j/10; if(counter[j] . value ==0 && counter[j] . link == 0) counter[j] . value = ar[i]; else { if(counter[j].link==0 && counter[j] .value != 0) { counter[j].link=(node *) malloc(sizeof(node)); n2 = counter[j].link; n2 -> link = 0;

EN. NO:080410107037 n2 -> value =ar[i]; continue; } while(n2 -> link !=0 ) { n2 = n2 -> link; } n2 -> link =(node *) malloc(sizeof(node)); n2 = n2 -> link; n2 -> link=0; n2 -> value = ar[i]; } } printf("The sorted values after merging all buckets in order are: "); for(i=0;i<10;i++) { if(counter[i] . link ==0 && counter[i] . value == 0) continue; else { n1 = &counter[i]; n2 = &counter[i] ; if(n2 -> link != 0) { while(n1!=0) { while(n2!= 0) { if(n1 -> value > n2 -> value) { temp =n1 -> value; n1 -> value =n2 -> value; n2 -> value =temp; } n2 = n2 -> link; } n2 = n1 -> link; n1 = n1 -> link; } n1 = &counter[i]; for(; n1!=0; k++) { fa[k] = n1 -> value;

EN. NO:080410107037 n1 = n1 -> link; } } else { fa[k] = counter[i].value; k=k+1; } } } for(i=0;i<10;i++) printf("%f",fa[i]); }

EN. NO:080410107037

OUTPUT:
[08CE28@LINTEL 08CE28]$ gcc bin.c [08CE28@LINTEL 08CE28]$ ./a.out BEST CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 10 enter the array elements : 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 Sorted array is 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 It took 18 microseconds WORST CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 10 enter the array elements : 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0

EN. NO:080410107037 Sorted array is 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 It took 20 microseconds AVERAGE CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 10 enter the array elements : 10.0 9.0 1.0 4.0 7.0 2.0 3.0 5.0 8.0 6.0 Sorted array is 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 It took 18 microseconds [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

AIM: Implementation of Insertion Sort Program:


#include<stdio.h> int main() { int i,j,a[5],k; for(i=0;i<5;i++) { printf("\n enter no>> "); scanf("%d",&a[i]); } for(j=1;j<5;j++) { k=a[j]; i=j-1; while(i>=0 & a[i]>k) { a[i+1]=a[i]; i--; } a[i+1]=k; } for(i=0;i<5;i++) { printf("\n %d ",a[i]); } return 0; }

EN. NO:080410107037

Output:
[08CE28@LINTEL 08CE28]$ time gcc inssort.c real 0m0.039s user 0m0.030s sys 0m0.010s [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 1 enter no>> 2 enter no>> 7 enter no>> 8 enter no>> 9 1 2 7 8 9 [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 1 enter no>> 7 enter no>> 4 enter no>> 2 enter no>> 5 1 2 4 5 7 [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 5 enter no>> 4

EN. NO:080410107037 enter no>> 3 enter no>> 2 enter no>> 1 1 2 3 4 5

EN. NO:080410107037

AIM: Implementation of Selection Sort Program:


#include<stdio.h> int main() { int a[5],i,j,min,temp; for(i=0;i<5;i++) { printf("\n ENTER NO>: "); scanf("%d",&a[i]); } for(i=0;i<5;i++) { min=i; for(j=i+1;j<5;j++) { if(a[min]>a[j]) { min=j; } } temp=a[i]; a[i]=a[min]; a[min]=temp; } for(i=0;i<5;i++) { printf("\n%d",a[i]); } return 0; }

EN. NO:080410107037

Output :
[08CE28@LINTEL 08CE28]$ time gcc selsort.c real 0m0.039s user 0m0.040s sys 0m0.000s [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 1 enter no>> 2 enter no>> 7 enter no>> 8 enter no>> 9 1 2 7 8 9 [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 1 enter no>> 7 enter no>> 4 enter no>> 2 enter no>> 5 1 2 4 5 7 [08CE28@LINTEL 08CE28]$ ./a.out enter no>> 5 enter no>> 4

EN. NO:080410107037 enter no>> 3 enter no>> 2 enter no>> 1 1 2 3 4 5 [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

DIVIDE AND CONQUER AIM: Implementation of Merge Sort


Program :
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<unistd.h> #define MILLION 1000000 int n; int main() { int i,high,low; int A[10]; void merge_sort(int A[10],int low,int high); void display (int A [10]); printf("\n \t \t MERGE SORT \n"); printf("\n ENTER THE LENGTH OF LIST "); scanf("%d", &n); printf("\n ENTER THE LIST ELEMENT"); for(i=0;i<n;i++) scanf("%d",&A[i]); low=0; high=n-1; merge_sort(A,low,high); display(A); return 0; } void merge_sort(int A[10],int low,int high) { int mid; void combine(int A[10],int low, int mid,int high); if(low<high) { mid=(low+high)/2; merge_sort(A,low,mid); merge_sort(A,mid+1,high); combine(A,low,mid,high); } }

EN. NO:080410107037 void combine(int A[10],int low,int mid,int high) { int i,j,k; int temp[10]; k=low; i=low; j=mid+1; while(i<=mid && j<=high) { if(A[i]<=A[j]) { temp[k]=A[i]; i++; k++; } else { temp[k]=A[j]; k++; j++; } } while(i<=mid) { temp[k]=A[i]; i++; k++; } while(j<=high) { temp[k]=A[j]; j++; k++; } for(k=low;k<=high;k++) A[k]=temp[k]; } void display(int A[10]) { int i; printf("\n \n THE SORTED ARRAY IS:\n"); for(i=0;i<n;i++)

EN. NO:080410107037 printf("%d\t",A[i]); }

EN. NO:080410107037 Output : [08CE28@LINTEL 08CE28]$ time gcc mergesort.c real 0m0.054s user 0m0.050s sys 0m0.010s [08CE28@LINTEL 08CE28]$ ./a.out MERGE SORT ENTER THE LENGTH OF LIST 5 ENTER THE LIST ELEMENT1 7 5 8 7 THE SORTED ARRAY IS: 1 5 7 7 8 [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

AIM: Implementation of QuickSort Program:


#include<stdio.h> #include<stdlib.h> #include <sys/time.h> #include <time.h> #include <unistd.h> #define MILLION 1000000 void quicksort(int *a,int p,int r) { int q=0; if(p<r) { q=partition(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,r); } } int partition(int *a,int p,int r) { int x,i,j,t; x=a[r]; i=p-1; for(j=p;j<=r-1;j++) { if(a[j]<=x) { i=i+1; t=a[i]; a[i]=a[j]; a[j]=t; } } t=a[i+1]; a[i+1]=a[r]; a[r]=t; return(i+1); }

EN. NO:080410107037 int main() { long timedif; struct timeval tpstart; struct timeval tpend,now; int p=0,r=4,d; int a[5]; //clrscr(); gettimeofday(&tpstart,NULL); for(d=0;d<5;d++) { a[d]=rand()%10; printf("\n%d",a[d]); } quicksort(a,p,r); gettimeofday(&tpend, NULL); for(d=0;d<5;d++) { printf(" \n %d",a[d]); } timedif = MILLION* (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec; printf("\n"); fprintf(stderr, "It took %ld microseconds", timedif); printf("\n"); return 0; }

EN. NO:080410107037

Output:
[08CE28@LINTEL 08CE28]$ time gcc quick.c real 0m0.553s user 0m0.060s sys 0m0.010s [08CE28@LINTEL 08CE28]$ ./a.out 3 6 7 5 3 3 3 5 6 7 It took 52 microseconds [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

AIM: Implementation of Matrix Multiplication Program :


#include<stdio.h> int main() { int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2; printf("Enter number of rows and columns of first matrix MAX 10\n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of second matrix MAX 10\n"); scanf("%d%d",&r2,&c2); if(r2==c1) { printf("Enter rows and columns of First matrix \n"); printf("Row wise\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); } printf("You have entered the first matrix as follows:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",m1[i][j]); printf("\n"); } printf("Enter rows and columns of Second matrix \n"); printf("Again row wise\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); } printf("You have entered the second matrix as follows:\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",m2[i][j]); printf("\n"); }

EN. NO:080410107037 if(r1==r2&&c1==c2) { printf("Now we add both the above matrix \n"); printf("The result of the addition is as follows;\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { add[i][j]=m1[i][j]+m2[i][j]; printf("%d\t",add[i][j]); } printf("\n"); } } else { printf("Addition cannot be done as rows or columns are not equal\n"); } printf("Now we multiply both the above matrix \n"); printf("The result of the multiplication is as follows:\n"); /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/ for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { mult[i][j]=0; for(k=0;k<r1;k++) { mult[i][j]+=m1[i][k]*m2[k][j]; /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/ } printf("%d\t",mult[i][j]); } printf("\n"); } return 0; } else { printf("Matrix multiplication cannot be done"); } }

EN. NO:080410107037 OUTPUT: [08CE28@LINTEL 08CE28]$ time gcc matrixmul.c real 0m0.047s user 0m0.050s sys 0m0.000s [08CE28@LINTEL 08CE28]$ ./a.out Enter number of rows and columns of first matrix MAX 10 3 3 Enter number of rows and columns of second matrix MAX 10 3 3 Enter rows and columns of First matrix Row wise 1 1 1 1 1 1 2 2 2 You have entered the first matrix as follows: 1 1 1 1 1 1 2 2 2 Enter rows and columns of Second matrix Again row wise 1 2 1 2 1 2 1 2 2 You have entered the second matrix as follows: 1 2 1 2 1 2 1 2 2 Now we add both the above matrix

EN. NO:080410107037 The result of the addition is as follows; 2 3 2 3 2 3 3 4 4 Now we multiply both the above matrix The result of the multiplication is as follows: 4 5 5 4 5 5 8 10 10 [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

GREEDY ALGORITHM AIM: Implementation of Prims Algorithm Program :


#include<stdio.h> int m=0,n=0,last_node,tot_weight=0,globmin; void prim() { int i,j,k,v1,v2,wght; printf("Enter the no of nodes:"); scanf("%d",&no_nodes); printf("Enter the no of edges:"); scanf("%d",&no_edges); for (i=0;i<no_nodes;i++) { for (j=0;j<no_nodes;j++) { graph[i][j]=100; node_cove[i]=101; } } for (i=0;i<no_edges;i++) { printf("Enter the v1 and v2 nodes:"); scanf("%d%d",&v1,&v2); printf("Enter the weight of this edge(Max wieght is 100)"); scanf("%d",&wght); graph[v1][v2]=wght; graph[v2][v1]=wght; } } void start() { int start,temp,i,j; printf("Enter the nodes from wich u want to start") ;

EN. NO:080410107037 scanf("%d",&start); node_cove[0]=start; nodecover=1; for (i=0;i<no_nodes;i++) { for (j=0;j<no_nodes;j++) { sol_set[i][j]=100; } int solution() { int i,j,h,l,flag=0,flag1; for (i=0;i<25;i++) a[i]=0; if(nodecover!=no_nodes) { globmin=min(); for (i=0;i<nodecover;i++) { if(flag==0) { h=node_cove[i]; for (j=0;j<no_nodes;j++) { if (globmin==graph[h][j]) {n=h; m=j; flag1=search2(j) ; if (flag1==1) { last_node=j; node_cove[nodecover] =last_node; nodecover++; sol_set[n][m]=1; flag=1; break; } } } } else break; } solution();

EN. NO:080410107037 } else { return(0); } } int min() { int k=0,min,j,i; int x,temp; for (i=0;i<nodecover;i++) { x=node_cove[i]; for (j=0;j<no_nodes;j++) { if(temp=search1(j)) { a[k]=graph[x][j] ; k++; } } } min=100 ; for (i=0;i<k;i++) { if(a[i]<min) { min=a[i] ; j=i; } } return(min) ; } int search1(int x) { int i,j,c,d,flag1=0; c=x; for (i=0;i<nodecover;i++) { if (c==node_cove[i]) { flag1=1; }

EN. NO:080410107037 } if (flag1==1) return(0); else return(1); } int search2(int y) { int d,c,i,j,flag1=1; d=y; for (i=0;i<nodecover;i++) { if (d==node_cove[i]) { flag1=0; } } if (flag1==1) return(1);

EN. NO:080410107037

Output :
[08CE28@LINTEL 08CE28]$ time gcc prim1.c real 0m0.051s user 0m0.040s sys 0m0.020s [08CE28@LINTEL 08CE28]$ ./a.out Enter the no of nodes:4 Enter the no of edges:5 Enter the v1 and v2 nodes:1 2 Enter the weight of this edge(Max weight is 100)10 Enter the v1 and v2 nodes:2 3 Enter the weight of this edge(Max weight is 100)7 Enter the v1 and v2 nodes:3 4 Enter the weight of this edge(Max weight is 100)15 Enter the v1 and v2 nodes:4 1 Enter the weight of this edge(Max weight is 100)18 Enter the v1 and v2 nodes:4 2 Enter the weight of this edge(Max weight is 100)16 Enter the nodes from which u want to start1 path from 1 to 0 path from 1 to 2 path from 2 to 3 117 [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

AIM: Implementation of kruskal's algorithm Program:


#include<stdio.h> #define INFINITY 999 typedef struct Graph { int v1,v2; int cost; } GR; GR G[20]; int tot_edges,tot_nodes; void create(); void spanning_tree(); int Minimum(int); int main() { printf("\n \t GRAPH CREATION BY ADJACENCY MATRIX "); create(); spanning_tree(); return 0; } void create() { int k; printf("\n ENTER TOTAL NUMBER OF NODES "); scanf("%d",&tot_nodes); printf("\n ENTER TOTAL NUMBER OF EDGES "); scanf("%d",&tot_edges); for(k=0;k<tot_edges;k++) { printf("\n ENTER EDGE IN (V1 V2) FORM "); scanf("%d%d",&G[k].v1,&G[k].v2); printf("\n ENTER CORRESPONDING COST "); scanf("%d",&G[k].cost); } } void spanning_tree() {

EN. NO:080410107037 int count,k,v1,v2,i,j,tree[10][10],pos,parent[10]; int sum; int find(int v2,int parent[]); void Union(int i,int j,int parent[]); count=0; k=0; sum=0; for(i=0;i<tot_nodes;i++) parent[i]=i; while(count!=tot_nodes-1) { pos=Minimum(tot_edges); if(pos==-1) break; v1=G[pos].v1; v2=G[pos].v2; i=find(v1,parent); j=find(v2,parent); if(i!=j) { tree[k][0]=v1; tree[k][1]=v2; k++; count++; sum+=G[pos].cost; Union(i,j,parent); } G[pos].cost=INFINITY; } { { if(count==tot_nodes-1) printf("Spanning tree is..."); printf("\n.................\n"); for(i=0;i<tot_nodes-1;i++) printf("%d",tree[i][0]); printf("--"); printf("%d",tree[i][1]); printf("]"); printf("\n..................\n"); printf("\ncost of spanning tree is = %d",sum); else { printf("There is no spanning tree");

} }

EN. NO:080410107037 } } int Minimum(int n) { int i,small,pos; small=INFINITY; pos=-1; for(i=0;i<n;i++) { { } } } { while(parent[v2]!=v2) { v2=parent[v2]; } return v2; } void Union(int i,int j,int parent[]) { if(i<j) parent[j]=i; else parent[i]=j; } if(G[i].cost<small) small=G[i].cost; pos=i; return pos; int find(int v2,int parent[])

EN. NO:080410107037

Output :
[08CE28@LINTEL 08CE28]$ vi krus.c [08CE28@LINTEL 08CE28]$ gcc krus.c [08CE28@LINTEL 08CE28]$ ./a.out

GRAPH CREATION BY ADJACENCY MATRIX ENTER TOTAL NUMBER OF NODES 4

ENTER TOTAL NUMBER OF EDGES 5

ENTER EDGE IN (V1 V2) FORM 1 2

ENTER CORRESPONDING COST 10

ENTER EDGE IN (V1 V2) FORM 2 4

ENTER CORRESPONDING COST 14

ENTER EDGE IN (V1 V2) FORM 2 3

ENTER CORRESPONDING COST 16

ENTER EDGE IN (V1 V2) FORM 3 4

EN. NO:080410107037

ENTER CORRESPONDING COST 9

ENTER EDGE IN (V1 V2) FORM 4 1

ENTER CORRESPONDING COST 2

cost of spanning tree is 35

DYNAMIC PROGRAMING AIM:Implementation Of Shortest Path


Program:
#include<stdio.h> #include<time.h> #include<unistd.h> #define MILLION 1000000 #define INFINITY 999 int path[10]; int main() { int tot_nodes,i,j,cost[10][10],dist[10],s[10]; void create(int tot_nodes,int cost[][10]);

EN. NO:080410107037 void dij(int tot_nodes,int cost[][10],int i,int dist[10]); void display(int 1,int j,int dist[10]); printf("\n \t CREATION OF GRAPH ::"); printf("\n ENTER THE TOTAL NUMBERS OF NODES ::"); scanf("%d",&tot_nodes); create(tot_nodes,cost); for(i=0;i<tot_nodes;i++) { printf("\n \t when source =%d \n",i); for(j=0;j<tot_nodes;j++) { dij(tot_nodes,cost,i,dist); if(dist[j]==infinity) printf("\n THERE IS NO PATH TO %d\n",j); else { display(i,j,dist); } } return 0; } void create(int tot_nodes,int cost[][10]) { int i,j,val,tot_edges,count=0; for(i=0;i<tot_nodes;i++) { for(j=0;j<tot_nodes;j++) { if(i==j) cost[i][j]=0; else cost[i][j]=infinity; } } printf("\n TOTAL NUMBER OF EDGES"); scanf("%d",tot_edges); while(count<tot_edges) { printf("\n ENTER Vi and Vj"); scanf("%d%d",&i,&j); printf("\n ENTER THE COST ALONG THIS EDGE"); scabf("%d",&val);

EN. NO:080410107037 cost[j][i]=val; cost[i][j]=val; count++; } } void dij(inttot_nodes,int cost[10[10],int source,int dist[]) { int i,j,v1,v2,min_dist; ints[10]; for(i=0;i<tot_nodes;i++) { dist[i]=cost[source][i]; s[i]=0; path[i]=source; } s[source]=1; for(i=0;i<tot_nodes;i++) { min_dist=infinity; v1=-1; for(j=0;j<tot_nodes;j++) { if(s[j]==0) { if(dist[j]<min_dist) { min_dist=dist[j]; v1=j; } } } s[v1]=1; for(v2=0;v2<tot_nodes;v2++) { if(s[v2]==0) { if(dist[v1]+cost[v1][v2]<dist[v2]) { dist[v2]=dist[v1]+cost[v1][v2]; } }

EN. NO:080410107037 } } } void display(int source,int destination,int dist[]) { int i; printf("\n STEP BY STEP SHORTEST PATH IS:: \n "); for(i=destination;i!=source;i=path[i]) { printf("%d<-",i); } printf("%d",i); printf("THE LENGTH= %d",dist[destination]); }

Output:
[08CE28@LINTEL 08CE28]$ gcc bin.c [08CE28@LINTEL 08CE28]$ ./a.out BEST CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 8 enter the array elements : 1 2 3

EN. NO:080410107037 4 5 6 7 8 sorted array is 12345678 It took 20 microseconds WORST CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 8 enter the array elements : 8 7 6 5 4 3 2 1 Sorted array is 12345678 It took 21 microseconds AVERAGE CASE: [08CE28@LINTEL 08CE28]$ ./a.out enter the no of elements of array n= : 8 enter the array elements : 1 2 4 6 5 3 8 7 Sorted array is 12345678 It took 21 microseconds [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

AIM: Implementation Of Knapsack Problem Program :


#include <stdio.h> #define MAXWEIGHT 100 int n = 3; /* The number of objects */ int c[10] = {8, 6, 4}; /* c[i] is the *COST* of the ith object; i.e. what YOU PAY to take the object */ int v[10] = {16, 10, 7}; /* v[i] is the *VALUE* of the ith object; i.e. what YOU GET for taking the object */ int W = 10; /* The maximum weight you can take */ void fill_sack() { int a[MAXWEIGHT]; /* a[i] holds the maximum value that can be obtained using at most i weight */ int last_added[MAXWEIGHT]; /* I use this to calculate which object were

EN. NO:080410107037 added */ int i, j; int aux; for (i = 0; i <= W; ++i) { a[i] = 0; last_added[i] = -1; } a[0] = 0; for (i = 1; i <= W; ++i) for (j = 0; j < n; ++j) if ((c[j] <= i) && (a[i] < a[i - c[j]] + v[j])) { a[i] = a[i - c[j]] + v[j]; last_added[i] = j; } for (i = 0; i <= W; ++i) if (last_added[i] != -1) printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$ %dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i - c[last_added[i]]); else printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i); printf("---\n"); aux = W; while ((aux > 0) && (last_added[aux] != -1)) { printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1, v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]); aux -= c[last_added[aux]]; } printf("Total value added: %d$\n", a[W]); } int main(int argc, char *argv[]) { fill_sack(); return 0; } The Fractional Knapsack Problem in C

EN. NO:080410107037 #include <stdio.h> int n = 5; /* The number of objects */ int c[10] = {12, 1, 2, 1, 4}; /* c[i] is the *COST* of the ith object; i.e. what YOU PAY to take the object */ int v[10] = {4, 2, 2, 1, 10}; /* v[i] is the *VALUE* of the ith object; i.e. what YOU GET for taking the object */ int W = 15; /* The maximum weight you can take */ void simple_fill() { int cur_w; float tot_v; int i, maxi; int used[10]; for (i = 0; i < n; ++i) used[i] = 0; /* I have not used the ith object yet */ cur_w = W; while (cur_w > 0) { /* while there's still room*/ /* Find the best object */ maxi = -1; for (i = 0; i < n; ++i) if ((used[i] == 0) && ((maxi == -1) || ((float)v[i]/c[i] > (float)v[maxi]/c[maxi]))) maxi = i; used[maxi] = 1; /* mark the maxi-th object as used */ cur_w -= c[maxi]; /* with the object in the bag, I can carry less */ tot_v += v[maxi]; if (cur_w >= 0) printf("Added object %d (%d$, %dKg) completly in the bag. Space left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w); else { printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1); tot_v -= v[maxi]; tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi]; } } printf("Filled the bag with objects worth %.2f$.\n", tot_v); }

EN. NO:080410107037 int main(int argc, char *argv[]) { simple_fill(); return 0; }

Output : [08CE28@LINTEL 08CE28]$ time gcc knapsack.c real 0m0.042s user 0m0.040s sys 0m0.000s [08CE28@LINTEL 08CE28]$ ./a.out Weight 0; Benefit: 0; Can't reach this exact weight. Weight 1; Benefit: 0; Can't reach this exact weight. Weight 2; Benefit: 0; Can't reach this exact weight. Weight 3; Benefit: 0; Can't reach this exact weight. Weight 4; Benefit: 7; To reach this weight I added object 3 (7$ 4Kg) to weight 0 . Weight 5; Benefit: 7; To reach this weight I added object 3 (7$ 4Kg) to weight 1 .

EN. NO:080410107037 Weight 6; Benefit: 10; To reach this weight I added object 2 (10$ 6Kg) to weight 0. Weight 7; Benefit: 10; To reach this weight I added object 2 (10$ 6Kg) to weight 1. Weight 8; Benefit: 16; To reach this weight I added object 1 (16$ 8Kg) to weight 0. Weight 9; Benefit: 16; To reach this weight I added object 1 (16$ 8Kg) to weight 1. Weight 10; Benefit: 17; To reach this weight I added object 2 (10$ 6Kg) to weigh t 4. --Added object 2 (10$ 6Kg). Space left: 4 Added object 3 (7$ 4Kg). Space left: 0 Total value added: 17$ [08CE28@LINTEL 08CE28]$

Aim: Implementation Of Longest Common Sub-sequence Program:


#include<stdio.h> #include<string.h> void print_lcs(char b[][20],char x[],int i,int j) { if(i==0 || j==0) return; if(b[i][j]=='c')

EN. NO:080410107037 { print_lcs(b,x,i-1,j-1); printf(" %c",x[i-1]); } else if(b[i][j]=='u') print_lcs(b,x,i-1,j); else print_lcs(b,x,i,j-1); } void lcs_length(char x[],char y[]) { int m,n,i,j,c[20][20]; char b[20][20]; m=strlen(x); n=strlen(y); for(i=0;i<=m;i++) c[i][0]=0; for(i=0;i<=n;i++) c[0][i]=0; for(i=1;i<=m;i++) for(j=1;j<=n;j++) {

EN. NO:080410107037 if(x[i-1]==y[j-1]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='c'; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='u'; } else { c[i][j]=c[i][j-1]; b[i][j]='l'; } } print_lcs(b,x,m,n); } void lcs() { int i,j; char x[20],y[20]; //l stands for left //u stands for upright or above //c stands for left upright cross

EN. NO:080410107037 printf("1st sequence:"); gets(x); printf("2nd sequence:"); gets(y); printf("\nlcs are:"); lcs_length(x,y); printf("\n"); lcs_length(y,x); } main() { char ch; do { lcs(); printf("\nContinue(y/n):"); } while(ch=='y'||ch=='Y'); }

EN. NO:080410107037

Output:
[08CE28@LINTEL 08CE28]$ gcc longest.c [08CE28@LINTEL 08CE28]$ ./a.out 1st sequence:1 2 4 5 8 2nd sequence:4 8

lcs are: 4 4 8 Continue(y/n):n [08CE28@LINTEL 08CE28]$

EN. NO:080410107037

You might also like