You are on page 1of 47

[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

1
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

2
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

3
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

4
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

5
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

6
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

7
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

8
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

9
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

10
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

11
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

12
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

13
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

14
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

15
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

16
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

17
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

18
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

19
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical : 4
Aim : Implementation and Time analysis of factorial program using
iterative and recursive method.
#include<stdio.h>
#include<iostream>
#include<time.h>
#include<math.h>
#include<stdlib.h>
long double recursive(int x);
long double iterative(int x);
long int fact(int x);
using namespace std;
int main()
{
int b[12]={8,17,26,35,44,53,62,71,88,107,116,2222};
int i,j,k,n;
int a[100000];
cout<<"============================================================
====="<<endl;
cout<<"\t\t\tFactorial"<<endl;
cout<<"============================================================
====="<<endl;
cout<<"VALUES\t\tRECURSIVE METHOD\t\tITERATIVE METHOD"<<endl<<endl;
cout<<"============================================================
====="<<endl;
for(i=0;i<12;i++)
{
int x=b[i];
cout<<x<<"\t\t\t"<<recursive(x)<<"\t\t\t\t"<<iterative(x)<<endl;
}
cout<<"============================================================
====="<<endl;
}

long double recursive(int x)


{
clock_t t1,t2;
long int i;
t1=clock();
i=fact(x);
t2=clock();
return((long double)(t2-t1)/CLK_TCK);
}
long double iterative(int x)
{
clock_t t1,t2;
long int temp=1;

20
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

t1=clock();
for(int i=x;i>=1;i--)
{
temp=temp*i;
}
t2=clock();
return((long double)(t2-t1)/CLK_TCK);
}
long int fact(int x)
{
if(x==1)
return 1;
else
return(x*recursive(x-1));
}
Output: -

21
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical : 5
Aim : Implementation of a knapsack problem using dynamic problem.
#include<stdio.h>
#include<conio.h>
#define MAX 20
void knapsackDP(int,int);
int max(int,int);
void backtracking();
int weight[MAX],value[MAX],W,no,*x;
int v[MAX][MAX];
void main()
{
int i,j;
printf("\nEnter number of Objects :");
scanf("%d",&no);
printf("\nEnter Weight and Values in ascending order of Values");;
for(i=1;i<=no;i++)
{
printf("\nEnter Weight and Value for Object %d :",i);
scanf("%d %d",&weight[i],&value[i]);
}
printf("\nEnter knapsack Capacity :");
scanf("%d",&W);
knapsackDP(no,W);
backtracking();
}
void knapsackDP(int no,int W)
{
int i,j;
for(i=0;i<= W ;i++)
v[0][i]=0;
for(i=0;i<= no;i++)
v[i][0]=0;
for(i=1;i<= no;i++)
{
for(j=1;j<= W;j++)
{
if((j-weight[i])< 0)
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-weight[i]]+value[i]);
}
}
printf("\n \t ");
for(i=0;i<= W;i++)
printf("%2d ",i);
printf("\n-----------------------------------------------------------------------------------------------------
---------------------------");

22
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

for(i=0;i<=no;i++)
{
printf("\n w%d=%2d v%d=%2d |",i,weight[i],i,value[i]);
for(j=0;j<= W;j++)
printf("%2d ",v[i][j]);
}
printf("\n-----------------------------------------------------------------------------------------------------
---------------------------");
printf("\n Maximum value carry by knapsack is:%2d",v[no][W]);
printf("\n-----------------------------------------------------------------------------------------------------
---------------------------");
}
int max(int a,int b)
{
return (a >b)?a:b;
}
void backtracking()
{
int j1,i;
j1=W;
printf("\nIncluded Object \t weight \t value");
printf("\n--------------------------------------------------");
for(i=no;i >=0;i--)
{
if(v[i][j1]!=v[i-1][j1] && (v[i][j1]==v[i-1][j1-weight[i]]+value[i]))
{
printf("\n%2d \t\t\t %2d \t\t %2d",i,weight[i],value[i]);
j1=j1-weight[i];
}
}
}

23
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output:

24
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical: - 6
Aim: Implementation of chain matrix multiplication using dynamic
programming.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<limits.h>
using namespace std;
void printParenthesis(int i, int j, int n, int *bracket, char &name)
{
if (i == j)
{ cout << name++;
return;
}
cout << "(";
printParenthesis(i, *((bracket+i*n)+j), n, bracket, name);
printParenthesis(*((bracket+i*n)+j) + 1, j,n, bracket, name);
cout << ")";
}
void matrixChainOrder(int p[], int n)
{
int m[n][n];
for(int w=1;w<n;w++)
{
for(int q=1;q<n;q++)
m[w][q]=0;
cout<<endl;
}
int bracket[n][n];
for(int w=1;w<n;w++)
{
for(int q=1;q<n;q++)
bracket[w][q]=0;
cout<<endl;
}
for (int i=1; i<n; i++)
m[i][i] = 0;
for (int L=2; L<n; L++)
{
for (int i=1; i<n-L+1; i++)
{
int j = i+L-1;

25
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

m[i][j] = INT_MAX;
for (int k=i; k<=j-1; k++)
{
int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
{
m[i][j] = q;
bracket[i][j] = k;
}
}
}
}
cout<<"M matrix:\n";
for(int w=1;w<n;w++)
{
for(int q=1;q<n;q++)
{
cout<<m[w][q]<<"\t";
}
cout<<endl;
}
cout<<"\n\n";
cout<<"S matrix:\n";
for(int w=1;w<n;w++)
{
for(int q=1;q<n;q++)
{
cout<<bracket[w][q]<<"\t";
}
cout<<endl;
} cout<<"\n";
char name = 'A';
cout << "Optimal Parenthesization is : ";
printParenthesis(1, n-1, n,(int *)bracket, name);
cout<<"\n";
cout << "\nOptimal Cost is : " << m[1][n-1];
cout<<"\n\n";
}
int main()
{
int arr[] = {30,35,15,5,10};
matrixChainOrder(arr, 5);
return 0;
}

26
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output:

27
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical: - 7
Aim: Implementation of making a change problem using dynamic
programming.
#include<iostream>
using namespace std;
int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
int main()
{
int x,y;
int i,j;
cout<<"Enter no of coins you have:";
cin>>x;
int d[x];
cout<<"Enter value of coins\n";
for(i=0;i<x;i++)
cin>>d[i];
cout<<"Enter amount you have to pay:";
cin>>y;
int a[x][y+1];
for(i=0;i<x;i++)
a[i][0]=0;
for(i=0;i<x;i++)
{
for(j=1;j<=y;j++)
{
if(i==0 && j<d[i])
{
a[i][j]=-1;
}
else if(i==0)
{
a[i][j]=1+a[i][j-d[0]];
}
else if(d[i]>j)
{
a[i][j]=a[i-1][j];

28
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

}
else
{
a[i][j]=min(a[i-1][j],1+a[i][j-d[i]]);
}
}
}
cout<<" ";
for(i=0;i<=y;i++)
{
cout<<i<<" ";
}
cout<<"\n";
for(i=0;i<x;i++)
{
cout<<d[i]<<" ";
for(j=0;j<=y;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
int n=x-1;
int m=y;
int ans[y],t=0;
while(n!=0 && m!=0)
{
if(a[n][m]==a[n-1][m])
{
n=n-1;
}
else
{
ans[t]=d[n];
t++;
m=m-d[n];
}
}
cout<<endl<<"number of coins needed to pay "<<y<<" amount are:"<<t<<endl;
cout<<"Coins needed are:";
for(i=0;i<t;i++)
cout<<ans[i]<<" ";
return 0;
}

29
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output:

30
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical: 8
Aim: Implementation of a knapsack problem using greedy algorithm.
#include<stdio.h>
#include<time.h>
#include<conio.h>
void knapsack(float capacity, int n, float weight[], float profit[])
{
float x[20], totalprofit,y;
int i,j;
y=capacity;
totalprofit=0;
for(i=0;i < n;i++)
x[i]=0.0;
for(i=0;i < n;i++)
{
if(weight[i] > y)
break;
else
{
x[i]=1.0;
totalprofit=totalprofit+profit[i];
y=y-weight[i];
}
}
if(i < n)
x[i]=y/weight[i];
totalprofit=totalprofit+(x[i]*profit[i]);
printf("The selected elements are:-\n ");
for(i=0;i < n;i++)
if(x[i]==1.0)
printf("\nProfit is %f with weight %f ", profit[i], weight[i]);
else if(x[i] > 0.0)
printf("\n%f part of Profit %f with weight %f", x[i], profit[i], weight[i]);
printf("\nTotal profit for %d objects with capacity %f = %f\n\n", n, capacity,totalprofit);
}
int main()
{
float weight[20],profit[20],ratio[20], t1,t2,t3;
int n;
time_t start,stop;
float capacity;

31
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

int i,j;
printf("Enter number of objects: ");
scanf("%d", &n);
printf("\nEnter the capacity of knapsack: ");
scanf("%f", &capacity);
for(i=0;i < n;i++)
{
printf("\nEnter %d(th) profit: ", (i+1));
scanf("%f", &profit[i]);
printf("Enter %d(th) weight: ", (i+1));
scanf("%f", &weight[i]);
ratio[i]=profit[i]/weight[i];
}
start=time(NULL);
for(i=0;i < n;i++)
for(j=0;j < n;j++)
{
if(ratio[i] > ratio[j])
{
t1=ratio[i];
ratio[i]=ratio[j];
ratio[j]=t1;
t2=weight[i];
weight[i]=weight[j];
weight[j]=t2;
t3=profit[i];
profit[i]=profit[j];
profit[j]=t3;
}
}
knapsack(capacity,n,weight,profit);
stop=time(NULL);
printf("\nKnapsack = %f\n", difftime(stop,start));
getch();
}

32
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output:

33
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical: - 9
Aim: Implementation of Graph and Searching (DFS and BFS)
DFS

#include<stdio.h>

void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
int main()
{
int i,j;
printf("Enter number of vertexes:");

scanf("%d",&n);

//read the adjecency matrix


printf("\nEnter adjecency matrix of the graph:\n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);

//visited is initialized to zero


for(i=0;i<n;i++)
visited[i]=0;

DFS(0);
return 0;
}

void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;

for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
i.) BFS
#include<iostream>

34
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

#include<string.h>
using namespace std;

int main()
{
cout<<"Enter p and q..."<<endl;
char p[50],q[50];
cin>>p>>q;
int l1=strlen(p);
int l2=strlen(q);
int a[l1+1][l2+1];
char c[l1+1][l2+1];
for(int i=0;i<=l1;i++)
{
a[i][0]=0;
c[i][0]='n';
}

for(int i=0;i<=l2;i++)
{
a[0][i]=0;
c[0][i]='n';
}

for(int i=1;i<=l1;i++)
{
for(int j=1;j<=l2;j++)
{
if(p[i-1]==q[j-1])
{
a[i][j]=a[i-1][j-1]+1;
c[i][j]='y';
}
else
{
if(a[i][j-1]>a[i-1][j])
{
a[i][j]=a[i][j-1];
c[i][j]='l';
}

else
{
a[i][j]=a[i-1][j];

35
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

c[i][j]='u';
}
}
}
}
cout<<endl;

for(int i=0;i<=l1;i++)
{
for(int j=0;j<=l2;j++)
cout<<a[i][j]<<"("<<c[i][j]<<")"<<" ";
cout<<endl;
}

int i=l1,j=l2,z=0;
char temp[10];
while(i>0 && j>0)
{
if(c[i][j]=='y')
{
temp[z++]=p[i-1];
i--;
j--;
}
else
{
if(c[i][j]=='u')
i--;
else
j--;
}
}

cout<<endl<<"LCS is: ";


for(int i=z-1;i>=0;i--)
cout<<temp[i];
cout<<endl;
return 0;
}

36
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output:

37
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

BFS

#include<stdio.h>

int main()
{
int q[10],v[10],a[10][10],n;//n is no of vertices and graph is sorted in array a[10][10]
int i,t,j,f=0,r=0;

printf("\nEnter number of vertexes:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
v[i]=0;
q[i]=0;
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
q[r]=0;
r++;
v[0]=1;
t=q[f];
f++;
printf("\n\t%d",t);
while(1)
{
for(i=0;i<n;i++)
{
if(v[i]==0 && a[t][i]==1)
{
v[i]=1;
q[r]=i;
r++;
}
}
if(f>=r)
{
break;
}
else
{
t=q[f];
f++;

38
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

printf("\n\t%d",t);
}
}
return 0;
}

Output:

39
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical : - 10

Aim : Implementation of Prim’s algorithm.

#include<stdio.h>
#include<conio.h>
int n, cost[10][10];
void prim()
{
int i, j, startVertex, endVertex;
int k, nr[10], temp, minimumCost = 0, tree[10][3];
/* For first smallest edge */
temp = cost[0][0];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (temp > cost[i][j]) {
temp = cost[i][j];
startVertex = i;
endVertex = j;
}
}
}
/* Now we have fist smallest edge in graph */
tree[0][0] = startVertex;
tree[0][1] = endVertex;
tree[0][2] = temp;
minimumCost = temp;
/* Now we have to find min dis of each vertex from either
startVertex or endVertex by initialising nr[] array */

for (i = 0; i < n; i++) {


if (cost[i][startVertex] < cost[i][endVertex])
nr[i] = startVertex;
else
nr[i] = endVertex;
}
/* To indicate visited vertex initialise nr[] for them to 100 */
nr[startVertex] = 100;
nr[endVertex] = 100;
/* Now find out remaining n-2 edges */
temp = 99;
for (i = 1; i < n - 1; i++) {
for (j = 0; j < n; j++) {
if (nr[j] != 100 && cost[j][nr[j]] < temp) {
temp = cost[j][nr[j]];
k = j;
}
}

40
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

/* Now i have got next vertex */


tree[i][0] = k;
tree[i][1] = nr[k];
tree[i][2] = cost[k][nr[k]];
minimumCost = minimumCost + cost[k][nr[k]];
nr[k] = 100;
/* Now find if k is nearest to any vertex
than its previous near value */
for (j = 0; j < n; j++) {
if (nr[j] != 100 && cost[j][nr[j]] > cost[j][k])
nr[j] = k;
}
temp = 99;
}
/* Now i have the answer, just going to print it */
printf("\nThe min spanning tree is:-\n ");
printf("\n");
for (i = 0; i < n - 1; i++) {
for (j = 0; j < 3; j++)
printf("%d\t", tree[i][j]);
printf("\n");
}

printf("\nMin cost :\n %d", minimumCost);


}

int main() {
int i, j;

printf("\nEnter the no. of vertices :");


scanf("%d", &n);

printf("\nEnter the costs of edges in matrix form :");


for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
scanf("%d", &cost[i][j]);
}

printf("\nThe matrix is :\n ");


printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", cost[i][j]);
}
printf("\n");
}
prim();
getch();
}

