SlideShare a Scribd company logo
1
UNIVERSITY SCHOOL OF INFORMATION COMMUNICATION
AND TECHNOLOGY
LAB FILE-IT354
ALGORITHM DESIGN AND ANALYSIS
SUBMITTED TO SUBMITTED BY
ANURADHA CHUG SURAJ KUMAR
ASSISTANT PROFESSOR 03616403215
B.TECH CSE VI SEM
2
INDEX
PROGRAMS PAGE NO. DATE SIGN
1. PROGRAM FOR SEARCHING. 3 21/1
2. PROGRAM FOR SORTING (ALL) 6 28/1 TO 18/2
3. PROGRAM FOR BREADTH FIRST SEARCH. 14 28/2
4. PROGRAM FOR DEPTH FIRST SEARCH. 15 28/2
5. PROGRAM FOR KRUSKAL ALGORITHM. 16 4/3
6. PROGRAM FOR PRIM’S ALGORITHM. 17 4/3
7. PROGRAM TO IMPLEMENT DIJKSTRA ALGORITHM. 20 11/3
8. PROGRAM TO IMPLEMENT FLOYAD WARSHAL ALGORITHM. 22 11/3
9. PROGRAM TO IMPLEMENT TOPLOGICAL SORT. 23 21/3
10. PROGRAM FOR MEDIAN ORDER STATISTICS. 24 21/3
11. PROGRAM FOR BELLMAN FORD ALGORITHM. 26 21/3
12. PROGRAM FOR MATRIX CHAIN MULTIPLICATION 28 2/4
13. PROGRAM FOR LONGEST COMMOM SUSEQUENCE 30 2/4
14. PROGRAM FOR HUFFMAN CODING. 32 2/4
15. PROGRAM FOR OPTIMAL BST. 35 2/4
16. STRING MATCHING USING NAÏVE ALGO 37 9/4
17. STRING MATCHING USING FINITE AUTOMATA METHOD 39 9/4
18. STRING MATCHING USING RABIN KARP ALGO 42 9/4
19. STRING MATCHING USING KMP ALGO 44 9/4
3
PROGRAM FOR SEARCHING
#include<bits/stdc++.h>
using namespace std;
long int a[1000000];
vector<long int> b[100000];
void linear (long int k,long int n)
{
long int i,j;
for(i=0;i<n;i++)
{
if(a[i]==k)
cout<<"found at "<<i+1<<" position n";
}
}
long int binary(long int k,long int n)
{
long int i,j,hi=n-1,mid,lo=0,found=0;
sort(a,a+n);
mid=(hi+lo)/2;
while(found==0&&lo<=hi)
{
if(a[mid]==k)
{
found=mid+1;
break;
}
else if(k<a[mid])
{
hi=mid-1;
}
else
{
lo=mid+1;
}
mid=(lo+hi)/2;
}
return found;
}
int main()
4
{
long int i,j,k,l,n,x,z=0;
clock_t start, end1;
cout<<"enter the size of arrayn";
cin>>n;
cout<<"enter the arrayn";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"select followingn";
cout<<"select 1 for linear searchn";
cout<<"selcet 2 for binary searchn";
cout<<"selcet 3 for direct searchn";
cin>>l;
if(l==1)
{
cout<<"enter the data to findn";
cin>>k;
start=clock();
linear(k,n);
}
if(l==2)
{
long int m=5;
cout<<"enter the data to findn";
cin>>k;
j=binary(k,n);
if(j==0)
cout<<"not findn";
else
cout<<"found at"<<j<<"n";
}
if(l==3)
{
for(i=0;i<n;i++)
{
x=a[i]%100000;
b[x].push_back(a[i]);
5
}
cout<<"enter the number to findn";
cin>>k;
x=k%100000;
for(j=0;j<b[x].size();j++)
{
if(b[x][j]==k)
{
cout<<"found n";
z=1;
break;
}
}
if(z==0)
cout<<"not foundn";
}
end1=clock();
double time=(end1-start)/CLOCKS_PER_SEC;
cout<<time<<" sec is taken";
}
6
PROGRAM FOR SORTING(ALL)
#include<bits/stdc++.h>
using namespace std;
vector< int> b1[13000];
vector< int> b2[13];
vector< int> b3[13];
int a[100000],b[10004],n=10000;
void select(int);
void bubble(int);
void insertion(int);
void mergesort(int[],int);
void merge1(int[],int[],int[],int,int);
void quick(int,int);
int partion(int,int);
void print(int);
void heapify(int [], int , int );
void heap(int []);
int main()
{
int start=0,last,start1;
char t='y';
int i,j,k,l,x,s=0,end1;
double time;
for (int i=0;i<10000;i++)
{
a[i]=rand()%1000;
}
printf("enter 1 for selection sortn");
printf("enter 2 for bubble sortn");
printf("enter 3 for insertion sortn");
printf("enter 4 for merge sortn");
printf("enter 5 for heap sortn");
printf("enter 6 for quick sortn");
cout<<"enter 7 for bucket sortn";
cout<<"enter 8 for radix sortn";
cout<<"enter 9 for counting sortn";
scanf("%d",&l);
switch(l)
{
case 1:
start1=clock();
select(n);
end1=clock();
7
time=(end1-start)/CLOCKS_PER_SEC;
cout<<time<<" sec is taken";
print(n);
break;
case 2:
bubble(n);
print(n);
break;
case 3:
insertion(n);
print(n);
break;
case 4:
mergesort(a,n);
print(n);
break;
case 5:
heap(a);
print(n);
break;
case 6:
last=n-1;
quick(start,last);
print(n);
break;
}
if(l==7)
{
for(i=0;i<10000;i++)
{
x=a[i]/10;
b1[x].push_back(a[i]);
}
for(i=0;i<1000;i++)
{
sort(b1[i].begin(),b1[i].end());
}
for(i=0;i<1000;i++)
{
for(j=0;j<b1[i].size();j++)
cout<<b1[i][j]<<" ";
}
}
8
if(l==8)
{
int max_digit=3;
for(i=0;i<10000;i++)
{
x=a[i]%10;
b1[x].push_back(a[i]);
}
for(i=0;i<10;i++){
for(j=0;j<b1[i].size();j++){
x=b1[i][j]%100;
x=x/10;
b2[x].push_back(b1[i][j]);
}
}
for(i=0;i<10;i++){
for(j=0;j<b2[i].size();j++){
x=b2[i][j]%1000;
x=x/100;
b3[x].push_back(b2[i][j]);
}
}
for(i=0;i<=9;i++)
{
for(j=0;j<b3[i].size();j++)
cout<<b3[i][j]<<" ",s++;
}
}
if(l==9)
{
k=0;
for(i=0;i<10000;i++)
b[a[i]]++;
for(i=1;i<=1000;i++)
b[i]+=b[i-1];
for(i=0;i<1000;i++)
{
for(j=k+1;j<=b[i];j++)
{
cout<<i<<" ";
}
k=b[i];
}
9
}
}
void select(int n)
{
int i,k,m,l,temp;
for(l=0;l<n-1;l++)
{
k=a[l];
m=l;
for(i=l;i<n;i++)
{
if(k>a[i])
{
m=i;
k=a[i];
}
}
temp=a[l];
a[l]=k;
a[m]=temp;
}
}
void bubble(int n)
{
int i,k,temp;
for(i=0;i<n-1;i++)
{
for(k=0;k<n-i-1;k++)
{
if(a[k]>a[k+1])
{
temp=a[k+1] ;
a[k+1]=a[k];
a[k]=temp;
}
}
}
}
void insertion(int n)
{
int i=0,j,k,temp;
for(i=1;i<n;i++)
{
10
j=i-1;
k=a[i];
while(k<a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=k;
}}
void mergesort(int a[10003],int n)
{
int i,j,k,mid=n/2;
int left[10000],right[10000];
if(n<2)
return;
else
{
for(i=0;i<mid;i++)
{
left[i]=a[i];
}
for(j=mid;j<n;j++)
{
right[j-mid]=a[j];
}
mergesort(left,mid);
mergesort(right,n-mid);
merge1(left,right,a,mid,n-mid);
}
}
void quick(int start,int last)
{
int index;
if(start>=last)
return;
index=partion(start,last);
quick(start,index-1);
quick(index+1,last);
}
void print(int n)
{
int i,s=0;
11
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
s++;
}
}
void merge1(int l[10003],int r[10003],int a[10003],int mid,int t)
{
int nl,nr,i=0,j=0,k=0;
nl=mid;
nr=t;
while(i<nl&&j<nr)
{
if(l[i]<r[j])
{
a[k]=l[i];
k++;
i++;
}
else
{
a[k]=r[j];
k++;
j++;
}
}
while(i<nl)
{
a[k]=l[i];
k++;
i++;
}
while(j<nr)
{
a[k]=r[j];
k++;
j++;
}
}
int partion(int start,int last)
{
int j,k,i,temp;
j=start;
12
k=a[last];
for(i=start;i<=last;i++)
{
if(a[i]<k)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j++;
}
}
temp=a[j];
a[j]=a[last];
a[last]=temp;
return(j);
}
void heap(int a[ ])
{
int heap_size = n;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, heap_size, i);
for(int i = n-1; i >=0 ; i-- )
{
swap(a[ 0 ], a[ i ]);
heap_size = heap_size - 1;
heapify(a,heap_size,0);
}
}
void heapify(int a[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && a[l] > a[largest])
largest = l;
if (r < n && a[r] > a[largest])
largest = r;
if (largest != i)
{
swap(a[i], a[largest]);
heapify(a, n, largest);
}
}
13
14
PROGRAM FOR BREADTH FIRST SEAFRCH.
#include<bits/stdc++.h>
using namespace std;
vector<long int >vect[100000];
long int b[10000];
void dfs(long int k)
{
long int i,l;
stack<long int > s;
s.push(k);
while(!s.empty())
{
l=s.top();
s.pop();
if(b[l]!=1){
for(i=0;i<vect[l].size();i++)
{
s.push(vect[l][i]);
}
b[l]=1;
cout<<l<<" ";}
}
}
int main()
{
long int i,j,k,l,m,n,u,v;
cin>>m>>n;
for(i=0;i<n;i++)
{
cin>>u>>v;
vect[u].push_back(v);
vect[v].push_back(u);
}
for(i=1;i<=n;i++)
{
if(b[i]==0)
dfs(i);
}}
15
PROGRAM FOR DEPTH FIRST SEARCH
#include<bits/stdc++.h>
using namespace std;
vector<long int >vect[100000];
long int b[10000];
void dfs(long int k)
{
long int i,l;
stack<long int > s;
s.push(k);
while(!s.empty())
{
l=s.top();
s.pop();
if(b[l]!=1){
for(i=0;i<vect[l].size();i++)
{
s.push(vect[l][i]);
}
b[l]=1;
cout<<l<<" ";}
}}
int main()
{
long int i,j,k,l,m,n,u,v;
cout<<"enter the number of vertices and edgesn";
cin>>m>>n;
for(i=0;i<n;i++)
{
cin>>u>>v;
vect[u].push_back(v);
vect[v].push_back(u);
}
for(i=1;i<=n;i++)
{
if(b[i]==0)
dfs(i);
}}
16
PROGRAM FOR KRUSKAL ALGORITHM
#include<bits/stdc++.h>
#define MAXN 102
using namespace std;
int P[MAXN], Rank[MAXN];
int Node, edg, Cost;
struct edge { int u, v; int cost; };
edge Edge[MAXN*MAXN]; edge Path[MAXN];
int com(const void *xy, const void *xz) { edge *x = (edge*)xy; edge *y = (edge*)xz; return (x->cost - y-
>cost); }
void In() {
int i; for(i = 1; i<= Node; i++) { P[i] = i; Rank[i] = 1;
} }
int Find(int n) {
if(P[n] != n) P[n] = Find(P[n]); return P[n];
}
void Link(int x, int y)
{
if(Rank[x] > Rank[y])
{
P[y] = x;
}
else
{
P[x] = y; if(Rank[x] == Rank[y]) Rank[y]++;
}
}
void Kruscal()
{
int x, y, total = 0; Cost = 0;
for(int i = 0; i<edg; i++)
{
17
x = Find(Edge[i].u);
y = Find(Edge[i].v);
if(x != y)
{
Path[total++] = Edge[i]; Link(x,y);
Cost += Edge[i].cost; if(total == Node - 1) break;
} } }
void Cal() { qsort(Edge,edg,sizeof(edge),com);
Kruscal();
cout<<"Total Cast :"<<Cost<<endl;
for(int i = 0; i<Node-1; i++)
cout<<Path[i].u<<" "<<Path[i].v<<" "<<Path[i].cost<<endl; }
int main()
{
int i;
cout<<"enter the number of verticesn";
cout<<"enter the number of edgesn";
while(cin>>Node>>edg)
{
In();
for(i = 0; i<edg; i++)
cin>>Edge[i].u>>Edge[i].v>>Edge[i].cost;
Cal();
}
}
18
PROGRAM FOR PRIMS ALGORITHM
#include<bits/stdc++.h>
using namespace std;
long int a[1000][1000],b[1000],near[1000][3];
vector<long int> vect;
#define inf 10000000
int main()
{
long int i,j,k,l,m,n,u,v,c,s=inf,cost=0,co=0,pos;
cin>>n>>m;
for(i=0;i<m;i++)
{
cin>>u>>v>>c;
a[u][v]=c;
a[v][u]=c;
}
for(i=1;i<=n;i++)
{
near[i][2]=inf;
for(j=1;j<=n;j++)
{
if(a[i][j]==0)
a[i][j]=inf;
}
}
pos=1;
for(i=1;i<=n;i++)
{
if(s>a[1][i])
s=a[1][i],pos=i;
}
vect.push_back(1);
vect.push_back(pos);
cost+=s;
co++;
b[1]=-1;
b[pos]=-1;
while(co!=n-1)
{
for(i=1;i<=n;i++)
{
if(b[i]!=-1)
{
19
for(j=1;j<=n;j++)
{
if(b[j]==-1)
{
if(a[i][j]<near[i][2])
near[i][2]=a[i][j],near[i][1]=j;
}
}
}
}
s=inf;
for(i=1;i<=n;i++)
{
if(b[i]!=-1)
{
if(near[i][2]<s)
s=near[i][2],pos=i;
}
}
vect.push_back(near[pos][1]);
vect.push_back(pos);
cost+=s;
b[pos]=-1;
co++;
}
cout<<"cost is "<<cost<<" n";
for(i=0;i<2*co;i+=2)
{
cout<<vect[i]<<" "<<vect[i+1]<<"n";
}
}
20
PROGRAM TO IMPLEMENT DIJKSTRA ALGORITHM
#include<bits/stdc++.h>
using namespace std;
bitset<1000005> b;
#define pi pair<int,int>
#define pb push_back
#define mp make_pair
#define inf 100000000000009
vector<pi> edges[1000002];
long long dis[1000002],node[1000002];
void printpath(long long int n)
{
if(n!=1)
printpath(node[n]);
cout<<n<<" ";
}
void path(long long int k)
{
priority_queue<long long int> q;
q.push(k);
long long int i,l;
while(!q.empty())
{
l=q.top();
q.pop();
for(i=0;i<edges[l].size();i++)
{
if(dis[l]+edges[l][i].second<dis[edges[l][i].first])
{
dis[edges[l][i].first]=dis[l]+edges[l][i].second;
q.push(edges[l][i].first);
node[edges[l][i].first]=l;
}
}
}
}
int main()
{
long long int t,n,i,j,k,l,u,v,w;
// printf("enter the number of vertices");
21
cin>>n;
// printf("enter the number of vertices");
cin>>t;
while(t--)
{
cin>>u>>v>>w;
edges[u].pb(mp(v,w));
edges[v].pb(mp(u,w));
}
for(i=2;i<=n;i++)
dis[i]=inf;
dis[1]=0;
path(1);
if(dis[n]==inf) cout<<"-1"<<endl;
else{
printpath(n);
cout<<endl;
}
}
22
PROGRAM TO IMPLEMET FLOYAD WARSHAL ALGORITHM
#include<bits/stdc++.h>
using namespace std;
#define inf 100000009;
long int a[1000][1000];
int main()
{
long int i,j,k,l,m,n,u,v,w;
cout<<"enter the number of vertex";
cin>>n;
cout<<"enter the number edges";
cin>>m;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
a[i][j]=inf;
}
for(i=1;i<=m;i++)
{
cin>>u>>v>>w;
a[u][v]=w;
a[v][u]=w;
a[i][i]=0;
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
}}}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<"n";}}
23
PROGRAM FOR TOPOLOGICAL SORT
#include<bits/stdc++.h>
using namespace std;
vector<long int >vect[100000];
long int b[10000];
stack<long int> s1;
void dfs(long int k)
{
long int i,l;
b[k]=1;
for(i=0;i<vect[k].size();i++)
{
if(b[vect[k][i]]!=1)
dfs(vect[k][i]);}
s1.push(k);}
int main()
{
long int i,j,k,l,m,n,u,v;
cin>>m>>n;
for(i=0;i<n;i++)
{
cin>>u>>v;
vect[u].push_back(v);
//vect[v].push_back(u); }
for(i=1;i<=n;i++)
{
if(b[i]==0)
dfs(i); }
while(!s1.empty()){
k=s1.top();
s1.pop();
cout<<k<<" ";
}
24
PROGRAM FOR MEDIN ORDER STATISTICS
#include<bits/stdc++.h>
using namespace std;
long int a[10000],l,m,n;
long int partion(long int start,long int last)
{
long int j,k,i,temp;
j=start;
k=a[last];
for(i=start;i<=last;i++)
{
if(a[i]<=k&&i!=last)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j++;
}
}
temp=a[j];
a[j]=a[last];
a[last]=temp;
l=j;
if(l==m-1){
return l; }
else if(m-1>l)
partion(l+1,n-1);
else
partion(0,l-1);
}
int main()
{
long int i,j=0,s,z=5;
cout<<"enter the size of arrayn";
cin>>n;
cout<<"enter the array elementsn";
for(i=0;i<n;i++)
cin>>a[i];
while(z--)
{
cout<<"enter the position to findn";
cin>>m;
25
s=partion(0,n-1);
cout<<"the value is "<<a[s]<<"n";
}
}
26
PROGRAM FOR BELLMAN FORD ALGORITHM
#include<bits/stdc++.h>
using namespace std;
# define inf 100000009
long int a[100000][5],dis[100005],dis1[100005];
int main()
{
long int i,j,k,l,m,n,u,v,w,z=0;
cout<<"enter the number of verticesn";
cin>>n;
cout<<"enter the number of edgesn";
cin>>m;
for(i=0;i<=n;i++)
dis[i]=inf;
dis[1]=0;
for(i=0;i<m;i++)
{
cin>>u>>v>>w;
a[i][0]=u;
a[i][1]=v;
a[i][2]=w;
}
for(i=1;i<=n-1;i++)
{
for(j=0;j<m;j++)
{
u=a[j][0];
v=a[j][1];
w=a[j][2];
if(dis[u]+w<dis[v])
dis[v]=dis[u]+w;
if(dis[v]+w<dis[u])
dis[u]=dis[v]+w;
}
}
for(i=1;i<=n;i++)
dis1[i]=dis[i];
for(j=0;j<m;j++)
{
u=a[j][0];
v=a[j][1];
w=a[j][2];
if(dis[u]+w<dis[v])
dis[v]=dis[u]+w;
27
if(dis[v]+w<dis[u])
dis[u]=dis[v]+w;
}
for(i=1;i<=n;i++)
{
if(dis[i]!=dis1[i])
z=1;
}
if(z==1)
cout<<"negative cycle";
else
{
for(i=1;i<=m;i++)
cout<<dis[i]<<" ";
}
}
28
PROGRAM FOR MATRIX CHAIN MULTIPLICATION
#include<bits/stdc++.h>
using namespace std;
long int a[100000],b[100][100];
int main()
{
long int i,j,k,l,m,n,p,z=9999999;
cout<<"enter the NUMBER of matricen";
cin>>n;
cout<<"enter the matrices ordern";
cin>>a[0]>>a[1];
for(i=2;i<=n;i++)
{
cin>>k>>m;
a[i]=m;
}
//cin>>a[n];
for(l=2;l<=n;l++)
{
for(i=1;i<=n-l+1;i++)
{
j=i+l-1;
b[i][j]=z;
for(k=i;k<=j-1;k++)
{
p=b[i][k]+b[k+1][j]+a[i-1]*a[k]*a[j];
29
if(p<b[i][j])
{
b[i][j]=p;
}}
}}
cout<<b[1][n];
}
30
PROGRAM FOR LONGEST COMMOM SUSEQUENCE
#include<bits/stdc++.h>
using namespace std;
long int i,j,k,l,m,n,l1,c[1000][1000];
int main()
{
char a[1000],b[1000];
cout<<"enter the first stringn";
cin>>a;
cout<<"enter the second stringn";
cin>>b;
l=strlen(a);
l1=strlen(b);
for(i=l-1;i>=0;i--)
a[i+1]=a[i];
for(i=l1-1;i>=0;i--)
b[i+1]=b[i];
for(i=1;i<=l;i++)
{
for(j=1;j<=l1;j++)
{
if(a[i]==b[j])
{
c[i][j]=c[i-1][j-1]+1;
31
}
else
{
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
}
}
cout<<c[l][l1];
}
32
PROGRAM FOR HUFFMAN CODING.
#include <bits/stdc++.h>
using namespace std;
long int i,j,k,n,f[1000];
char d[1000];
struct minheap {
char data;
int freq;
minheap *left, *right;
minheap(char data, int freq)
{
left = right = NULL;
this->data = data;
this->freq = freq;
}
};
struct compare {
bool operator()(minheap* l, minheap* r)
{
return (l->freq > r->freq);
}
};
void print(struct minheap* root, string str)
{
if (!root)
return;
if (root->data != '#')
33
cout << root->data << ": " << str << "n";
print(root->left, str + "0");
print(root->right, str + "1");
}
void Huffman()
{
struct minheap *left, *right, *top;
priority_queue<minheap*, vector<minheap*>, compare> minheaps;
for (int i = 0; i < n; ++i)
minheaps.push(new minheap(d[i], f[i]));
while (minheaps.size() != 1) {
left = minheaps.top();
minheaps.pop();
right = minheaps.top();
minheaps.pop();
top = new minheap('#', left->freq + right->freq);
top->left = left;
top->right = right;
minheaps.push(top);
}
print(minheaps.top(), "");
}
int main()
{
cout<<"enter the number of charractersn";
cin>>n;
cout<<"enter the charraectersn";
34
for(i=0;i<n;i++)
cin>>d[i];
cout<<"enter the frequencyn";
for(i=0;i<n;i++)
cin>>f[i];
Huffman();
return 0;
}
35
PROGRAM FOR OPTIMAL BST.
#include<bits/stdc++.h>
using namespace std;
long int c[1000][1000],f[1000],a[1000],p;
int sum(int i, int j)
{
int s = 0;
for (int k = i; k <=j; k++)
s += f[k];
return s;
}
int main()
{
long int i,j,k,l,m,n,z=999999;
cout<<"enter the size of bstn";
cin>>n;
cout<<"enter the bst elementn";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the frequency of elementsn";
for(i=0;i<n;i++)
cin>>f[i];
for(i=0;i<n;i++)
c[i][i]=f[i];
for(l=2;l<=n;l++)
{
for(i=0;i<n-l+1;i++) {
36
j=i+l-1;
c[i][j]=z;
for(k=i;k<=j;k++)
{
p=0;
if(k>i)
p+= c[i][k-1];
if(k<j)
p+=c[k+1][j];
p+=sum( i, j);
if(p<c[i][j])
{
c[i][j]=p;
}}}}
cout<<c[0][n-1];
}
37
STRING MATCHING USING NAÏVE ALGO
#include<bits/stdc++.h>
using namespace std;
int main()
{
char a[1000],b[1000];
long int i,j,k,l,l1,m,n,s=0;
cout<<"enter the stringn";
cin>>a;
cout<<"enter the string to matchn";
cin>>b;
l=strlen(a);
l1=strlen(b);
for(i=0;i<l;i++)
{
k=i;
s=0;
for(j=0;j<l1;j++)
{
if((a[k]!=b[j])||((k==l-1)&&(j!=l1-1)))
{
s=1;
break;
}
else
{
38
k++;
}
}
if(s==0)
{
break;
}
}
if(s==0)
cout<<"pattern is present in string";
else
cout<<"no match";
}
39
STRING MATCHING USING FINITE AUTOMATA METHOD
#include<bits/stdc++.h>
using namespace std;
long int c[1000][256];
char a[1000],b[1000];
set<char > d;
set<char>::iterator iter;
long int nextstate(long int st,long int m,char x)
{
long int nextst,i,j,l;
if (st < m && x ==b[st])
return st+1;
for (nextst = st; nextst > 0; nextst--)
{
if (b[nextst-1] == x)
{
for (i = 0; i < nextst-1; i++)
if (b[i] != b[st-nextst+1+i])
break;
if (i == nextst-1)
return nextst;
}}}
void form()
{
long int i,j,k,l,m,p,q;
m=strlen(b);
40
for(i=0;i<=m;i++)
{
for(iter=d.begin(); iter!=d.end();++iter)
{
p=(*iter);
l=nextstate(i,m,p);
c[i][p]=l;
}}}
void match()
{
long int i,j,k=0,m,z=0,l;
m=strlen(a);
l=strlen(b);
for(j=0;j<m;j++)
{
k=c[k][a[j]];
if(k==l)
{
z=1;
break;
}}if(z==1)
{
cout<<"found at pos"<<j-l+2;
}
else
41
cout<<"not found";
}
int main()
{
long int i,j,k,l,m,n;
cout<<"enter the stringn";
cin>>a;
cout<<"enter the patternn";
cin>>b;
l=strlen(b);
for(i=0;i<l;i++)
{
d.insert(b[i]);
}
form();
match();
}
42
STRING MATCHING USING RABIN KARP ALGO
#include<bits/stdc++.h>
using namespace std;
int main()
{
long int i,j,k,l,l1,p=199,d=256,m=1,n,x=0,y=0,flag=0;
char a[1000],b[1000];
cout<<"enter the stringn";
cin>>a;
cout<<"enter the patternn";
cin>>b;
l=strlen(a);
l1=strlen(b);
for(i=0;i<l1-1;i++)
m=(m*d)%p;
for(i=0;i<l1;i++)
{
x=(x*d+b[i])%p;
y=(y*d+a[i])%p;
}
for(i=0;i<=l-l1;i++)
{
// cout<<x<<" "<<y<<"n";
if(x==y)
{
for(j=0;j<l1;j++)
43
{
if(a[i+j]!=b[j])
break;
} }
if(j==l1){
cout<<"found at index "<<i+1<<"n";
flag=1;
break;
}
if ( i <l-l1 )
{
y = (d*(y - a[i]*m) + a[i+l1])%p;
if (y < 0)
y = (y + p);
}}
if(flag==0)
cout<<"not found";
}
44
STRING MATCHING USING KMP ALGO
#include<bits/stdc++.h>
using namespace std;
void computeLPSArray(char *pat, int M, int *lps);
void KMPSearch(char *pat, char *txt)
{
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
45
if (j == M)
{
printf("Found pattern at index %d n", i-j);
j = lps[j-1];
}
else if (i < N && pat[j] != txt[i])
{
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
}
void computeLPSArray(char *pat, int M, int *lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M)
46
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len-1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
int main()
{
char txt[1000],pat[1000];
47
cout<<"enter the string ";
cin>>txt;
cout<<"enter the patternn";
cin>>pat;
KMPSearch(pat, txt);
return 0;
}

More Related Content

What's hot

VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
Ananda Kumar HN
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
Syed Mustafa
 
C programs
C programsC programs
Critical buckling load geometric nonlinearity analysis of springs with rigid ...
Critical buckling load geometric nonlinearity analysis of springs with rigid ...Critical buckling load geometric nonlinearity analysis of springs with rigid ...
Critical buckling load geometric nonlinearity analysis of springs with rigid ...
Salar Delavar Qashqai
 
PyParis - weather and climate data
PyParis - weather and climate dataPyParis - weather and climate data
PyParis - weather and climate data
Margriet Groenendijk
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
Abhishek Jena
 
Geometric nonlinearity analysis of springs with rigid element displacement co...
Geometric nonlinearity analysis of springs with rigid element displacement co...Geometric nonlinearity analysis of springs with rigid element displacement co...
Geometric nonlinearity analysis of springs with rigid element displacement co...
Salar Delavar Qashqai
 
Computer Science Investigatory Project class 12th
Computer Science Investigatory Project class 12thComputer Science Investigatory Project class 12th
Computer Science Investigatory Project class 12th
iamtheanupam
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example
Atit Gaonkar
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
Syed Umair
 
Nonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programmingNonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programming
Salar Delavar Qashqai
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
C PROGRAMS
C PROGRAMSC PROGRAMS
Junaid program assignment
Junaid program assignmentJunaid program assignment
Junaid program assignment
Junaid Ahmed
 
Cpds lab
Cpds labCpds lab
Advance java
Advance javaAdvance java
Advance java
Vivek Kumar Sinha
 
Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 

What's hot (20)

VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
C programs
C programsC programs
C programs
 
Critical buckling load geometric nonlinearity analysis of springs with rigid ...
Critical buckling load geometric nonlinearity analysis of springs with rigid ...Critical buckling load geometric nonlinearity analysis of springs with rigid ...
Critical buckling load geometric nonlinearity analysis of springs with rigid ...
 
PyParis - weather and climate data
PyParis - weather and climate dataPyParis - weather and climate data
PyParis - weather and climate data
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
 
Geometric nonlinearity analysis of springs with rigid element displacement co...
Geometric nonlinearity analysis of springs with rigid element displacement co...Geometric nonlinearity analysis of springs with rigid element displacement co...
Geometric nonlinearity analysis of springs with rigid element displacement co...
 
Computer Science Investigatory Project class 12th
Computer Science Investigatory Project class 12thComputer Science Investigatory Project class 12th
Computer Science Investigatory Project class 12th
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
 
Nonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programmingNonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programming
 
C programms
C programmsC programms
C programms
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Junaid program assignment
Junaid program assignmentJunaid program assignment
Junaid program assignment
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Advance java
Advance javaAdvance java
Advance java
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 

Similar to algorithm design file

Project presentation(View calender)
Project presentation(View calender)Project presentation(View calender)
Project presentation(View calender)
Ikhtiar Khan Sohan
 
Hardik Jadam , BCA Third Year
Hardik Jadam , BCA Third YearHardik Jadam , BCA Third Year
Hardik Jadam , BCA Third Year
Dezyneecole
 
Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic Concepts
Hareem Aslam
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
Ghh
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
Ghh
 
Rkf
RkfRkf
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
vickyvikas51556
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
akashpunarvi2005
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
Ghh
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
Gaurav Jatav , BCA Third Year
Gaurav Jatav , BCA Third YearGaurav Jatav , BCA Third Year
Gaurav Jatav , BCA Third Year
dezyneecole
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
ericupnorth
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
Programming Homework Help
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
PVS-Studio LLC
 
C arrays
C arraysC arrays
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
Murali Kummitha
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
Murali Kummitha
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
Murali Kummitha
 

Similar to algorithm design file (20)

Project presentation(View calender)
Project presentation(View calender)Project presentation(View calender)
Project presentation(View calender)
 
Hardik Jadam , BCA Third Year
Hardik Jadam , BCA Third YearHardik Jadam , BCA Third Year
Hardik Jadam , BCA Third Year
 
Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic Concepts
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
 
Rkf
RkfRkf
Rkf
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Gaurav Jatav , BCA Third Year
Gaurav Jatav , BCA Third YearGaurav Jatav , BCA Third Year
Gaurav Jatav , BCA Third Year
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
C arrays
C arraysC arrays
C arrays
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 

Recently uploaded

New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 

Recently uploaded (20)

New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 

algorithm design file

  • 1. 1 UNIVERSITY SCHOOL OF INFORMATION COMMUNICATION AND TECHNOLOGY LAB FILE-IT354 ALGORITHM DESIGN AND ANALYSIS SUBMITTED TO SUBMITTED BY ANURADHA CHUG SURAJ KUMAR ASSISTANT PROFESSOR 03616403215 B.TECH CSE VI SEM
  • 2. 2 INDEX PROGRAMS PAGE NO. DATE SIGN 1. PROGRAM FOR SEARCHING. 3 21/1 2. PROGRAM FOR SORTING (ALL) 6 28/1 TO 18/2 3. PROGRAM FOR BREADTH FIRST SEARCH. 14 28/2 4. PROGRAM FOR DEPTH FIRST SEARCH. 15 28/2 5. PROGRAM FOR KRUSKAL ALGORITHM. 16 4/3 6. PROGRAM FOR PRIM’S ALGORITHM. 17 4/3 7. PROGRAM TO IMPLEMENT DIJKSTRA ALGORITHM. 20 11/3 8. PROGRAM TO IMPLEMENT FLOYAD WARSHAL ALGORITHM. 22 11/3 9. PROGRAM TO IMPLEMENT TOPLOGICAL SORT. 23 21/3 10. PROGRAM FOR MEDIAN ORDER STATISTICS. 24 21/3 11. PROGRAM FOR BELLMAN FORD ALGORITHM. 26 21/3 12. PROGRAM FOR MATRIX CHAIN MULTIPLICATION 28 2/4 13. PROGRAM FOR LONGEST COMMOM SUSEQUENCE 30 2/4 14. PROGRAM FOR HUFFMAN CODING. 32 2/4 15. PROGRAM FOR OPTIMAL BST. 35 2/4 16. STRING MATCHING USING NAÏVE ALGO 37 9/4 17. STRING MATCHING USING FINITE AUTOMATA METHOD 39 9/4 18. STRING MATCHING USING RABIN KARP ALGO 42 9/4 19. STRING MATCHING USING KMP ALGO 44 9/4
  • 3. 3 PROGRAM FOR SEARCHING #include<bits/stdc++.h> using namespace std; long int a[1000000]; vector<long int> b[100000]; void linear (long int k,long int n) { long int i,j; for(i=0;i<n;i++) { if(a[i]==k) cout<<"found at "<<i+1<<" position n"; } } long int binary(long int k,long int n) { long int i,j,hi=n-1,mid,lo=0,found=0; sort(a,a+n); mid=(hi+lo)/2; while(found==0&&lo<=hi) { if(a[mid]==k) { found=mid+1; break; } else if(k<a[mid]) { hi=mid-1; } else { lo=mid+1; } mid=(lo+hi)/2; } return found; } int main()
  • 4. 4 { long int i,j,k,l,n,x,z=0; clock_t start, end1; cout<<"enter the size of arrayn"; cin>>n; cout<<"enter the arrayn"; for(i=0;i<n;i++) cin>>a[i]; cout<<"select followingn"; cout<<"select 1 for linear searchn"; cout<<"selcet 2 for binary searchn"; cout<<"selcet 3 for direct searchn"; cin>>l; if(l==1) { cout<<"enter the data to findn"; cin>>k; start=clock(); linear(k,n); } if(l==2) { long int m=5; cout<<"enter the data to findn"; cin>>k; j=binary(k,n); if(j==0) cout<<"not findn"; else cout<<"found at"<<j<<"n"; } if(l==3) { for(i=0;i<n;i++) { x=a[i]%100000; b[x].push_back(a[i]);
  • 5. 5 } cout<<"enter the number to findn"; cin>>k; x=k%100000; for(j=0;j<b[x].size();j++) { if(b[x][j]==k) { cout<<"found n"; z=1; break; } } if(z==0) cout<<"not foundn"; } end1=clock(); double time=(end1-start)/CLOCKS_PER_SEC; cout<<time<<" sec is taken"; }
  • 6. 6 PROGRAM FOR SORTING(ALL) #include<bits/stdc++.h> using namespace std; vector< int> b1[13000]; vector< int> b2[13]; vector< int> b3[13]; int a[100000],b[10004],n=10000; void select(int); void bubble(int); void insertion(int); void mergesort(int[],int); void merge1(int[],int[],int[],int,int); void quick(int,int); int partion(int,int); void print(int); void heapify(int [], int , int ); void heap(int []); int main() { int start=0,last,start1; char t='y'; int i,j,k,l,x,s=0,end1; double time; for (int i=0;i<10000;i++) { a[i]=rand()%1000; } printf("enter 1 for selection sortn"); printf("enter 2 for bubble sortn"); printf("enter 3 for insertion sortn"); printf("enter 4 for merge sortn"); printf("enter 5 for heap sortn"); printf("enter 6 for quick sortn"); cout<<"enter 7 for bucket sortn"; cout<<"enter 8 for radix sortn"; cout<<"enter 9 for counting sortn"; scanf("%d",&l); switch(l) { case 1: start1=clock(); select(n); end1=clock();
  • 7. 7 time=(end1-start)/CLOCKS_PER_SEC; cout<<time<<" sec is taken"; print(n); break; case 2: bubble(n); print(n); break; case 3: insertion(n); print(n); break; case 4: mergesort(a,n); print(n); break; case 5: heap(a); print(n); break; case 6: last=n-1; quick(start,last); print(n); break; } if(l==7) { for(i=0;i<10000;i++) { x=a[i]/10; b1[x].push_back(a[i]); } for(i=0;i<1000;i++) { sort(b1[i].begin(),b1[i].end()); } for(i=0;i<1000;i++) { for(j=0;j<b1[i].size();j++) cout<<b1[i][j]<<" "; } }
  • 9. 9 } } void select(int n) { int i,k,m,l,temp; for(l=0;l<n-1;l++) { k=a[l]; m=l; for(i=l;i<n;i++) { if(k>a[i]) { m=i; k=a[i]; } } temp=a[l]; a[l]=k; a[m]=temp; } } void bubble(int n) { int i,k,temp; for(i=0;i<n-1;i++) { for(k=0;k<n-i-1;k++) { if(a[k]>a[k+1]) { temp=a[k+1] ; a[k+1]=a[k]; a[k]=temp; } } } } void insertion(int n) { int i=0,j,k,temp; for(i=1;i<n;i++) {
  • 10. 10 j=i-1; k=a[i]; while(k<a[j]&&j>=0) { a[j+1]=a[j]; j--; } a[j+1]=k; }} void mergesort(int a[10003],int n) { int i,j,k,mid=n/2; int left[10000],right[10000]; if(n<2) return; else { for(i=0;i<mid;i++) { left[i]=a[i]; } for(j=mid;j<n;j++) { right[j-mid]=a[j]; } mergesort(left,mid); mergesort(right,n-mid); merge1(left,right,a,mid,n-mid); } } void quick(int start,int last) { int index; if(start>=last) return; index=partion(start,last); quick(start,index-1); quick(index+1,last); } void print(int n) { int i,s=0;
  • 11. 11 for(i=0;i<n;i++) { printf("%d ",a[i]); s++; } } void merge1(int l[10003],int r[10003],int a[10003],int mid,int t) { int nl,nr,i=0,j=0,k=0; nl=mid; nr=t; while(i<nl&&j<nr) { if(l[i]<r[j]) { a[k]=l[i]; k++; i++; } else { a[k]=r[j]; k++; j++; } } while(i<nl) { a[k]=l[i]; k++; i++; } while(j<nr) { a[k]=r[j]; k++; j++; } } int partion(int start,int last) { int j,k,i,temp; j=start;
  • 12. 12 k=a[last]; for(i=start;i<=last;i++) { if(a[i]<k) { temp=a[i]; a[i]=a[j]; a[j]=temp; j++; } } temp=a[j]; a[j]=a[last]; a[last]=temp; return(j); } void heap(int a[ ]) { int heap_size = n; for (int i = n / 2 - 1; i >= 0; i--) heapify(a, heap_size, i); for(int i = n-1; i >=0 ; i-- ) { swap(a[ 0 ], a[ i ]); heap_size = heap_size - 1; heapify(a,heap_size,0); } } void heapify(int a[], int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if (l < n && a[l] > a[largest]) largest = l; if (r < n && a[r] > a[largest]) largest = r; if (largest != i) { swap(a[i], a[largest]); heapify(a, n, largest); } }
  • 13. 13
  • 14. 14 PROGRAM FOR BREADTH FIRST SEAFRCH. #include<bits/stdc++.h> using namespace std; vector<long int >vect[100000]; long int b[10000]; void dfs(long int k) { long int i,l; stack<long int > s; s.push(k); while(!s.empty()) { l=s.top(); s.pop(); if(b[l]!=1){ for(i=0;i<vect[l].size();i++) { s.push(vect[l][i]); } b[l]=1; cout<<l<<" ";} } } int main() { long int i,j,k,l,m,n,u,v; cin>>m>>n; for(i=0;i<n;i++) { cin>>u>>v; vect[u].push_back(v); vect[v].push_back(u); } for(i=1;i<=n;i++) { if(b[i]==0) dfs(i); }}
  • 15. 15 PROGRAM FOR DEPTH FIRST SEARCH #include<bits/stdc++.h> using namespace std; vector<long int >vect[100000]; long int b[10000]; void dfs(long int k) { long int i,l; stack<long int > s; s.push(k); while(!s.empty()) { l=s.top(); s.pop(); if(b[l]!=1){ for(i=0;i<vect[l].size();i++) { s.push(vect[l][i]); } b[l]=1; cout<<l<<" ";} }} int main() { long int i,j,k,l,m,n,u,v; cout<<"enter the number of vertices and edgesn"; cin>>m>>n; for(i=0;i<n;i++) { cin>>u>>v; vect[u].push_back(v); vect[v].push_back(u); } for(i=1;i<=n;i++) { if(b[i]==0) dfs(i); }}
  • 16. 16 PROGRAM FOR KRUSKAL ALGORITHM #include<bits/stdc++.h> #define MAXN 102 using namespace std; int P[MAXN], Rank[MAXN]; int Node, edg, Cost; struct edge { int u, v; int cost; }; edge Edge[MAXN*MAXN]; edge Path[MAXN]; int com(const void *xy, const void *xz) { edge *x = (edge*)xy; edge *y = (edge*)xz; return (x->cost - y- >cost); } void In() { int i; for(i = 1; i<= Node; i++) { P[i] = i; Rank[i] = 1; } } int Find(int n) { if(P[n] != n) P[n] = Find(P[n]); return P[n]; } void Link(int x, int y) { if(Rank[x] > Rank[y]) { P[y] = x; } else { P[x] = y; if(Rank[x] == Rank[y]) Rank[y]++; } } void Kruscal() { int x, y, total = 0; Cost = 0; for(int i = 0; i<edg; i++) {
  • 17. 17 x = Find(Edge[i].u); y = Find(Edge[i].v); if(x != y) { Path[total++] = Edge[i]; Link(x,y); Cost += Edge[i].cost; if(total == Node - 1) break; } } } void Cal() { qsort(Edge,edg,sizeof(edge),com); Kruscal(); cout<<"Total Cast :"<<Cost<<endl; for(int i = 0; i<Node-1; i++) cout<<Path[i].u<<" "<<Path[i].v<<" "<<Path[i].cost<<endl; } int main() { int i; cout<<"enter the number of verticesn"; cout<<"enter the number of edgesn"; while(cin>>Node>>edg) { In(); for(i = 0; i<edg; i++) cin>>Edge[i].u>>Edge[i].v>>Edge[i].cost; Cal(); } }
  • 18. 18 PROGRAM FOR PRIMS ALGORITHM #include<bits/stdc++.h> using namespace std; long int a[1000][1000],b[1000],near[1000][3]; vector<long int> vect; #define inf 10000000 int main() { long int i,j,k,l,m,n,u,v,c,s=inf,cost=0,co=0,pos; cin>>n>>m; for(i=0;i<m;i++) { cin>>u>>v>>c; a[u][v]=c; a[v][u]=c; } for(i=1;i<=n;i++) { near[i][2]=inf; for(j=1;j<=n;j++) { if(a[i][j]==0) a[i][j]=inf; } } pos=1; for(i=1;i<=n;i++) { if(s>a[1][i]) s=a[1][i],pos=i; } vect.push_back(1); vect.push_back(pos); cost+=s; co++; b[1]=-1; b[pos]=-1; while(co!=n-1) { for(i=1;i<=n;i++) { if(b[i]!=-1) {
  • 20. 20 PROGRAM TO IMPLEMENT DIJKSTRA ALGORITHM #include<bits/stdc++.h> using namespace std; bitset<1000005> b; #define pi pair<int,int> #define pb push_back #define mp make_pair #define inf 100000000000009 vector<pi> edges[1000002]; long long dis[1000002],node[1000002]; void printpath(long long int n) { if(n!=1) printpath(node[n]); cout<<n<<" "; } void path(long long int k) { priority_queue<long long int> q; q.push(k); long long int i,l; while(!q.empty()) { l=q.top(); q.pop(); for(i=0;i<edges[l].size();i++) { if(dis[l]+edges[l][i].second<dis[edges[l][i].first]) { dis[edges[l][i].first]=dis[l]+edges[l][i].second; q.push(edges[l][i].first); node[edges[l][i].first]=l; } } } } int main() { long long int t,n,i,j,k,l,u,v,w; // printf("enter the number of vertices");
  • 21. 21 cin>>n; // printf("enter the number of vertices"); cin>>t; while(t--) { cin>>u>>v>>w; edges[u].pb(mp(v,w)); edges[v].pb(mp(u,w)); } for(i=2;i<=n;i++) dis[i]=inf; dis[1]=0; path(1); if(dis[n]==inf) cout<<"-1"<<endl; else{ printpath(n); cout<<endl; } }
  • 22. 22 PROGRAM TO IMPLEMET FLOYAD WARSHAL ALGORITHM #include<bits/stdc++.h> using namespace std; #define inf 100000009; long int a[1000][1000]; int main() { long int i,j,k,l,m,n,u,v,w; cout<<"enter the number of vertex"; cin>>n; cout<<"enter the number edges"; cin>>m; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) a[i][j]=inf; } for(i=1;i<=m;i++) { cin>>u>>v>>w; a[u][v]=w; a[v][u]=w; a[i][i]=0; } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][j]=min(a[i][j],a[i][k]+a[k][j]); }}} for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<"n";}}
  • 23. 23 PROGRAM FOR TOPOLOGICAL SORT #include<bits/stdc++.h> using namespace std; vector<long int >vect[100000]; long int b[10000]; stack<long int> s1; void dfs(long int k) { long int i,l; b[k]=1; for(i=0;i<vect[k].size();i++) { if(b[vect[k][i]]!=1) dfs(vect[k][i]);} s1.push(k);} int main() { long int i,j,k,l,m,n,u,v; cin>>m>>n; for(i=0;i<n;i++) { cin>>u>>v; vect[u].push_back(v); //vect[v].push_back(u); } for(i=1;i<=n;i++) { if(b[i]==0) dfs(i); } while(!s1.empty()){ k=s1.top(); s1.pop(); cout<<k<<" "; }
  • 24. 24 PROGRAM FOR MEDIN ORDER STATISTICS #include<bits/stdc++.h> using namespace std; long int a[10000],l,m,n; long int partion(long int start,long int last) { long int j,k,i,temp; j=start; k=a[last]; for(i=start;i<=last;i++) { if(a[i]<=k&&i!=last) { temp=a[i]; a[i]=a[j]; a[j]=temp; j++; } } temp=a[j]; a[j]=a[last]; a[last]=temp; l=j; if(l==m-1){ return l; } else if(m-1>l) partion(l+1,n-1); else partion(0,l-1); } int main() { long int i,j=0,s,z=5; cout<<"enter the size of arrayn"; cin>>n; cout<<"enter the array elementsn"; for(i=0;i<n;i++) cin>>a[i]; while(z--) { cout<<"enter the position to findn"; cin>>m;
  • 26. 26 PROGRAM FOR BELLMAN FORD ALGORITHM #include<bits/stdc++.h> using namespace std; # define inf 100000009 long int a[100000][5],dis[100005],dis1[100005]; int main() { long int i,j,k,l,m,n,u,v,w,z=0; cout<<"enter the number of verticesn"; cin>>n; cout<<"enter the number of edgesn"; cin>>m; for(i=0;i<=n;i++) dis[i]=inf; dis[1]=0; for(i=0;i<m;i++) { cin>>u>>v>>w; a[i][0]=u; a[i][1]=v; a[i][2]=w; } for(i=1;i<=n-1;i++) { for(j=0;j<m;j++) { u=a[j][0]; v=a[j][1]; w=a[j][2]; if(dis[u]+w<dis[v]) dis[v]=dis[u]+w; if(dis[v]+w<dis[u]) dis[u]=dis[v]+w; } } for(i=1;i<=n;i++) dis1[i]=dis[i]; for(j=0;j<m;j++) { u=a[j][0]; v=a[j][1]; w=a[j][2]; if(dis[u]+w<dis[v]) dis[v]=dis[u]+w;
  • 28. 28 PROGRAM FOR MATRIX CHAIN MULTIPLICATION #include<bits/stdc++.h> using namespace std; long int a[100000],b[100][100]; int main() { long int i,j,k,l,m,n,p,z=9999999; cout<<"enter the NUMBER of matricen"; cin>>n; cout<<"enter the matrices ordern"; cin>>a[0]>>a[1]; for(i=2;i<=n;i++) { cin>>k>>m; a[i]=m; } //cin>>a[n]; for(l=2;l<=n;l++) { for(i=1;i<=n-l+1;i++) { j=i+l-1; b[i][j]=z; for(k=i;k<=j-1;k++) { p=b[i][k]+b[k+1][j]+a[i-1]*a[k]*a[j];
  • 30. 30 PROGRAM FOR LONGEST COMMOM SUSEQUENCE #include<bits/stdc++.h> using namespace std; long int i,j,k,l,m,n,l1,c[1000][1000]; int main() { char a[1000],b[1000]; cout<<"enter the first stringn"; cin>>a; cout<<"enter the second stringn"; cin>>b; l=strlen(a); l1=strlen(b); for(i=l-1;i>=0;i--) a[i+1]=a[i]; for(i=l1-1;i>=0;i--) b[i+1]=b[i]; for(i=1;i<=l;i++) { for(j=1;j<=l1;j++) { if(a[i]==b[j]) { c[i][j]=c[i-1][j-1]+1;
  • 32. 32 PROGRAM FOR HUFFMAN CODING. #include <bits/stdc++.h> using namespace std; long int i,j,k,n,f[1000]; char d[1000]; struct minheap { char data; int freq; minheap *left, *right; minheap(char data, int freq) { left = right = NULL; this->data = data; this->freq = freq; } }; struct compare { bool operator()(minheap* l, minheap* r) { return (l->freq > r->freq); } }; void print(struct minheap* root, string str) { if (!root) return; if (root->data != '#')
  • 33. 33 cout << root->data << ": " << str << "n"; print(root->left, str + "0"); print(root->right, str + "1"); } void Huffman() { struct minheap *left, *right, *top; priority_queue<minheap*, vector<minheap*>, compare> minheaps; for (int i = 0; i < n; ++i) minheaps.push(new minheap(d[i], f[i])); while (minheaps.size() != 1) { left = minheaps.top(); minheaps.pop(); right = minheaps.top(); minheaps.pop(); top = new minheap('#', left->freq + right->freq); top->left = left; top->right = right; minheaps.push(top); } print(minheaps.top(), ""); } int main() { cout<<"enter the number of charractersn"; cin>>n; cout<<"enter the charraectersn";
  • 35. 35 PROGRAM FOR OPTIMAL BST. #include<bits/stdc++.h> using namespace std; long int c[1000][1000],f[1000],a[1000],p; int sum(int i, int j) { int s = 0; for (int k = i; k <=j; k++) s += f[k]; return s; } int main() { long int i,j,k,l,m,n,z=999999; cout<<"enter the size of bstn"; cin>>n; cout<<"enter the bst elementn"; for(i=0;i<n;i++) cin>>a[i]; cout<<"enter the frequency of elementsn"; for(i=0;i<n;i++) cin>>f[i]; for(i=0;i<n;i++) c[i][i]=f[i]; for(l=2;l<=n;l++) { for(i=0;i<n-l+1;i++) {
  • 37. 37 STRING MATCHING USING NAÏVE ALGO #include<bits/stdc++.h> using namespace std; int main() { char a[1000],b[1000]; long int i,j,k,l,l1,m,n,s=0; cout<<"enter the stringn"; cin>>a; cout<<"enter the string to matchn"; cin>>b; l=strlen(a); l1=strlen(b); for(i=0;i<l;i++) { k=i; s=0; for(j=0;j<l1;j++) { if((a[k]!=b[j])||((k==l-1)&&(j!=l1-1))) { s=1; break; } else {
  • 39. 39 STRING MATCHING USING FINITE AUTOMATA METHOD #include<bits/stdc++.h> using namespace std; long int c[1000][256]; char a[1000],b[1000]; set<char > d; set<char>::iterator iter; long int nextstate(long int st,long int m,char x) { long int nextst,i,j,l; if (st < m && x ==b[st]) return st+1; for (nextst = st; nextst > 0; nextst--) { if (b[nextst-1] == x) { for (i = 0; i < nextst-1; i++) if (b[i] != b[st-nextst+1+i]) break; if (i == nextst-1) return nextst; }}} void form() { long int i,j,k,l,m,p,q; m=strlen(b);
  • 40. 40 for(i=0;i<=m;i++) { for(iter=d.begin(); iter!=d.end();++iter) { p=(*iter); l=nextstate(i,m,p); c[i][p]=l; }}} void match() { long int i,j,k=0,m,z=0,l; m=strlen(a); l=strlen(b); for(j=0;j<m;j++) { k=c[k][a[j]]; if(k==l) { z=1; break; }}if(z==1) { cout<<"found at pos"<<j-l+2; } else
  • 41. 41 cout<<"not found"; } int main() { long int i,j,k,l,m,n; cout<<"enter the stringn"; cin>>a; cout<<"enter the patternn"; cin>>b; l=strlen(b); for(i=0;i<l;i++) { d.insert(b[i]); } form(); match(); }
  • 42. 42 STRING MATCHING USING RABIN KARP ALGO #include<bits/stdc++.h> using namespace std; int main() { long int i,j,k,l,l1,p=199,d=256,m=1,n,x=0,y=0,flag=0; char a[1000],b[1000]; cout<<"enter the stringn"; cin>>a; cout<<"enter the patternn"; cin>>b; l=strlen(a); l1=strlen(b); for(i=0;i<l1-1;i++) m=(m*d)%p; for(i=0;i<l1;i++) { x=(x*d+b[i])%p; y=(y*d+a[i])%p; } for(i=0;i<=l-l1;i++) { // cout<<x<<" "<<y<<"n"; if(x==y) { for(j=0;j<l1;j++)
  • 43. 43 { if(a[i+j]!=b[j]) break; } } if(j==l1){ cout<<"found at index "<<i+1<<"n"; flag=1; break; } if ( i <l-l1 ) { y = (d*(y - a[i]*m) + a[i+l1])%p; if (y < 0) y = (y + p); }} if(flag==0) cout<<"not found"; }
  • 44. 44 STRING MATCHING USING KMP ALGO #include<bits/stdc++.h> using namespace std; void computeLPSArray(char *pat, int M, int *lps); void KMPSearch(char *pat, char *txt) { int M = strlen(pat); int N = strlen(txt); int lps[M]; computeLPSArray(pat, M, lps); int i = 0; int j = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; }
  • 45. 45 if (j == M) { printf("Found pattern at index %d n", i-j); j = lps[j-1]; } else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j-1]; else i = i+1; } } } void computeLPSArray(char *pat, int M, int *lps) { int len = 0; lps[0] = 0; int i = 1; while (i < M)
  • 46. 46 { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len-1]; } else { lps[i] = 0; i++; } } } } int main() { char txt[1000],pat[1000];
  • 47. 47 cout<<"enter the string "; cin>>txt; cout<<"enter the patternn"; cin>>pat; KMPSearch(pat, txt); return 0; }