ADA Practical Solution
ADA Practical Solution
1. Write a program to implement insert data into Array and display Array.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf("=========Array===========");
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
printf(“a[%d]:”,i);
printf(“%d\n”,a[i]);
getch();
1|P ag e
Analysis And Design of Algorithms
Output
========= Array===========
Array Elements
a[0]:70
a[1]:30
a[2]:20
a[3]:50
a[4]:60
a[5]:10
a[6]:40
a[7]:80
a[8]:05
a[9]:90
2|P ag e
Analysis And Design of Algorithms
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
for(j=0;j<=n-2-i;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
3|P ag e
Analysis And Design of Algorithms
for(i=0;i<=n-1;i++)
printf(“a[%d]:”,i);
printf(“%d\n”,a[i]);
getch();
4|P ag e
Analysis And Design of Algorithms
Output
Sorted Elements
a[0]:05
a[1]:10
a[2]:20
a[3]:30
a[4]:40
a[5]:50
a[6]:60
a[7]:70
a[8]:80
a[9]:90
5|P ag e
Analysis And Design of Algorithms
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
for(j=i+1;j<=n-1-i;j++)
if(a[j]<a[min])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
6|P ag e
Analysis And Design of Algorithms
for(i=0;i<=n-1;i++)
printf(“a[%d]:”,i);
printf(“%d\n”,a[i]);
getch();
7|P ag e
Analysis And Design of Algorithms
Output
Sorted Elements
a[0]:05
a[1]:10
a[2]:20
a[3]:30
a[4]:40
a[5]:50
a[6]:60
a[7]:70
a[8]:80
a[9]:90
8|P ag e
Analysis And Design of Algorithms
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
Key=a[i];
for(j=i-1;j>=0;j--)
if(a[j]>key)
a[j+1]=a[j];
a[j]=key;
9|P ag e
Analysis And Design of Algorithms
for(i=0;i<=n-1;i++)
printf(“a[%d]:”,i);
printf(“%d\n”,a[i]);
getch();
10 | P a g e
Analysis And Design of Algorithms
Output
Sorted Elements
a[0]:05
a[1]:10
a[2]:20
a[3]:30
a[4]:40
a[5]:50
a[6]:60
a[7]:70
a[8]:80
a[9]:90
11 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
void main()
clrscr();
printf("=========Sequential Search===========");
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
scanf(“%d”,&key);
for(i=0;i<=n-1;i++)
if(a[i]==key)
break;
getch();
12 | P a g e
Analysis And Design of Algorithms
Output
13 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
void main()
clrscr();
printf("=========Binary Search===========");
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high)
mid=(low+high)/2;
if(key==a[mid])
break;
14 | P a g e
Analysis And Design of Algorithms
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
getch();
Output
15 | P a g e
Analysis And Design of Algorithms
7. Write a program to search key using Binary Search Method using Divide and
Conquer.
#include<stdio.h>
#include<conio.h>
#define size 20
int n;
void main()
int a[size],key,i,flag,low,high;
clrscr();
printf("=========Binary Search===========");
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
scanf(“%d”,&key);
low=0;
high=n-1;
flag=bin_search(a,key,low,high);
printf("\n The element you want to search is found at location a[%d] ",flag);
getch();
16 | P a g e
Analysis And Design of Algorithms
int m;
if(key==a[m])
return m;
else if(key<a[m])
else
Output
========= Binary Search ===========
17 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n);
// Input Array
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
low=1, high=n;
MergeSort(a,low,high);
for(i=0;i<=1;i++)
printf(“a[%d]:”,i);
printf(“%d\n”,a[i]);
getch();
18 | P a g e
Analysis And Design of Algorithms
int mid;
if(low<high)
mid=(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Combine(a,low,mid,high);
}
}
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while (i<=mid&&j<=high)
if(a[i]<=a[j])
temp[k]=a[i];
i++;
k++;
else
temp[k]=a[j];
j++;
19 | P a g e
Analysis And Design of Algorithms
k++;
while(i<=mid)
temp[k]=a[i];
i++;
k++;
while(j<=mid)
temp[k]=a[j];
j++;
k++;
for(k=low;k<=high;k++)
a[k]=temp[k];
20 | P a g e
Analysis And Design of Algorithms
Output
Sorted Elements
a[0]:05
a[1]:10
a[2]:20
a[3]:30
a[4]:40
a[5]:50
a[6]:60
a[7]:70
a[8]:80
a[9]:90
21 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
#define size 20
int n;
void main()
int i;
int a[size];
clrscr();
scanf("%d",&n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
int m,i;
if(low<high)
22 | P a g e
Analysis And Design of Algorithms
quick(a,low,m-1);//spliting of list
quick(a,m+1,high);//spiting of list
int pivot=a[low],i=low,j=high;
while(i<=j)
while(a[i]<=pivot)i++;
while(a[j]>pivot)j--;
if(i<j)swap(a,&i,&j);
swap(a,&low,&j);
return j;
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
23 | P a g e
Analysis And Design of Algorithms
Output
Enter 5 numbers: 80 70 90 30 10
Sorted array is :
10 30 70 80 90
24 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
void main()
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("=========Matrix Addition===========");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
c[i][j]=0;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<=1;i++)
25 | P a g e
Analysis And Design of Algorithms
for(j=0;j<=1;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
printf("\n\n");
getch();
Output
=========Matrix Addition===========
6 8
10 12
26 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
void main()
int a[2][2],b[2][2],c[2][2],i,j,k;
clrscr();
printf("=========Matrix Multiplication===========");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
c[i][j]=0;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
27 | P a g e
Analysis And Design of Algorithms
for(j=0;j<2;j++)
c[i][j]=0;
for(k=0;k<2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\t%d",c[i][j]);
printf("\n\n");
getch();
28 | P a g e
Analysis And Design of Algorithms
Output
=========Matrix Multiplication===========
19 22
43 50
29 | P a g e
Analysis And Design of Algorithms
12. Write a program to implement strassion matrices multiplication using Divide and
Conquer strategy.
//Matrix multiplication using strassion multiplication
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3],b[3][3],i,j;
int s1,s2,s3,s4,s5,s6,s7;
int c[3][3];
clrscr();
printf("=========Matrix Multiplication===========");
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
scanf("%d",&b[i][j]);
30 | P a g e
Analysis And Design of Algorithms
s1=(a[1][1]+a[2][2])*(b[1][1]+b[2][2]);
s2=(a[2][1]+a[2][2])*b[1][1];
s3=a[1][1]*(b[1][2]-b[2][2]);
s4=a[2][2]*(b[2][1]-b[1][1]);
s5=(a[1][1]+a[1][2])*b[2][2];
s6=(a[2][1]-a[1][1])*(b[1][1]+b[1][2]);
s7=(a[1][2]-a[2][2])*(b[2][1]+b[2][2]);
c[1][1]=s1+s4-s5+s7;
c[1][2]=s3+s5;
c[2][1]=s2+s4;
c[2][2]=s1+s3-s2+s6;
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
printf("\t%d",c[i][j]);
printf("\n\n");
getch();
31 | P a g e
Analysis And Design of Algorithms
Output
=========Matrix Multiplication===========
19 22
43 50
32 | P a g e
Analysis And Design of Algorithms
13. Write a program to implement Exponential of two numbers using Divide and
Conquer.
#include<stdio.h>
#include<conio.h>
int exp(int,int);
void main()
{
int x,n;
clrscr();
printf("Exponentiation (x,n)\n");
printf("Enter x: ");
scanf("%d",&x);
printf("Enter n: ");
scanf("%d",&n);
getch();
if(n==0)
return 1;
if(n%2==0)
else
Output
Exponentiation (x,n)
Enter x: 5
Enter n: 3
33 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
#define SIZE 20
void main()
int G[SIZE][SIZE],nodes,edges;
int v1,v2,length,i,j;
clrscr();
scanf("%d",&nodes);
scanf("%d",&edges);
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
G[i][j]=0;
for(i=0;i<edges;i++)
scanf("%d %d",&v1,&v2);
scanf("%d",&length);
G[v1][v2]=G[v2][v1]=length;
34 | P a g e
Analysis And Design of Algorithms
getch();
printf("\n\t");
clrscr();
Prim(G,nodes);
getch();
int tree[SIZE],i,j,k;
int min_dist,v1,v2,total=0;
for(i=0;i<nodes;i++)
tree[i]=0;
tree[0]=1;
for(k=1;k<=nodes-1;k++)
min_dist=INFINITY;
for(i=0;i<=nodes-1;i++)
for(j=0;j<=nodes-1;j++)
if(G[i][j]*((tree[i]&&!tree[j])||(!tree[i]&&tree[j])))
if(G[i][j]<min_dist)
min_dist=G[i][j];
v1=i;
v2=j;
35 | P a g e
Analysis And Design of Algorithms
tree[v1]=tree[v2]=1;
total=total+min_dist;
36 | P a g e
Analysis And Design of Algorithms
Output
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 0 1
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 1 2
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 2 3
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 3 4
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 4 0
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 1 3
Enter Edges by V1 and V2: [Read the graph from starting nodes 0]: 4 2
37 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
int v1;
int v2;
int cost;
}GR;
GR G[20];
int tot_edges,tot_nodes;
void create();
void spanning_tree();
int Minimum(int);
void main()
clrscr();
create();
spanning_tree();
getch();
void create()
int k;
scanf("%d",&tot_nodes);
scanf("%d",&tot_edges);
38 | P a g e
Analysis And Design of Algorithms
for(k=0;k<=tot_edges-1;k++)
scanf("%d %d",&G[k].v1,&G[k].v2);
scanf("%d",&G[k].cost);
void spanning_tree()
int count,k,v1,v2,i,j,tree[10][10],pos,parent[10],sum;
count=0;
k=0;
sum=0;
for(i=0;i<=tot_nodes-1;i++)
parent[i]=i;
while(count!=tot_nodes-1)
pos=Minimum(tot_edges);
if(pos==-1)
break;
v1=G[pos].v1;
v2=G[pos].v2;
i=Find(v1,parent);
39 | P a g e
Analysis And Design of Algorithms
j=Find(v2,parent);
if(i!=j)
tree[k][0]=v1;
tree[k][1]=v2;
k++;
count++;
sum+=G[pos].cost;
Union(i,j,parent);
G[pos].cost=INFINITY;
if(count==tot_nodes-1)
printf("\n-------------------\n");
for(i=0;i<tot_nodes-1;i++)
printf("[%d",tree[i][0]);
printf("-");
printf("%d",tree[i][1]);
printf("]");
printf("\n----------------------");
else
40 | P a g e
Analysis And Design of Algorithms
int Minimum(int n)
int i,small,pos;
small=INFINITY;
pos=-1;
for(i=0;i<=n-1;i++)
if(G[i].cost<small)
small=G[i].cost;
pos=i;
return pos;
while(parent[v2]!=v2)
v2=parent[v2];
return v2;
if(i<j)
parent[j]=i;
41 | P a g e
Analysis And Design of Algorithms
else
parent[i]=j;
Output
Graph Creation by adjacenvy matrix
42 | P a g e
Analysis And Design of Algorithms
-------------------
[0-1][1-3][3-4][2-4]
----------------------
43 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
int path[20];
int i;
for(i=d;i!=s;i=path[i])
printf("%d<-",i);
printf("%d",i);
int i,j,v1,v2,md;
int sr[10];
for(i=0;i<tn;i++)
dist[i]=cost[s][i];
sr[i]=0;
path[i]=s;
sr[s]=1;
for(i=1;i<tn;i++)
md=INFINITY;
44 | P a g e
Analysis And Design of Algorithms
v1=-1;
for(j=0;j<tn;j++)
if(sr[j]==0)
if(dist[j]<md)
md=dist[j];
v1=j;
sr[v1]=1;
for(v2=0;v2<tn;v2++)
if(sr[v2]==0)
if(dist[v1]+cost[v1][v2]<dist[v2])
dist[v2]=dist[v1]+cost[v1][v2];
path[v2]=v1;
void main()
int tn,te,val,i,j,k,cost[20][20],dist[20],s[20],c=0;
clrscr();
45 | P a g e
Analysis And Design of Algorithms
printf("\nCreation of graph");
scanf("%d",&tn);
for(i=0;i<tn;i++)
for(j=0;j<=tn;j++)
if(i==j)
cost[i][j]=0;
else
cost[i][j]=INFINITY;
scanf("%d",&te);
while(c<te)
scanf("%d %d",&i,&j);
scanf("%d",&val);
cost[j][i]=val;
cost[i][j]=val;
c++;
for(i=0;i<tn;i++)
getch();
46 | P a g e
Analysis And Design of Algorithms
for(j=0;j<tn;j++)
dij(tn,cost,i,dist);
if(dist[j]==INFINITY)
else
disp(i,j,dist);
getch();
47 | P a g e
Analysis And Design of Algorithms
Output
Creation of graph
Enter Vi,Vj: 0 1
Enter Vi,Vj: 0 2
Enter Vi,Vj: 1 3
Enter Vi,Vj: 1 4
Enter Vi,Vj: 2 4
Enter Vi,Vj: 3 4
When source = 0
48 | P a g e
Analysis And Design of Algorithms
0 The length = 0
When source = 1
1 The length = 0
When source = 2
2 The length = 0
49 | P a g e
Analysis And Design of Algorithms
When source = 3
3 The length = 0
When source = 4
4 The length = 0
50 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
struct object
float weight;
float profit;
float ratio;
}obj[50];
void knapsack(float,int);
void main()
int i,n;
float W;
clrscr();
scanf("%f",&W);
scanf("%d",&n);
printf("\n");
for(i=0;i<=n-1;i++)
printf("\nObject-%d",i+1);
printf("\n\tWeight : ");
scanf("%f",&obj[i].weight);
printf("\n\tProfit : ");
scanf("%f",&obj[i].profit);
obj[i].ratio=obj[i].profit/obj[i].weight;
knapsack(W,n);
51 | P a g e
Analysis And Design of Algorithms
getch();
int i;
float x[50],weight,profit=0;
weight=0;
i=0;
x[i]=0;
if(weight+obj[i].weight<=W)
x[i]=1;
weight=weight+obj[i].weight;
else
x[i]=(float)(W-weight)/obj[i].weight;
weight=W;
profit=profit+x[i]*obj[i].profit;
i++;
printf("\n\n\t\tProfit= %f ",profit);
52 | P a g e
Analysis And Design of Algorithms
Output
Enter no of objects: 3
Object-1
Weight : 15
Profit : 20
Object-2
Weight : 3
Profit : 3
Object-3
Weight : 5
Profit : 2
Profit= 23.799999
53 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
#define MAX 10
int Binomial(int,int);
void main()
int n,k;
clrscr();
scanf("%d%d",&n,&k);
printf("C(%d,%d)=%d",n,k,Binomial(n,k));
getch();
int i,j,C[MAX][MAX];
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
C[i][j]=0;
for(i=0;i<=n;i++)
for(j=0;j<=k;j++)
54 | P a g e
Analysis And Design of Algorithms
if((j==0)||(i==j))
C[i][j]=1;
else
C[i][j]=C[i-1][j-1]+C[i-1][j];
return C[n][k];
Output
55 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
void main()
int n,e[3],x[3],t[3][20],f1[20],f2[20];
int l1[20],l2[20],f_star,l_star,i,j,a[3][20];
clrscr();
scanf("%d%d",&e[1],&e[2]);
scanf("%d%d",&x[1],&x[2]);
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("a[1][%d]: ",i);
scanf("%d",&a[1][i]);
printf("\n");
for(i=1;i<=n;i++)
printf("a[2][%d]: ",i);
scanf("%d",&a[2][i]);
56 | P a g e
Analysis And Design of Algorithms
for(i=1;i<n;i++)
printf("t[1][%d]: ",i);
scanf("%d",&t[1][i]);
printf("\n");
for(i=1;i<n;i++)
printf("t[2][%d]: ",i);
scanf("%d",&t[2][i]);
f1[1]=e[1]+a[1][1];
f2[1]=e[2]+a[2][1];
if(f1[1]<=f2[1])
l1[1]=1;
else
l2[1]=1;
for(j=2;j<=n+1;j++)
if(f1[j-1]+a[1][j]<=f2[j-1]+t[2][j-1]+a[1][j])
f1[j]=f1[j-1]+a[1][j];
l1[j]=1;
else
57 | P a g e
Analysis And Design of Algorithms
f1[j]=f2[j-1]+t[2][j-1]+a[1][j];
l1[j]=2;
if(f2[j-1]+a[2][j]<=f1[j-1]+t[1][j-1]+a[2][j])
f2[j]=f2[j-1]+a[2][j];
l2[j]=2;
else
f2[j]=f1[j-1]+t[1][j-1]+a[2][j];
l2[j]=1;
if(f1[n]+x[1]<=f2[n]+x[2])
f_star=f1[n]+x[1];
l_star=1;
else
f_star=f2[n]+x[2];
l_star=2;
printf("\nSTATION\tLINE\n");
for(i=2;i<=n+1;i++)
58 | P a g e
Analysis And Design of Algorithms
if(l_star==1)
getch();
Output
Enter the entry time for line-1 and line-2 : 4 2
a[1][1]: 2
a[1][2]: 8
a[1][3]: 9
a[1][4]: 3
a[1][5]: 4
a[1][6]: 1
a[2][1]: 6
a[2][2]: 11
a[2][3]: 2
a[2][4]: 2
a[2][5]: 7
a[2][6]: 3
59 | P a g e
Analysis And Design of Algorithms
t[1][1]: 3
t[1][2]: 1
t[1][3]: 2
t[1][4]: 1
t[1][5]: 3
t[2][1]: 3
t[2][2]: 4
t[2][3]: 1
t[2][4]: 1
t[2][5]: 3
FASTEST TIME = 28
STATION LINE
1 1
2 1
3 2
4 2
5 1
6 2
60 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
int n,d[100],c[100][100];
if(a<=b)
return(a);
else
return(b);
Int no_of_coins(int N)
inti,j;
for(i=1;i<=n;i++)
c[i][0]=0;
for(i=1;i<=n;i++)
for(j=1;j<=N;j++)
c[i][j]=INFINITY;
else if(i==1)
61 | P a g e
Analysis And Design of Algorithms
c[i][j]=1+c[1][j-d[1]];
else if(j<d[i])
c[i][j]=c[i-1][j];
else
c[i][j]=min(c[i-1][j],1+c[i][j-d[i]]);
return(c[n][N]);
void main()
inti,j,N;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("d[%d]:",i);
scanf("%d",&d[i]);
scanf("%d",&N);
getch();
}
62 | P a g e
Analysis And Design of Algorithms
Output
d[1]:1
d[2]:4
d[3]:6
63 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(inta,int b)
if(a>=b)
return(a);
else
return(b);
intk,j,i,t[100][100];
for(i=0;i<=n;i++)
for(j=0;j<=W;j++)
t[i][0]=0;
t[0][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=W;j++)
if(j<w[i])
t[i][j]=t[i-1][j];
else if(j>=w[i])
t[i][j]=max(t[i-1][j],(v[i]+t[i-1][j-w[i]]));
64 | P a g e
Analysis And Design of Algorithms
i=n;
k=W;
if(t[i][k]!=t[i-1][k])
i--;
k=k-w[i];
else
i--;
return t[n][W];
void main()
inti,j;
int v[20],w[20],W,n,pro;
clrscr();
scanf("%d",&n);
scanf("%d",&W);
for(i=1;i<=n;i++)
printf("Enter w[%d]:",i);
65 | P a g e
Analysis And Design of Algorithms
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
printf("Enter v[%d]:",i);
scanf("%d",&v[i]);
pro=dynamic_knap(n,W,w,v);
printf("Maximum profit=%d",pro);
getch();
Output
Enter number of total Items:4
Enter w[1]:2
Enter w[2]:3
Enter w[3]:4
Enter w[4]:5
Enter v[1]:3
Enter v[2]:4
Enter v[3]:5
Enter v[4]:6
Maximum profit=7
66 | P a g e
Analysis And Design of Algorithms
22. Write a program to implement to find shortest path using Floyd’s Algorithm using
Dynamic Programming.
//This program is for computing shortest path using Floyd's algorithm
#include<stdio.h>
#include<conio.h>
if(a<b)return(a);
else
return(b);
int d[10][20][20],i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[0][i][j]=wt[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[k][i][j]=min(d[k-1][i][j],d[k-1][i][k]+d[k-1][k][j]);
67 | P a g e
Analysis And Design of Algorithms
for(k=0;k<=n;k++)
printf("\nd(%d)=\n",k);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("%d\t",d[k][i][j]);
printf("\n");
void main()
int wt[20][20],n,i,j;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("\nwt[%d][%d]= ",i,j);
scanf("%d",&wt[i][j]);
68 | P a g e
Analysis And Design of Algorithms
fsp(wt,n);
getch();
Output
Create a graph using adjacency matrix
wt[1][1]= 0
wt[1][2]= 8
wt[1][3]= 7
wt[1][4]= -2
wt[1][5]= 999
wt[2][1]= 999
wt[2][2]= 0
wt[2][3]= 4
wt[2][4]= 999
wt[2][5]= 1
wt[3][1]= 999
wt[3][2]= 999
wt[3][3]= 0
wt[3][4]= 999
wt[3][5]= 1
wt[4][1]= 999
wt[4][2]= 999
wt[4][3]= 3
wt[4][4]= 0
wt[4][5]= -5
wt[5][1]= 6
wt[5][2]= 3
wt[5][3]= 999
wt[5][4]= 999
wt[5][5]= 0
Computing Shortest path
d(0)=
0 8 7 -2 999
69 | P a g e
Analysis And Design of Algorithms
999 0 4 999 1
999 999 0 999 1
999 999 3 0 -5
6 3 999 999 0
d(1)=
0 8 7 -2 999
999 0 4 997 1
999 999 0 997 1
999 999 3 0 -5
6 3 13 4 0
d(2)=
0 8 7 -2 9
999 0 4 997 1
999 999 0 997 1
999 999 3 0 -5
6 3 7 4 0
d(3)=
0 8 7 -2 8
999 0 4 997 1
999 999 0 997 1
999 999 3 0 -5
6 3 7 4 0
d(4)=
0 8 1 -2 -7
999 0 4 997 1
999 999 0 997 1
999 999 3 0 -5
6 3 7 4 -1
d(5)=
-1 -4 0 -3 -8
7 0 4 5 0
7 4 0 5 0
1 -2 2 -1 -6
5 2 6 3 -2
70 | P a g e
Analysis And Design of Algorithms
#include<conio.h>
int m[20][20],n;
if(i==j)
printf("A%d",i);
else
printf("(");
display(s,i,s[i][j]);
display(s,s[i][j]+1,j);
printf(")");
inti,j,l,k,q,s[20][20];
for(i=1;i<=n;i++)
m[i][i]=0;
for(l=2;l<=n;l++)
for(i=1;i<=(n-l+1);i++)
j=i+l-1;
71 | P a g e
Analysis And Design of Algorithms
m[i][j]=INFINITE;
for(k=i;k<j;k++)
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
m[i][j]=q;
s[i][j]=k;
printf("\nm[%d][%d]=%d",i,j,m[i][j]);
printf("\n");
display(s,1,n);
return(m[1][n]);
void main()
int p[20],i,j,c;
clrscr();
scanf("%d",&n);
for(i=0;i<=n;i++)
printf("p[%d]:",i);
scanf("%d",&p[i]);
72 | P a g e
Analysis And Design of Algorithms
c=matrix_chain(p);
getch();
Output
Enter total number of Matrices you want to multiply:4
p[0]=5
p[1]=4
p[2]=6
p[3]=2
p[4]=7
m[1][2]=120
m[2][3]=48
m[3][4]=84
m[1][3]=88
m[2][4]=104
m[1][4]=158
((A1(A2A3))A4)
73 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
intm,n,i,j,c[20][20],k;
char d[20][20],lc[20];
char a[20]="klokmknklok";
char b[20]="xkllknkllknyy";
clrscr();
m=strlen(a);
n=strlen(b);
//================================
for(i=-1;i<m;i++)
c[i][-1]=0;
for(j=0;j<n;j++)
c[-1][j]=0;
//=================================
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i]==b[j])
74 | P a g e
Analysis And Design of Algorithms
c[i][j]=c[i-1][j-1]+1;
d[i][j]='!';
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-i][j];
d[i][j]='|';
else
c[i][j]=c[i][j-1];
d[i][j]='-';
i=m-1;
j=n-1;
k=0;
while(i>=0 || j>=0)
if(d[i][j]=='!')
lc[k]=a[i];
i--;
j--;
k++;
else if(d[i][j]=='|')
75 | P a g e
Analysis And Design of Algorithms
i--;
else
j--;
lc[k]='\0';
for(i=k-1;i>=0;i--)
printf("%c",lc[i]);
getch();
Output
Longest Common Subsequence:klknklk
76 | P a g e
Analysis And Design of Algorithms
77 | P a g e
Analysis And Design of Algorithms
{
g[v1][v2]=1;
g[v2][v1]=1;
}
printf("Do you want to add more vertices:");
ans=getche();
}while(ans=='y');
printf("\nEnter the vertex from which u want to traverse:");
scanf("%d",&v);
bfs(v);
getch();
}
Output
Enter total number of Vertices in the given graph:4
Enter v1:0
Enter v2:1
Enter v1:0
Enter v2:2
Enter v1:1
Enter v2:3
Enter v1:2
Enter v2:3
78 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
intn,q[20];
intr,f;
int g[20][20];
int visited[20];
voiddfs(int v)
int i;
visited[v]=1;
printf("%d\n",v);
for(i=0;i<n;i++)
dfs(i);
void main()
int i,j;
int v1,v2,v;
char ans='y';
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
79 | P a g e
Analysis And Design of Algorithms
for(j=0;j<n;j++)
g[i][j]=0;
for(i=0;i<n;i++)
visited[i]=0;
do
printf("\nEnter v1:");
scanf("%d",&v1);
printf("Enter v2:");
scanf("%d",&v2);
if(v1>=n || v2>=n)
else
g[v1][v2]=1;
g[v2][v1]=1;
ans=getche();
}while(ans=='y');
scanf("%d",&v);
dfs(v);
getch();
80 | P a g e
Analysis And Design of Algorithms
Output
Enter total number of Vertices in the given graph:4
Enter v1:0
Enter v2:1
Enter v1:0
Enter v2:2
Enter v1:1
Enter v2:3
Enter v1:2
Enter v2:3
81 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
#include<math.h>
int w[10],p[10],m,n,y[10],x[10];
int fp=-1,fw;
int b,c,i;
b=cp;
c=cw;
for(i=k+1;i<=n;i++)
c=c+w[i];
if(c<m)
b=b+p[i];
else
return(b+(1-(c-m)/w[i])*p[i]);
return(b);
int j;
if(cw+w[k]<=m)
y[k]=1;
if(k<n)
bknap(k+1,cp+p[k],cw+w[k]);
82 | P a g e
Analysis And Design of Algorithms
fp=cp+p[k];
fw=cw+w[k];
for(j=1;j<=k;j++)
x[j]=y[j];
if(bound(cp,cw,k)>=fp)
y[k]=0;
if(k<n)
bknap(k+1,cp,cw);
fp=cp;
fw=cw;
for(j=1;j<=k;j++)
x[j]=y[j];
void main()
int i,j;
clrscr();
printf("Enter N:");
scanf("%d",&n);
scanf("%d",&m);
83 | P a g e
Analysis And Design of Algorithms
for(i=1;i<=n;i++)
printf("Enter w[%d]:",i);
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
printf("Enter p[%d]:",i);
scanf("%d",&p[i]);
bknap(1,0,0);
for(i=1;i<=n;i++)
if(x[i]==1)
printf("\nItem %d",i);
printf("\nWeight=%d",fw);
printf("\nProfit=%d",fp);
getch();
84 | P a g e
Analysis And Design of Algorithms
Output
Enter N:8
Enter w[1]:1
Enter w[2]:11
Enter w[3]:21
Enter w[4]:23
Enter w[5]:33
Enter w[6]:43
Enter w[7]:45
Enter w[8]:55
Enter p[1]:11
Enter p[2]:21
Enter p[3]:31
Enter p[4]:33
Enter p[5]:43
Enter p[6]:53
Enter p[7]:55
Enter p[8]:65
Item 1
Item 2
Item 3
Item 5
Item 6
Weight=109
Profit=159
85 | P a g e
Analysis And Design of Algorithms
#include<stdio.h>
#include<conio.h>
#include<math.h>
int x[25];
int c=0;
int p(int k,int i)
{
int j;
for(j=1;j<=k-1;j++)
{
if((x[j]==i) || (abs(x[j]-i)==abs(j-k)))
return(0);
}
return(1);
}
void nq(int k,int n)
{
int i,j;
for(i=1;i<=n;i++)
{
if(p(k,i))
{
x[k]=i;
if(k==n)
{
printf("\nSolution Vector:");
for(j=1;j<=n;j++)
printf("%d ",x[j]);
c++;
}
else
nq(k+1,n);
}
}
}
void main()
{
int n,i,j;
clrscr();
printf("Enter N:");
scanf("%d",&n);
nq(1,n);
printf("\n\nTotal Solution Vectors are %d",c);
getch();
}
Output
Enter N:4
Solution vector:2 4 1 3
Solution vector:3 1 4 2
86 | P a g e
Analysis And Design of Algorithms
29. Write a program to implement Naïve String Algorithm using String Matching
Algorithm.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int m,n,i,j,k;
char s[100];
n=strlen(t);
m=strlen(p);
for(i=0;i<=n-m;i++)
j=0;
while((j<m)&&(p[j]==t[i+j]))
j=j+1;
if(j==m)
void main()
char t[100],p[100];
clrscr();
gets(t);
87 | P a g e
Analysis And Design of Algorithms
gets(p);
naive(t,p);
getch();
Output
Enter the text:-Sunny and Pratik
88 | P a g e
Analysis And Design of Algorithms
30. Write a program to implement Rabin Karp using String Matching Algorithm.
//RabinKarpMatch(T,P,d,q)
//This algorithm matches whether P is a substring of T and returnthe starting index of the firstoccurence of
//P in T. Also n is length(T) and m islength(P).
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
//Macro Defined
#define tonum(c) (c >= 'A' && c <= 'Z' ? c - 'A' : c - 'a'+26) //converts alphabets to //numbers
int mod(inta,intp,int m)
intsqr;
if(p==0)
return(1);
sqr=mod(a,p/2,m)%m;
sqr=(sqr*sqr)%m;
if(p & 1)
return((a%m)*sqr)%m;
else
return(sqr);
int m,n,h,p=0,t=0,i,f,j;
n=strlen(T);
89 | P a g e
Analysis And Design of Algorithms
m=strlen(P);
h=mod(d,m-1,q);
for(i=0;i<m;i++)
p=(d*p+tonum(P[i]))%q;
t=(d*t+tonum(T[i]))%q;
for(i=0;i<=n-m;i++)
if(p==t)
f=1;
for(j=0;i<m;j++)
if(P[j]!=T[i+j])
f=0;
break;
if(f)
return(i);
else
if(i<n-m)
t=(d*(t-((tonum(T[i])*h)%q))+tonum(T[i+m]))%q;
return(-1);
void main()
90 | P a g e
Analysis And Design of Algorithms
char t[100],p[100];
intans,d,q;
clrscr();
gets(t);
gets(p);
scanf("%d",&d);
scanf("%d",&q);
ans=rabin_karp_matcher(t,p,d,q);
if(ans==-1)
else
getch();
Output
Enter the text:-3141592653589793
Found at pos. 7
91 | P a g e