7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 1/61
PROGRAM 1
// WAP to implement insertion sort
#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();}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 2/61
OUTPUT:
Enter value of array a[0] : 2Enter value of array a[1] : 8Enter value of array a[2] : 4Enter value of array a[3] : 6Enter value of array a[4] : 0Array after sorting is:value of array a[0] : 0value of array a[1] : 2
value of array a[2] : 4value of array a[3] : 6value of array a[4] : 8
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 3/61
PROGRAM 2
// WAP to implement Merge Sort
#include<iostream.h>#include<conio.h>#include<stdio.h>int arr[100];void merge(int f1, int l1,int f2,int l2){int i=f1;int j=f2;int temp[100],k=0;while(i<=l1 && j<=l2){if(arr[i]<arr[j]){temp[k]=arr[i];i++,k++;}else{temp[k]=arr[j];j++,k++;}}
while(i<=l1){temp[k]=arr[i];k++,i++;}while(j<=l2){temp[k]=arr[j];k++,j++;}for(i=f1,k=0;i<=l2;i++,k++)
arr[i]=temp[k];}void mergesort(int f,int l){int m;if(l>f){m=(f+l)/2;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 4/61
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();}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 5/61
OUTPUT:
Enter number of terms: 5Enter value for a[0] : 2Enter value for a[1] : 6Enter value for a[2] : 1Enter value for a[3] : 9Enter value for a[4] : 5Sorted array is:Value for a[0] : 1Value for a[1] : 2Value for a[2] : 5
Value for a[3] : 6Value for a[4] : 9
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 6/61
PROGRAM 3
// WAP to implement Quick Sort
#include <iostream>#define n 10
using namespace std;
int main(){
int A[n]={45,92,37,33,95,78,26,48,82,63};void quicksort(int [n], int, int);quicksort(A,0,n-1);cout<<"The sorted array is";for(int i=0;i<n;i++)
cout<<A[i]<<" ";system("PAUSE");return 0;
}
void quicksort(int A[n], int p, int r){
int partition(int [n],int , int );int q;if(p<r)
{q=partition(A,p,r);quicksort(A,p,q-1);quicksort(A,q+1,r);
}}
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];
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 7/61
A[j]=temp;}
}temp=A[i+1];A[i+1]=A[r];
A[r]=temp;return i+1;}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 8/61
OUTPUT:
the unsorted sequence: 459237339578264882
63
The sorted array is 26 33 37 45 48 63 78 82 92 95
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 9/61
PROGRAM 4
// WAP to implement Randomized Quick Sort
#include <iostream> #define n 10
using namespace std;
int main(){
int A[n]={45,92,37,33,95,78,26,48,82,63};void quicksort(int [n], int, int);quicksort(A,0,n-1);cout<<"The sorted array is";
for(int i=0;i<n;i++)cout<<A[i]<<" ";
system("PAUSE");return 0;
}
void quicksort(int A[n], int p, int r){
int rand_partition(int [n],int , int );int q;if(p<r)
{q=rand_partition(A,p,r);quicksort(A,p,q-1);quicksort(A,q+1,r);
}}
int rand_partition(int A[n], int p, int r){
int partition(int *,int , int);int i=rand()%100;
while(!((i<=r)&&(i>=p)))i=rand()%100;int temp;temp=A[r];A[r]=A[i];A[i]=temp;return partition(A,p,r);
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 10/61
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;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 11/61
OUTPUT:
the unsorted sequence: 459237 33957826 488263
The sorted array is 26 33 37 45 48 63 78 82 92 95
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 12/61
PROGRAM 5
// WAP to implement strassen’s algorithm for Matrix multiplication
#include<iostream.h>#include<conio.h>
int m3[2][2],m1[2][2],m2[2][2];int p[6];
void mat_accept(int mat[ ][2]){
int i,j;for(i=0;i<2;i++)
for(j=0;j<2;j++)cin>>mat[i][j];
} void mat_display(int mat[ ][2]){
int i,j;for(i=0;i<2;i++){cout<<"\n\n";
for(j=0;j<2;j++)cout<<'\t'<<mat[i][j];}
}
void mat_multiply(int m1[ ][2],int m2[ ][2]){
p[0]=m1[0][0]*(m2[0][1]-m2[1][1]);p[1]=(m1[0][0]+m1[0][1])*m2[1][1];p[2]=(m1[1][0]+m1[1][1])*m2[0][0];p[3]=m1[1][1]*(m2[1][0]-m2[0][0]);
p[4]=(m1[0][0]+m1[1][1])*(m2[0][0]+m2[1][1]);p[5]=(m1[0][1]-m1[1][1])*(m2[1][0]+m2[1][1]);p[6]=(m1[0][0]-m1[1][0])*(m2[0][0]+m2[0][1]);
m3[0][0]=p[4]+p[3]-p[1]+p[5];m3[0][1]=p[0]+p[1];m3[1][0]=p[2]+p[3];m3[1][1]=p[4]+p[0]-p[2]-p[6];
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 13/61
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;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 14/61
OUTPUT:
Enter the values in the first 2*2 matrix: 2121Enter the values in the second 2*2 matrix: 211
1
The resultant matrix is: 5 3 5 3
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 15/61
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 16/61
}}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;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 17/61
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)))
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 18/61
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);}
elseprint_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];}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 19/61
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';}}}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 20/61
cout<<"\nLCS is...";print_lcs(b,x,i,j);return 0;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 21/61
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
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 22/61
PROGRAM 8
// WAP to implement Depth First Search algorithm
# include<stdio.h># define size 20# define T 1# define F 0
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 Vertexvert[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);
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 23/61
/* 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 ++){
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 24/61
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++){
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 25/61
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');}}
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 26/61
OUTPUT:
Input the number of vertices in the graph: 3
Input the adjacency matrix121
323
454
Adjacency matrix1 2 13 2 34 5 4
Input the starting vertex 0- 2:1
Path length of the vertex from BVertex Length Vertex Connectivity
A 1 A B CB 0 A B CC 2 A B C
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 27/61
PROGRAM 9
// WAP to implement Breadth First Search algorithm
# include<stdio.h># define size 20# define T 1# define F 0
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 Vertexvert[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));
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 28/61
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;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 29/61
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 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++){
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 30/61
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++)
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 31/61
{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');
}}
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 32/61
OUTPUT:
Input the number of vertices in the graph: 3
Input the adjacency matrix232
12
1
454
Adjacency matrix2 3 21 2 14 5 4
Input the starting vertex 0- 2 :2
Path length of the vertex from CVertex Length Vertex Connectivity
A 1 A B CB 1 A B CC 0 A B C
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 33/61
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++){
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 34/61
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);
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 35/61
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 vertexp[1]:0p[2]:0p[2]:1p[2]:2
This is the path followed
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 36/61
PROGRAM 11
// WAP to implement Dijkstra Algorithm
# include<stdio.h>
# define size 20# define P 1# define T 0# define infinity 9999
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;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 37/61
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;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 38/61
} while(k!= 0);
/* Reverse the vertices since algorithm stores path inreverse 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");
}}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 39/61
/* 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");}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 40/61
OUTPUT:
Input the number of vertices in the graph: 3
Input adjacency matrix121
32
3
454
Adjacency matrix1 2 13 2 34 5 4
Input strating vertex: 2
Input destination: 3
Path Does not exist
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 41/61
PROGRAM 12
// WAP to implement Floyd Warshall algorithm
#include<stdio.h>#include<curses.h>#include<stdlib.h>int min(int e,int f);
int main(){
clrscr();int n,m,b,c,d,i,j,a[10][10],pi[10][10];printf(“\n Enter the no. of rows and column: “);scanf(“%d %d”,&m,&n);for(i=1;i<=m;i++){
for(j=1;j<=n;j++)scanf”%d”,&a[i][j]);
}for(i=1;i<=m;i++){
printf(“\n”);
for(j=1;j<=n;j++){
if(i==j |\ a[i][j]>=100)p[i][j]=0;elsepi[i][i]=i;printf(“ %d”,pi[i][j]);
}}for(b=1;b<=m;b++){
for(c=1;c<=m;c++){
for(d=1;d<=m;d++){
if(a[c][d]<=a[c][b]+a[b][d])pi[c][d]=pi[b][d];a[c][d]=min(a[c][d],a[c][b]+a[b][d]);
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 42/61
}}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);}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 43/61
OUTPUT:
Enter the no. of rows and columns: 5 5
0 3 8 101 -4101 0 101 1 7101 4 0 101 1012 101 -5 0 101101 101 101 6 0
The resultant shortest weight matrix is:
0 1 -3 2 -43 0 -4 1 -1
7 4 0 5 32 -1 -5 0 -28 5 1 6 0
The resultant intermediate vertex matrix is:
0 3 4 5 14 0 4 2 14 3 0 2 14 3 4 0 14 3 4 5 0
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 44/61
PROGRAM 13
// WAP To Implement Naive String Matching
#include<iostream.h>#include<conio.h>#include<string.h>void main(){
char mstr[20];char pstr[20];int n,m;int s,i,flag=0,j;cout<<"\n\nEnter Main String : ";cin>>mstr;
cout<<"\n\nEnter Pattern String : ";cin>>pstr;n=strlen(mstr);m=strlen(pstr);for(s=0;s<=(n-m);s++){
flag=0;j=0;while(!flag && (j<m)){
if(pstr[j]!=mstr[j+s])
flag=1;elsej++;
}if(!flag)
cout<<"\nPattern Exists WithShift :"<<s<<"\n";
}
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 45/61
OUTPUT:
Enter Main String : abaabba
Enter Pattern String : aba
Pattern Exists With Shift :0
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 46/61
PROGRAM 14
// WAP To Implement Rabin Karp String Matching
#include<iostream.h>#include<conio.h>#include<string.h>#include<math.h>#include<conio.h>void main(){clrscr();
double mstr;int pstr;
int j;cout<<"\nEnter Main Text : ";cin>>mstr;cout<<"\nEnter Pattern Text : ";cin>>pstr;int p=pstr % 13;int q=0;//To Know The No. Of Digits In Pattern Textint n=pstr;int dp=0;while(n)
{ n=n/10;dp++;
}//This Logic Ends Here//To Know The No. Of Digits In Main Textint m=mstr;int dm=0;while(m){
m=m/10;
dm++;}//This Logic Ends Here
int s=0;double marray[20],parray[20];m=mstr;n=pstr;int d,i;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 47/61
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;
elsek++;
}if(!flag)
cout<<"\nPattern Exists With Shift :"<<s-1<<"\n";
}}
getch();}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 48/61
OUTPUT:
Enter Main Text : abaaba
Enter Pattern Text : aba
Pattern Exists With Shift :0
PROGRAM 15
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 49/61
// 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();
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 50/61
OUTPUT
Enter the no, of activities: 5Enter the start value of activities: 1 3 0 5 3Enter the finishing value of the activities: 4 5 6 7 8
a = (1,4)b = (3,5)c = (0,6)d = (5,7)e = (3,8)
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 51/61
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;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 52/61
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;
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 53/61
temp2=w[i];w[i]=w[pos];w[pos]=temp2;
}}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 54/61
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
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 55/61
PROGRAM 17
//PROGRAM FOR OPTIMAL BINARY SEARCH TREE
#include<iostream.h>
#include<conio.h>float e[50][50], p[50], q[50],w[50][50];
int root[50][50];
void obst(int n,float p[],float q[])
{int i, j,l,r;
float t;
for(i=1;i<=n+1;i++){
e[i][i-1]=q[i-1];
w[i][i-1]=q[i-1];}
for(l=1;l<=n;l++)
{
for(i=1;i<=n-l+1;i++){
j=i+l-1;
e[i][j]=10.0;w[i][j]=w[i][j-1]+p[j]+q[j];
for(r=i;r<=j;r++)
{t=e[i][r-1] + e[r+1][j] + w[i][j];
if(t<e[i][j]){e[i][j]=t;
root[i][j]=r;
}
}}
}
}void main()
{
int n, i, j;clrscr();
cout << "Enter the no of keys : ";
cin >> n;
cout << "Enter the array p ";for(i=1;i<=n;i++)
{
cin >> p[i];
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 56/61
}
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();
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 57/61
PROGRAM 18
//PROGRAM FOR PRIM'S ALGORITHM
#include<iostream.h>#include<conio.h>
#define infinity 1000
int num_of_nodes;
int nodes[10][10][2];int near_edges[10];
int temp[50];
int solution[10][2];void min_edge(int *, int *);
int srch_min_near_edge(void);
void main(){
int i = 0;
int j = 0;int k = 0;
int u = 0;
int v = 0;
int min_cost = 0;clrscr();
cout<<"\nEnter the number of nodes in the graph: ";
cin>>num_of_nodes;cout<<"\n!!!!!!!! Enter 1000 for a not connected link!!!!!!\n";
for(i=0;i<num_of_nodes;i++)
{nodes[i][i][0]=0;
nodes[i][i][1]=1;
for(j=(i+1);j<num_of_nodes;j++)
{cout<<"\nEnter the cost from node "<<i+1<<" to node "<<j+1<<" ";
cin>>nodes[i][j][0];
nodes[j][i][0]=nodes[i][j][0];if(nodes[i][j][0]>=infinity)
{
nodes[i][j][1]=1;nodes[j][i][1]=1;
}
else{
nodes[i][j][1]=0;
nodes[j][i][1]=1;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 58/61
}
}
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)
{
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 59/61
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;
}
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 60/61
PROGRAM 19
//PROGRAM FOR TASK SCHEDULING PROBLEM
#include<iostream.h>#include<conio.h>
struct task
{
int deadl;int cost;
}task[20],temp;
void main(){
int i,j,n,p[20],count=0,c=0,ans[20],ex[10],penalty=0,temp1=1,x=0;
clrscr();cout<<"Enter the number of tasks";
cin>>n;
cout<<"\nEnter dead line and cost of each task\n"" dead line cost\n";
for(i=0;i<n;i++){
cin>>task[i].deadl>>task[i].cost;
p[i]=-1;}
for(i=0;i<n;i++)
{ for(j=i+1;j<n;j++){ if(task[i].cost<task[j].cost)
{ temp=task[i];
task[i]=task[j];
task[j]=temp;}
}
}for(i=0;i<n;i++)
{ if(p[task[i].deadl]==-1)
{ p[task[i].deadl]=0;ans[count++]=i;
}
else{ c=task[i].deadl;
temp1=1;
while(c>0)
{
7/28/2019 ADA Programs
http://slidepdf.com/reader/full/ada-programs 61/61
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();
}