41
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Output :

42
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical :-11
Aim : Implementation of Kruskal’s algorithm.
#include<stdio.h>
#define MAX 30
typedef struct edge
{
int u,v,w;
}edge;
typedef struct edgelist
{
edge data[MAX];
int n;
}edgelist;
edgelist elist;
int G[MAX][MAX],n;
edgelist spanlist;
void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();
void main()
{
int i,j,total_cost;
printf("\nEnter number of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
kruskal();
print();
}
void kruskal()
{
int belongs[MAX],i,j,cno1,cno2;
elist.n=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(G[i][j]!=0)
{

43
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

elist.data[elist.n].u=i;
elist.data[elist.n].v=j;
elist.data[elist.n].w=G[i][j];
elist.n++;
}
}
sort();
for(i=0;i<n;i++)
belongs[i]=i;
spanlist.n=0;
for(i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u);
cno2=find(belongs,elist.data[i].v);
if(cno1!=cno2)
{
spanlist.data[spanlist.n]=elist.data[i];
spanlist.n=spanlist.n+1;
union1(belongs,cno1,cno2);
}
}
}
int find(int belongs[],int vertexno)
{
return(belongs[vertexno]);
}
void union1(int belongs[],int c1,int c2)
{
int i;
for(i=0;i<n;i++)
if(belongs[i]==c2)
belongs[i]=c1;
}
void sort()
{
int i,j;
edge temp;
for(i=1;i<elist.n;i++)
for(j=0;j<elist.n-1;j++)
if(elist.data[j].w>elist.data[j+1].w)
{
temp=elist.data[j];
elist.data[j]=elist.data[j+1];
elist.data[j+1]=temp;

44
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

}
}
void print()
{
int i,cost=0;
for(i=0;i<spanlist.n;i++)
{
printf("\n%d\t%d\t%d",spanlist.data[i].u,spanlist.data[i].v,spanlist.data[i].w);
cost=cost+spanlist.data[i].w;
}
printf("\n\nCost of the spanning tree=%d",cost);
}

Output :

45
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

Practical :-12
Aim : Implementation of LCS problem.
#include<stdio.h>
#include<string.h>

int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];

void print(int i,int j)


{
if(i==0 || j==0)
return;
if(b[i][j]=='c')
{
print(i-1,j-1);
printf("%c",x[i-1]);
}
else if(b[i][j]=='u')
print(i-1,j);
else
print(i,j-1);
}

void lcs()
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;

//c, u and l denotes cross, upward and downward directions respectively


for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='c';
}
else if(c[i-1][j]>=c[i][j-1])

46
150110107034
[AN ALYSI S A ND DESI G N O F A LGO RI TH M S ] 2150703

{
c[i][j]=c[i-1][j];
b[i][j]='u';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='l';
}
}
}

int main()
{
printf("Enter 1st sequence:");
scanf("%s",x);
printf("Enter 2nd sequence:");
scanf("%s",y);
printf("\nThe Longest Common Subsequence is ");
lcs();
print(m,n);
return 0;
}

Output :

47
150110107034

You might also like