Académique Documents
Professionnel Documents
Culture Documents
#include<iostream.h> #include<conio.h> void main() { clrscr(); int k,a[5],i,j,temp; for(i=0;i<5;i++) { cout<<"\nEnter value of array a["<<i<<"] : "; cin>>a[i]; } cout<<"\nArray after sorting is: "; for(k=1;k<5;k++) { temp=a[k]; j=k-1; while((temp<a[j])&&(j>=0)) { a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } for(i=0;i<5;i++) cout<<"\n value of array a["<<i<<"] : "<<a[i]; getch(); }
OUTPUT:
Enter Enter Enter Enter Enter Array value value value value value value of array a[0] value of array a[1] value of array a[2] value of array a[3] value of array a[4] after sorting is: of array a[0] : 0 of array a[1] : 2 of array a[2] : 4 of array a[3] : 6 of array a[4] : 8 : : : : : 2 8 4 6 0
mergesort(f,m); mergesort(m+1,l); merge(f,m,m+1,l); } } void main() { clrscr(); int n,i,j=0; cout<<"\n Enter number of terms: "; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter value for a["<<i<<"] : "; cin>>arr[i]; } mergesort(j,n-1); cout<<"\n\nSorted array is: "; for(i=0;i<n;i++) cout<<"\nValue for a["<<i<<"] : "<<arr[i]; getch(); }
OUTPUT:
Enter number of terms: Enter value for a[0] : Enter value for a[1] : Enter value for a[2] : Enter value for a[3] : Enter value for a[4] : Sorted array is: Value for a[0] : 1 Value for a[1] : 2 Value for a[2] : 5 Value for a[3] : 6 Value for a[4] : 9 5 2 6 1 9 5
OUTPUT:
int partition(int A[n], int p, int r) { int x,i; x=A[r]; i=p-1; int temp; for(int j=p;j<=r-1;j++) { if(A[j]<=x) { i++; temp=A[i]; A[i]=A[j]; A[j]=temp; } } temp=A[i+1]; A[i+1]=A[r]; A[r]=temp; return i+1; }
OUTPUT:
the unsorted sequence: 45 92 37 33 95 78 26 48 82 63 The sorted array is 26 33 37 45 48 63 78 82 92 95
cout<<"\n The resultant matrix is : "; mat_display(m3); } int main( ) { clrscr( ); int i,j; cout<<"\n Enter the values in the first 2x2 matrix : "; mat_accept(m1); cout<<"\n Enter the values in the second 2x2 matrix :"; mat_accept(m2); mat_multiply(m1,m2); getch( ); return 0; }
OUTPUT:
Enter the values in the first 2*2 matrix: 2 1 2 1 Enter the values in the second 2*2 matrix: 2 1 1 1 The resultant matrix is: 5 3 5 3
} } m[i][j]=min; m[j][i]=flag; } } mat_disp(0,n-1); } int main() { int n; int p[20]; cout<<"\n Enter the number of matrices to be multplied : "; cin>>n; cout<<"\n Enter the order sequence of matrices : "; for(int i=0;i<=n;i++) cin>>p[i]; cout<<"\n Optimal Pranthesization is:"; mat_chain(n,p); return 0; }
OUTPUT:
Enter the number of matrices to be multplied : 6 Enter the order sequence of matrices : 30 35 15 5 10 20 25 Optimal Paranthesization is: (A1((A2A3)((A4A5)A6)))
PROGRAM 7 // WAP to implement largest common subsequence from two given sequences
#include<iostream.h> #include<string.h> int print_lcs(char b[20][20],char x[10],int i,int j) { if(i==-1||j==-1) { return 1; } if(b[i][j]=='a') { print_lcs(b,x,i-1,j-1); cout<<x[i]; } else if(b[i][j]=='b') { print_lcs(b,x,i-1,j); } else print_lcs(b,x,i,j-1); return 0; } int main() { char x[20],y[20],c[20][20]; int m,n,i,j; char b[20][20]; cout<<"\nEnter the size of X and Y"; cin>>m>>n; cout<<"\nEnter the elements of X..."; for(i=0;i<m;i++) { cin>>x[i]; }
cout<<"\nEnter the elements of Y..."; for(i=0;i<n;i++) { cin>>y[i]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][-1]=0; } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[-1][j]=0; } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(x[i]==y[j]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='a'; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='b'; } else { c[i][j]=c[i][j-1]; b[i][j]='c'; } } }
OUTPUT:
Enter the size of X and Y: 6 7 Enter the elements of X: b d c a b a Enter the elements of Y: a b c b d a b LCS is: b c b a
struct Edge { int terminal; struct Edge *next; }; struct Vertex { int visit; int vertex_no; char info; int path_length; struct Edge *Edge_Ptr; }; void Table(int , int matrix [size][size], struct Vertex vert[size]); struct Edge *Insert_Vertex (int , struct Edge *); void DFS ( int , int *dist, struct Vertex vert [size]); void Input(int, int a [size][size]); void Output(int, int a [size][size]); struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) { struct Edge *new1, *current; new1 = (struct Edge *) malloc(sizeof(struct Edge)); new1->terminal = vertex_no; new1->next = NULL; if (!first) return (new1); for (current = first; current->next; current = current->next); current->next = new1; return (first); }
/* Initializing entries */ void Table(int vertex_num, int matrix [size][size], struct Vertex vert[size]) { int i, j; for (i = 0; i < vertex_num; i++) { vert [i].visit = F; vert [i].vertex_no = i+1; vert [i].info = 'A'+ i; vert [i].path_length = 0; vert [i].Edge_Ptr = NULL; } for (i =0; i < vertex_num ; i++) for (j =0; j < vertex_num ; j++) if (matrix [i][j] > 0) vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr); } /* Computing path length */ void DFS ( int index, int *dist, struct Vertex vert [size]) { struct Edge *Link; vert [index].visit = T; vert [index].path_length = *dist; *dist += 1; for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next) if (vert [Link->terminal].visit == F) DFS (Link->terminal, dist, vert); } /* Input function to read adjacency matrix */ void Input(int number, int a [size][size]) { int i, j; printf("\n Input the adjacency matrix \n"); for (i =0; i < number; i++) { for (j=0; j < number; j ++) {
scanf("%d", &a [i][j]); } printf("\n"); } } /* Output function */ void Output(int number, int a [size][size]) { int i, j; printf("\n Adjacency matrix \n"); for (i = 0; i < number; i++) { for (j = 0; j < number; j ++) { printf(" %d", a [i][j]); } printf("\n"); } } /* Function main */ void main() { int i; int number, index, dist; int a [size][size]; struct Vertex vert [size]; struct Edge *List; printf("\n Input the number of vertices in the graph: "); scanf("%d", &number); Input(number, a); Output(number, a); Table(number, a, vert); printf("\n Input the starting vertex 0- %d:", number-1); scanf("%d", &index); dist = 0; DFS (index, &dist, vert); printf("\n Path length of the vertex from %c", vert[index].info); printf("\n Vertex Length Vertex Connectivity \n "); for (i = 0; i < number; i++) {
printf("\n %c %d ", vert[i].info, vert[i].path_length); for (List= vert[i].Edge_Ptr; List; List = List>next) { printf(" "); putchar(List->terminal+'A'); } } }
OUTPUT:
Input the number of vertices in the graph: 3 Input the adjacency matrix 1 2 1 3 2 3 4 5 4 Adjacency 1 2 3 2 4 5 matrix 1 3 4
Input the starting vertex 0- 2:1 Path length of the vertex from B Vertex Length Vertex Connectivity A B C 1 0 2 A B C A B C A B C
struct Edge { int terminal; struct Edge *next; }; struct Vertex { int visit; int vertex_no; char info; int path_length; struct Edge *Edge_Ptr; }; struct Q { int info; struct Q *next; }; void Table(int , int matrix [size][size], struct Vertex vert[size]); struct Edge *Insert_Vertex (int , struct Edge *); void BFS ( int , struct Vertex vert [size]); void Input(int, int mat [size][size]); void Output(int number, int mat [size][size]); struct Q *Insert_Queue(int vertex_no, struct Q *first); struct Q *Delete_Queue(int *vertex_no, struct Q *first); /* Insert vertex into connectivity list */ struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) { struct Edge *new1, *current; new1 = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no; new1->next = NULL; if (!first) return (new1); for (current = first; current->next; current = current->next); current->next = new1; return (first); } /* Insert vertices into queue */ struct Q * Insert_Queue(int vertex_no, struct Q *first) { struct Q *new1, *current; new1 =(struct Q *) malloc(sizeof(struct Q)); new1->info = vertex_no; new1->next = NULL; if (!first) return (new1); for (current = first; current->next; current = current->next); current->next = new1; return (first); } struct { Q * Delete_Queue(int *vertex_no, struct Q *first) struct Q *previous; if (!first) return (NULL); *vertex_no = first->info; previous = first; first = first->next; free(previous); return (first); } /* Initializing entries */ void Table(int vertex_num, int matrix [size][size], struct Vertex vert[size]) { int i, j; for (i = 0; i < vertex_num; i++) { vert [i].visit = F;
for (i =0; i < vertex_num ; i++) for (j =0; j < vertex_num ; j++) if (matrix [i][j] > 0 ) vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr); } /* Computing path length */ void BFS ( int index, struct Vertex vert [size]) { struct Q *queue = NULL; struct Edge *Link; vert [index].visit = T; queue = Insert_Queue(index, queue); while(queue) { queue = Delete_Queue(&index, queue); for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next) { if (vert [Link->terminal].visit == F) { vert[Link->terminal].visit = T; vert[Link>terminal].path_length=vert[index].path_length+1; queue = Insert_Queue(Link->terminal, queue); } } } } /* Input function to read adjacency matrix */ void Input(int number, int mat [size][size]) { int i, j; printf("\n Input the adjacency matrix \n"); for (i =0; i < number; i++) {
for (j=0; j < number; j ++) { scanf("%d", &mat [i][j]); } printf("\n"); } } /* Output function to display adjacency matrix */ void Output(int number, int mat [size][size]) { int i, j; printf("\n Adjacency matrix \n"); for (i =0; i < number; i++) { for (j=0; j < number; j ++) { printf(" %d", mat [i][j]); } printf("\n"); } } /* Function main */ void main() { int i, number, index; int mat [size][size]; struct Vertex vert [size]; struct Edge *List; printf("\n Input the number of vertices in the graph: "); scanf("%d", &number); Input(number, mat); Output(number, mat); Table(number, mat, vert); printf("\n Input the starting vertex 0%d :",number-1); scanf("%d", &index); BFS (index, vert); printf("\n Path length of the vertex from %c", vert[index].info) ; printf("\n Vertex Length Vertex Connectivity \n "); for (i = 0; i < number; i++)
{ printf("\n %c %d ",vert[i].info, vert[i].path_length); for (List= vert[i].Edge_Ptr; List; List = List>next) { printf(" "); putchar(List->terminal+'A'); } } }
OUTPUT:
Input the number of vertices in the graph: 3 Input the adjacency matrix 2 3 2 1 2 1 4 5 4 Adjacency 2 3 1 2 4 5 matrix 2 1 4
Input the starting vertex 0- 2 :2 Path length of the vertex from C Vertex Length Vertex Connectivity A B C 1 1 0 A B C A B C A B C
PROGRAM 10 // WAP to implement Bellman Ford algorithm to find the Shortest Path
#include<stdio.h> #include<curses.h> void relax(int d[10],int w[25][25],int p[10],int t); int main() { clrscr(); int n,i,j,wt[25][25],d[10],pi[10],x; printf(\n Enter the no. of vertices (0<n<6): ); scanf(%d,&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i!=j) { printf(\nweight[%d][%d]: ,i,j); scanf(%d,&wt[i][j]); } } } printf(\n Assume source node is 0th vertex ); for(x=1;x<n;x++) { d[x]=100; pi[x]=NULL; } d[0]=0; pi[0]=NULL; relax(d,wt,pi,n); printf(\n This is the path followed ); int getch(); } void relax(int d[10],int w[25][25],int p[10],int t) { int m,n,q; for(q=0;q<2;q++) {
for(n=0;n<t;n++) { for(m=0;m<t;m++) { if(n!=m && w[n][m]!=0) { if(d[m]>d[n]+w[n][m]) { d[m]=d[n]+w[n][m]; p[m]=n; printf(\np[%d]: %d,m,p[m]); } } } } } printf(\n%d,t-1); }
OUTPUT:
Enter the no. of vertices (0<n<6): 3 weight[0][1]: 5 weight[0][2]: 7 weight[1][0]: 0 weight[1][2]: -2 weight[2][0]: 0 weight[2][1]: 0 Assume source node is 0th vertex p[1]:0 p[2]:0 p[2]:1 p[2]:2 This is the path followed
struct Label { int predecessor; int length; int flag; }; int k, min, count; struct Label state[size]; int visit_path[size]; int Short_Path(int a[size][size], int , int , int, int path [size], int *); void Input(int , int a[size][size]); void Output(int , int a[size][size]); /* Shortest path computing function */ int Short_Path(int a[size][size], int n, int s, int t, int path[size], int *dist) { int i; int t1, t2; *dist = 0; for(i = 1; i <= n; i ++) { state[i].predecessor = 0; state[i].length = infinity ; state[i].flag = T; } /* Make source vertex permanent */ state[s].predecessor = 0; state[s].length = 0;
state[s].flag = P; /* Start from source vertex */ k = s; do { /* Check all the paths from kth vertex and find their distance from k vertex */ for ( i = 1; i <= n; i++) { if ((a[k][i] > 0) && (state[i].flag == T)) { if ((state[k].length + a[k][i]) < state[i].length) { state[i].predecessor = k; state[i].length = state[k].length + a[k][i]; } } } min = infinity; k = 0; for ( i =1; i <= n; i++) { if ((state[i].flag == T) && (state[i].length < min)) { min = state[i].length; k = i; } } if ( k==0) return (0); state[k].flag = P; } while(k != t); /* Store optimal path */ k = t; count = 1; do { visit_path[count] = k; count ++; k = state[k].predecessor;
} while(k!= 0); /* Reverse the vertices since algorithm stores path in reverse direction */ count --; for ( i = 1; i <= count ; i++) path[i] = visit_path[count-i+1]; for ( i = 1; i < count ; i ++) { t1 = path[i]; t2 = path [i+1]; *dist += a[t1][t2]; } return (count); } /* Input function */ void Input(int n, int a[size][size]) { int i, j; printf("\n Input adjacency matrix \n"); for(i =0; i < n; i++) { for(j =0; j < n; j++) { scanf("%d", &a[i][j]); } printf("\n"); } } /* Output function */ void Output(int n, int a[size][size]) { int i, j; printf("\n Adjacency matrix \n"); for(i =0; i < n; i++) { for(j =0; j < n; j++) { printf(" %d", a[i][j]); } printf("\n"); } }
/* Function main */ void main() { int a[size][size]; int path [size]; int org, dest, dist; int count, i, n; printf("\n Input the number of vertices in the graph: "); scanf("%d", &n); Input(n,a); Output(n,a); printf("\n Input strating vertex: "); scanf("%d", &org); printf("\n Input destination: "); scanf("%d", &dest); count = Short_Path(a, n, org, dest, path, &dist); if (dist) { printf("\n Shortest path: %d", path[1]); for (i = 2; i<=count; i++); printf(" => %d", path[i]); printf("\n Minimum distance = %i", dist); } else printf("\n Path does not exist \n"); }
OUTPUT:
Input the number of vertices in the graph: 3 Input adjacency matrix 1 2 1 3 2 3 4 5 4 Adjacency matrix 1 2 1 3 2 3 4 5 4 Input strating vertex: 2 Input destination: 3 Path Does not exist
} } printf(\n\nThe resultant shortest weight matrix is: \n\n); for(i=1;i<=m;i++) { printf(\n); for(j=1;j<=n;j++) printf(%d,a[i][j]; } printf(\n\nThe resultant intermediate vertex matrix is: \n\n); for(i=1;i<=m;i++) { printf(\n); for(j=1;j<=n;j++) printf(%d,pi[i][j]; } int getch(); } int min(int e,int f) { return((e<f) ? e : f); }
OUTPUT:
Enter the no. of rows and columns: 5 5 0 3 8 101 -4 101 0 101 1 7 101 4 0 101 101 2 101 -5 0 101 101 101 101 6 0 The resultant shortest weight matrix is: 0 1 -3 2 -4 3 0 -4 1 -1 7 4 0 5 3 2 -1 -5 0 -2 8 5 1 6 0 The resultant intermediate vertex matrix is: 0 4 4 4 4 3 0 3 3 3 4 4 0 4 4 5 2 2 0 5 1 1 1 1 0
OUTPUT:
Enter Main String : abaabba Enter Pattern String : aba Pattern Exists With Shift :0
for(i=dm;i>=0;i--) { d=m % 10; marray[i]=d; m=m / 10; } for(i=dp;i>=0;i--) { d=n % 10; parray[i]=d; n=n / 10; } for(s=1;s<=(dm-dp+1);s++) { i=dp+s-1; j=0; n=0; while(i>=s) { n=n+(pow(10,j)*marray[i]); i--; j++; } q=int(n) % 13; if(q==p) //checking the case of spurious hit { int flag=0; int k=1; while(!flag && (k<=dp)) { if(parray[k]!=marray[k+s-1]) flag=1; else k++; } if(!flag) cout<<"\nPattern Exists With Shift :"<<s-1<<"\n"; } } getch(); }
OUTPUT:
Enter Main Text : abaaba Enter Pattern Text : aba Pattern Exists With Shift :0
PROGRAM 15
// Write a program to implement Activity Selection problem #include<stdio.h> #include<curses.h> int main() { clrscr(); int n,s[15],f[15],m,i; char A[15]; printf(\n Enter the no. of activities: ); scanf(%d,&n); printf(\n Enter the start values of activities: ); for(i=1;i<=n;i++) scanf(%d,&s[i]); printf(\n Enter the finishing values of activities: ); for(i=1;i<=n;i++) scanf(%d,&f[i]); for(i=1;i<=n;i++) { A[i]=96+i; printf(\n %s=(%d,%d),A[i],i,f[i]); } i=1; printf( \n ); printf(A[1]); for(m=2;m<=n;m++) { if(s[m]>=f[i]) { i=m; printf(A[m]); } } int getch(); }
OUTPUT
Enter the no, of activities: 5 Enter the start value of activities: 1 3 0 5 3 Enter the finishing value of the activities: 4 5 6 7 8 a b c d e = = = = = (1,4) (3,5) (0,6) (5,7) (3,8)
PROGRAM 16
// To determine solution to Knapsack problem #include<stdio.h> #include<curses.h> #include<process.h> void sort(float t[10],int n,float p[10],float w[10]); int main() { clrscr(); int i,n,m,s,t; float x[10],w[10],u,p[10],sum=0.0,tr[10]; printf(\n Enter the size of knapsack bag: ); scanf(%d,&m); printf(\n Enter the no. of items: ); scanf(%d,&n); printf(\n Enter the weight of each items: ); for(i=0;i<n;i++) { printf(\nw[%d]: ,i); scanf(%d,&w[i]); } printf(\n Enter the profit of each items: ); for(i=0;i<n;i++) { printf(\np[%d]: ,i); scanf(%d,&p[i]); } for(i=0;i<n;i++) x[i]=0.0; u=m; for(s=0;s<n;s++) { tr[s]=p[s]/w[s]; } sort(tr,n,p,w); for(i=0;i<n;i++) { if(u>=w[i]) { x[i]=1; u=u-w[i]; } } t=u;
if(u<m) { for(i=0;<n;i++) { if(x[i]==0 && u>0) { x[i]=2; u=u-t; } } } for(i=0;i<n;i++) { if(x[i]==1) { sum=sum+p[i]; } if(x[i]==2) { sum=sum+((t/w[i])*p[i]); } } printf(\n Profit: %d,sum); int getch(); } void sort(float t[10],int n,float p[10],float w[10]) { float i,big,pos,j,temp,m,temp1,x,temp2; for(i=0;i<n-1;i++) { big=t[i]; pos=i; for(j=i+1;j<n;j++) { if(big<t[j]) { big=t[j]; pos=j; } } temp=t[i]; t[i]=t[pos]; t[pos]=temp; temp1=p[i]; p[i]=p[pos]; p[pos]=temp1;
OUTPUT:
Enter the size of knapsack bag: 20 Enter the no. of items: 3 Enter the weight of each item: w[0] = 18 w[1] = 15 w[2] = 10 Enter the profit of each item: p[0] = 25 p[1] = 24 p[2] = 15 Profit: 31.5
} cout << "Enter the array q "; for(i=0;i<=n;i++) { cin >> q[i]; } obst(n,p,q); for(i=1;i<=n+1;i++) { for(j=0;j<=n;j++) { cout << e[i][j]<<" "; } cout<<"\n"; } cout<<"\n"; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout << root[i][j]<<" "; } cout<<"\n"; } getch(); }
} } min_edge(&u, &v); min_cost=nodes[u][v][0]; solution[0][0]=u; solution[0][1]=v; for(i=0;i<num_of_nodes;i++) { if(nodes[i][u][0] < nodes[i][v][0]) near_edges[i] = u; else { if((nodes[i][u][0]==infinity) && (nodes[i][v][0]==infinity)) near_edges[i]=infinity; else near_edges[i]=v; } } near_edges[u] = -1; near_edges[v] = -1; for(i = 1; i < num_of_nodes-1; i++) { j=srch_min_near_edge(); solution[i][0]=j; solution[i][1]=near_edges[j]; min_cost=min_cost+nodes[j][near_edges[j]][0]; near_edges[j]=-1; for(k=0;k<num_of_nodes;k++) { if((near_edges[k]!=infinity)) { if((near_edges[k] != -1) && (nodes[k][near_edges[k]][0]>nodes[k][j][0])) near_edges[k]=j; } else near_edges[k] = j; } } cout<<"\nMinimum cost spanning tree is: \n"; for(i=0;i<num_of_nodes-1;i++) cout<<solution[i][0]+1<<" "<<solution[i][1]+1<<"\n"; cout<<"\nCost of spanning tree: "<<min_cost; getch(); } void min_edge(int *x, int *y) {
int i = 0; int j = 0; int min = 1000; int u = 0; int v = 0; for(i=0;i<num_of_nodes;i++) { for(j=(i+1);j<num_of_nodes;j++) { if((nodes[i][j][1]==0) && (nodes[i][j][0]!=infinity)) { if(min>nodes[i][j][0]) { min=nodes[i][j][0]; u=i; v=j; } } } } nodes[u][v][1]=1; *x=u; *y=v; } int srch_min_near_edge(void) { int edge; int min=1000; int i=0; for(i=0;i<num_of_nodes;i++) { if((near_edges[i] != -1) && (near_edges[i] != infinity)) { if((min>nodes[i][near_edges[i]][0]) && (nodes[i][near_edges[i]][0]!=infinity)) { min=nodes[i][near_edges[i]][0]; edge=i; } } } return edge; }
if(p[c]==-1) {temp1=0; p[c]=0; ans[count++]=i; break; } c--; } if(temp1==1) { ex[x++]=i; penalty=penalty+task[i].cost; } } } for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { if(task[ans[i]].deadl>=task[ans[j]].deadl) {temp1=ans[i]; ans[i]=ans[j]; ans[j]=temp1; } } } cout<<"\n The scheduled tasks are \n "; for(i=0;i<count;i++) cout<<" T"<<ans[i]+1; for(i=0;i<x;i++) cout<<" T"<<ex[i]+1; cout<<"\n\nTotal penalty is"; cout<<penalty; getch(); }