(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
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]<<" ";
}
}
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;
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++) {
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);
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;
}