Professional Documents
Culture Documents
Ada Lab Prgms (All)
Ada Lab Prgms (All)
Laboratory Work
1. Design, develop, and run a program in any language to implement the Bellman-Ford
algorithm and determine its performance.
2. Design, develop, and run a program in any language to implement Johnson’s algorithm
and determine its performance.
3. Design, develop, and run a program in any language to implement a Monte Carlo
algorithm to test the primality of a given integer and determine its performance.
4. Design, develop, and run a program in any language to solve the string matching
problem using naïve approach and the KMP algorithm and compare their performances.
5. Design, develop, and run a program in any language to solve modular linear equations.
6. Design, develop, and run a program in any language to implement the FFT algorithm
efficiently.
/*Program to Implement the Bellman-Ford algorithm */
#include <stdio.h>
#include <conio.h>
typedef struct {
int u, v, w ;
} Edge;
Edge edges[SIZE];
int e,n,d[SIZE],pre[SIZE],temp[SIZE];
void initialise(int s)
{ int i;
d[s] = 0;
void relax()
{ int i,j ;
for (i = 1; i <= n-1 ; ++i)
{ for (j = 0; j < e; ++j)
{ if (d[edges[j].u] + edges[j].w < d[edges[j].v])
{ d[edges[j].v] = d[edges[j].u] + edges[j].w;
pre[edges[j].v]= edges[j].u;
}
}
int check()
{ int i;
for(i=0;i<e;++i)
{ if (d[edges[i].u] + edges[i].w < d[edges[i].v])
return FALSE;
}
return TRUE;
void printDist() {
int i,j;
temp[0]=n-1;
j=n-1;
for(i=1; i<n-1; ++i)
{
temp[i]=pre[j];
j=temp[i];
}
printf("%d\n",temp[j]+1);
// for(i=0;i<e;i++)
// printf("%d",pre[i]);
}
void bellman_ford(int s)
{ int temp;
initialise(s);
relax();
temp=check();
if(temp==TRUE)
printDist();
else
printf("The Graph contains negative cycle");
}
int main( )
{
int i, j, s;
int w,matrix[10][10];
clrscr();
do
{
printf("Enter the no. of VERTICES:\n");
scanf("%d",&n);
if(n<1)
printf("A graph should have atleast one VERTEX");
}while(n<1);
e = 0;
edges[e].u=i;
edges[e].v = j;
edges[e].w = matrix[i][j];
++e;
}
}
}
bellman_ford(s-1);
getch();
return 0;
}
/* OutPut
Enter the no. of VERTICES:
5
#include <stdio.h>
#include <conio.h>
typedef struct {
int inode, enode;
float weight,nweight ;
} Edge;
Edge edges[SIZE];
int e,eno,pre[SIZE],temp[SIZE],h[SIZE];
int numOfVertices,adjMatrix[10][10],source;
int predecessor[15],distance[15],dist[20][20];
int mark[20];
void johnson();
void initialise(int s)
{ int i;
distance[s] = 0;
void relax()
{ int i,j ;
for (i = 1; i <= numOfVertices-1 ; ++i)
{ for (j = 0; j < e; ++j)
{ if (distance[edges[j].inode] + edges[j].weight <
distance[edges[j].enode])
{ distance[edges[j].enode] = distance[edges[j].inode] +
edges[j].weight;
pre[edges[j].enode]= edges[j].inode;
}
}
}
}
initialise(source);
relax();
for(i=0;i<e;++i)
void d_initialize(int s)
{ int i;
for( i=0;i<numOfVertices-1;i++)
{
mark[i] =FALSE;
predecessor[i] = -1;
distance[i] = INFINITY;
}
distance [s] = 0;
}
int getClosestUnmarkedNode()
{
int minDistance = INFINITY;
int closestUnmarkedNode,i;
for( i=0;i<numOfVertices;i++)
{
if((mark[i]==FALSE) && ( minDistance > distance[i]))
{
minDistance = distance[i];
closestUnmarkedNode = i;
}
}
return closestUnmarkedNode;
}
void dijkstra(int s)
{
for( i=0;i<numOfVertices;i++)
{ if((mark[i]==FALSE) && (adjMatrix[closestUnmarkedNode][i]>=0 &&
adjMatrix[closestUnmarkedNode][i]!=INFINITY ) )
{
if(distance[i] > distance[closestUnmarkedNode]
+adjMatrix[closestUnmarkedNode][i])
{ distance[i] = distance[closestUnmarkedNode]
+adjMatrix[closestUnmarkedNode][i];
predecessor[i] = closestUnmarkedNode;
}
}
}
count++;
}
for(i=0;i<numOfVertices;i++)
printf("n%d ",distance[i]);
}
void output()
{int i;
for (i=0;i<numOfVertices-1;i++)
printf("d=%d",distance[i]);
}
int main( )
{
int i, j, s;
int w,matrix[10][10];
clrscr();
do
{
printf("Enter the no. of VERTICES:\n");
scanf("%d",&numOfVertices);
if(numOfVertices<1)
printf("A graph should have atleast one VERTEX");
}while(numOfVertices<1);
e = 0;
edges[e].inode=i;
edges[e].enode = j;
edges[e].weight = matrix[i][j];
++e;
}
}
}
eno=e;
johnson();
getch();
return 0;
}
void johnson()
{ int i,j,k,s,temp;
s=numOfVertices;
for(i=0;i<numOfVertices;i++)
{ edges[e].inode=s;
edges[e].enode=i;
edges[e].weight=0;
++e;
}
temp=bellman_ford(s);
if(temp==FALSE)
printf("The Graph contains negative cycle");
else
{
for(i=0;i<=numOfVertices;i++)
h[i]=distance[i];
for(i=0;i<numOfVertices;i++)
printf("\n%d",distance[i]);
for(i=0;i<e;i++)
edges[i].nweight=edges[i].weight + h[edges[i].inode] - h[edges[i].enode];
for(i=0;i<numOfVertices;i++)
{ for(j=0;j < numOfVertices;j++)
{ if(i!=j)
adjMatrix[i][j]=INFINITY;
else
adjMatrix[i][j]=0;
}
for(i=0;i<eno;i++)
{ adjMatrix[edges[i].inode][edges[i].enode]=edges[i].nweight;
}
/*for(i=0;i<numOfVertices;i++)
{ for(j=0;j<numOfVertices;j++)
printf("%5d",adjMatrix[i][j]);
printf("%n");
} */
for(i=0;i<numOfVertices;i++)
{
dijkstra(i);
for(j=0;j<numOfVertices ;j++)
{// printf("+%3d",distance[j]);
dist[i][j]= distance[j]+h[j]-h[i];
}
}
for(i=0;i<numOfVertices;i++)
{ for(j=0;j<numOfVertices;j++)
printf("\n%5d",dist[i][j]);
printf("\n");
}
}
}
/*OutPut
Enter the no. of VERTICES:
5
Enter the ADJACENCY MATRIX of the GRAPH:
0 3 8 0 -4
00017
04000
2 0 -5 0 0
00060
0
-1
-5
0
-4
0 -3 -7 -2 -8
3 0 -4 1 -7
7 4 0 5 -3
2 -1 -5 0 -8
9 6 2 7 0 */
/*Program to implement a Monte Carlo algorithm to test the
primality of a given integer and determine its performance.*/
/*
Program to test primality of an integer by implementing Monte-Carlo algorithm
This program uses Miller-Rabil test for primality
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
/*
witness(a,n): This function returns false if 'a' is a factor of 'n', else it returns
true.
By invoking this function k-times, we can probably guess whether the number
is prime or not
*/
t=0,u=n-1;
while(u%2!=1)
t++,u/=2;
printf("t=%ld,u=%ld\n",t,u);
x0=modExp(a,u,n);
printf("x0=%ld\n",x0);
for(i=1;i<=t;i++)
{
x1=(x0*x0)%n;
if(x1==1 && x0!=1 && x0!=n-1)
return 1;
x0=x1;
}
if(x1!=1)
return 1;
return 0;
}
srand(time(NULL));
for(j=1;j<=s;j++)
{
a=0;
while(a==0)
a=rand()%n;
printf("a=%ld,",a);
if(witness(a,n))
return 0; //Definitely not prime
}
return 1; // probably prime
}
void main()
{
long n =0; // holds the number whose primality has to be tested
long a;// holds random number generated in each iteration
int i;
while(n<=2)
{
printf("\nEnter an odd number >2 to check for primality:");
scanf("%ld",&n);
}
if((n%2)==0)
printf("%ld is not prime.\n",n);
else if(miller(n,log(n)))
printf("\n%ld may be prime.",n);
else
printf("\n%ld is not prime.",n);
getch();
}
Output
n=strlen(text),m=strlen(pat);
if(n<m)
{
printf("\nPattern is longer than text.");
exit(0);
}
for(j=0;j<=n-m;j++)
{
for(i=0;i<m;i++)
{
if(pat[i]!=text[i+j])
break;
}
if(i==m)
return j+1;
}
return 0;
}
void main()
{
char text[50],pat[50];
int pos=0;
puts("\nEnter the text:");
gets(text);
puts("\nEnter the pattern:");
gets(pat);
if(pos=naive(text,pat))
printf("\nPattern found at position %d",pos);
else
printf("\nPattern not found.");
getch();
}
Output
Enter the text:
Hello world
Enter the pattern:
Hello
Pattern found at position 1.
#include<stdio.h>
#include<string.h>
m=strlen(pat);
i=0;
j=prefix[0]=-1;
while (i<m)
{
while (j>-1 && pat[i]!=pat[j])
j=prefix[j];
i++,j++;
if (pat[i] == pat[j])
prefix[i] = prefix[j];
else
prefix[i] = j;
}
}
n=strlen(text);
m=strlen(pat);
/* Preprocessing */
compuPrefix(pat,prefix);
/* Searching */
i=j=0;
while(j<n)
{
while(i>-1 && pat[i]!= text[j])
i=prefix[i];
i++,j++;
if (i>= m)
return(j-i+1);
}
return 0;
}
void main()
{
char text[50],pat[50];
int pos=0;
puts("\nEnter the text:");
gets(text);
puts("\nEnter the pattern:");
gets(pat);
if(pos=KMP(text,pat))
printf("\nPattern found at position %d\n",pos);
else
printf("\nPattern not found.\n");
}
Output
Comparison
Let m be the length of the pattern and n be the length of the text.
Algorithm Preprocessing time Matching time
Naïve string matching 0 (No preprocessing) (m(n-m+1))
KMP string matching (m) (n)
/* Program to Implement to solve Modular Linear Equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef struct
{
int d,x,y;
}Ex;
Ex extended_Euclid(int a, int b)
{
Ex res,resp;
if(b==0)
{
res.d=a;
res.x=1;
res.y=0;
return res;
}
else
{ resp=extended_Euclid(b,a%b);
res.d=resp.d;
res.x=resp.y;
res.y=resp.x-(a/b)*resp.y;
return res;
}
}
}
else printf("NO SOLUTIONS");
}
int main()
{
int a,b,x,y,d,n;
clrscr();
printf("Enter the values of \"a\": " );
scanf("%d",&a);
printf("\nEnter the values of \"b\": ");
scanf("%d",&b);
printf("\nEnter the values of \"n\": ");
scanf("%d",&n);
printf("\nThe given MODULAR EQUATION is %dx=%d mod %d\n",a,b,n);
modularlinear(a,b,n);
// printf("X = %d, Y = %d, D = %d\n",res.x,res.y,res.d);
getch();
return 0;
}
/*OutPut
D = 1, X = -1, Y = 2
Solution1 = 2 */
/* Program to Implement the EFT Algorithm*/
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <complex.h>
for(k=0;k<n;k++)
{ m=reverse(k);
B[m]=a[k];
}
}
num1=num;
num1 >>= 1;
while(num1)
{
reverse_num <<= 1;
reverse_num |= num1 & 1;
num1 >>= 1;
count++;
else
reverse_num <<= i;
return reverse_num;
}
for (s=1;s<=res;s++)
m=pow(2,s);
ang=(2*M_PI/m);
wm=complex(cos (ang), sin (ang));
complex t,u;
for(k=0;k<n-1;k+=m){
{ w = complex(1,0);
ang=(2*M_PI/m);
w=w*complex(cos (ang), sin (ang)); }
}
cout<<"\n\nTHE DFT OF THE GIVEN POLYNOMIAL IS:\n\n";
for(i=0;i<n;i++)
{ printf("A%d=",i);
cout<<B[i]<<endl; }
}
void main()
{ int i,n;
complex a[10];
clrscr();
cout<<"ITERATIVE-FFT IMPLEMENTATION:\n\n";
cout<<"\nEnter d coefficients:\n";
for(i=0;i<n;i++)
cin>>a[i];
iterative_FFT(a,n);
getch();
}
/*OutPut
ITERATIVE-FFT IMPLEMENTATION:
Enter d coefficients:
3 -8 7
A0=(3, 1.33848e-126)
A1=(3, -1.33848e-126)
A2=(7, 0) */