analysis and design algorithm Manual
analysis and design algorithm Manual
1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
Code:
#include<stdio.h>
#include<conio.h>
int parent[10];
int main()
int mincost=0,cost[10][10],min,ne,a,b,n,i,j,u,v;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
ne=1;
while(ne<n)
for(min=999,i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
min=cost[i][j];
a=u=i;
b=v=j;
while(parent[u]) u=parent[u];
while(parent[v]) v=parent[v];
if(v!=u)
printf("%d Edge(%d,%d)=%d\n",ne++,a,b,min);
mincost +=min;
parent[v]=u;
cost[a][b]=cost[b][a]=999;
getch();
Output:
0 1 7 11
1093
7905
11 3 5 0
1 Edge(1,2)=1
2 Edge(2,4)=3
3 Edge(3,4)=5
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
Code:
#include<stdio.h>
#include<conio.h>
int main()
int mincost=0,cost[10][10],visited[10],min,ne,a,b,n,i,j,u,v;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
visited[1]=1;
for(i=2;i<=n;i++)
visited[i]=0;
ne=1;
while(ne<n)
for(min=999,i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]==0) continue;
else {
min=cost[i][j];
a=u=i;
b=v=j;
printf("%d Edge(%d,%d)=%d\n",ne++,a,b,min);
mincost += min;
visited[v]=1;
cost[a][b]=cost[b][a]=999;
getch();
Output:
0219
2 0 12 18
1 12 0 5
9850
1 Edge(1,3)=1
2 Edge(1,2)=2
3 Edge(3,4)=5
3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
Code 3a:
#include<stdio.h>
if(i<j) return i;
else return j;
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
int main()
int i,j,n,a[10][10];
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(n,a);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");}
return 0;
Output:
0 15 8 10 999
15 0 4 999 999
8 4 0 999 12
10 999 999 0 7
999 999 12 7 0
0 12 8 10 17
12 0 4 22 16
8 4 0 18 12
10 22 18 0 7
17 16 12 7 0
Code 3b:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,cost[10][10],n;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
warshall(cost,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("%d\t",cost[i][j]);
printf("\n");
getch();
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
c[i][j]=(c[i][j]||c[i][k]&&c[k][j]);
Output:
0100
0001
0000
1010
1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
4. Design and implement C/C++ Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra's algorithm.
Code:
#include<stdio.h>
#include<conio.h>
int i,u,w,flag[10],min,count;
for(i=1;i<=n;i++)
flag[i]=0;
dist[i]=cost[v][i];
flag[v]=1;
dist[v]=1;
count=2;
while(count<n)
for(i=1,min=999;i<=n;i++)
min=dist[i];
v=i;
flag[v]=i;
count++;
for(w=1;w<=n;w++)
if(dist[v]+cost[v][w]<dist[w]&&!flag[w])
dist[w]=dist[v]+cost[v][w];
void main()
int i,j,source,n,dist[10],cost[10][10];
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
scanf("%d",&source);
dijkstras(cost,dist,n,source);
for(i=1;i<=n;i++)
if(source!=i)
printf("%d-->%d::%d\n",source,i,dist[i]);
getch();
Output:
0200
0035
0000
4030
4-->1::4
4-->2::6
4-->3::3
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.
Code:
#include<stdio.h>
#include<conio.h>
int temp[10],k=0;
int i,j;
for(i=1;i<=n;i++)
if(indegree[i]==0)
indegree[i]=-1;
temp[++k]=i;
for(j=1;j<=n;j++)
indegree[j]--;
} i=0;
void main()
int i,j,n,a[10][10],indegree[10];
scanf("%d",&n);
for(i=1;i<=n;i++)
indegree[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
if(a[i][j]==1)
indegree[j]++;
topo(n,indegree,a);
if(k!=n)
else
for(i=1;i<=k;i++)
printf("%d\t",temp[i]);
getch();
Output:
00100
00100
00011
00000
00000
1 2 3 4 5
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
Code:
#include<stdio.h>
#include<conio.h>
if(a>b) return a;
else return b;
void main()
int i,j,n,capacity;
int w,wt[20],ve[20],v[20][20];
scanf("%d",&n);
printf("\n-----\nWeight values\n-----\n");
for(i=1;i<=n;i++)
scanf("%d",&wt[i]);
scanf("%d",&ve[i]);
scanf("%d",&capacity);
for(i=0;i<=n;i++)
for(j=0;j<=capacity;j++)
if(i==0 || j==0)
v[i][j]=0;
else if(j-wt[i]>=0)
v[i][j]=max(v[i-1][j],v[i-1][j-wt[i]]+ve[i]);
else
v[i][j]=v[i-1][j];
printf("%4d",v[i][j]);
printf("\n");
w=capacity;
for(i=n;i>0;i--)
if(v[i][w]==v[i-1][w]) continue;
else
w=w-wt[i];
printf("%3d",wt[i]);
getch();
Output:
-----
Weight values
-----
10
20
0 0 0 0
0 0 10 10
0 0 10 20
Total profit: 20
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.
Code:
#include<stdio.h>
int main()
float weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f %f",&weight[i],&profit[i]);
scanf("%f",&capacity);
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(ratio[i]<ratio[j])
temp=ratio[i];
ratio[j]=ratio[i];
ratio[i]=temp;
temp=weight[j];
weight[j]=weight[i];
weight[i]=temp;
temp=profit[j];
profit[j]=profit[i];
profit[i]=temp;
for(i=0;i<n;i++)
if(weight[i]>capacity)
break;
else
Totalvalue=Totalvalue+profit[i];
capacity=capacity-weight[i];
if(i<n)
Totalvalue=Totalvalue + (ratio[i]*capacity);
return 0;
Output:
12
10
20
15
8. Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn}
of n positive integers whose sum is equal to a given positive integer d.
Code:
#include<stdio.h>
#include<conio.h>
#define max 10
int s[max],x[max],d;
int i;
x[k]=1;
if(p+s[k]==d)
for(i=1;i<=k;i++)
if(x[i]==1)
printf("%d\t",s[i]);
printf("\n");
else if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if((p+r-s[k]>=d)&&(p+s[k+1]<=d))
x[k]=0;
sumofsub(p,k+1,r-s[k]);
void main()
int i,n,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
scanf("%d",&d);
for(i=1;i<=n;i++)
sum=sum+s[i];
if(sum<d||s[1]>d)
else
sumofsub(0,1,sum);
getch();
Output:
12568
1 2 6
1 8
12568
No possible subset
9. Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied
values of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus
n. The elements can be read from a file or can be generated using the random number
generator.
Code:
#include <stdio.h>
*a = *b;
*b = temp;
min_idx = i;
swap(&array[min_idx], &array[step]);
printf("\n");
int main() {
selectionSort(data, size);
printArray(data, size);
Output:
2 10 12 15 20
10. Design and implement C/C++ Program to sort a given set of n integer elements using
Quick Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number
generator.
Code:
#include<stdio.h>
#include<conio.h>
#include<time.h>
*x = *y;
*y = temp;
int i,j;
int p;
p=a[l];
i=l+1;
j=r;
while(i<=j)
while(a[i]<=p)
i++;
while(a[j]>=p)
j++;
swap(&a[i],&a[j]);
swap(&a[i],&a[j]);
swap(&a[l],&a[j]);
return j;
int s;
if(l<r)
s=partition(a,l,r);
qsort(a,l,s-1);
qsort(a,s+1,r);
void main()
int a[8000],i,j,n;
clock_t begin,end;
printf("\nQuick Sort");
printf("\n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
for(i=0;i<n;i++)
printf("\t%d",a[i]=rand()%100);
begin=clock();
qsort(a,1,n-1);
end=clock();
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n\n");
getch();
Output:
Quick Sort
Elements are: 24 78 58 62 64
11. Design and implement C/C++ Program to sort a given set of n integer elements using
Merge Sort method and compute its time complexity. Run the program for varied values
of n> 5000, and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number
generator.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<omp.h>
int b[100],i,j,k;
i=low;
j=mid+1;
k=low;
if(a[i]<a[j])
b[k]=a[i];
k++;
i++;
else
b[k]=a[j];
k++;
j++;
while(i<=mid)
b[k]=a[i];
k++;
i++;
while(j<=high)
b[k]=a[j];
k++;
j++;
for(i=low;i<=high;i++)
a[i]=b[i];
int mid;
if(low<high)
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
void main()
int a[100],i,n;
clock_t starttime,endtime;
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=rand()%100;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
starttime=clock();
mergesort(a,0,n-1);
endtime=clock();
for(i=0;i<n;i++)
printf("%d\t",a[i]);
Output:
41 67 34 0 69
0 34 41 67 69
12. Design and implement C/C++ Program for N Queen's problem using Backtracking.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char s[30][30],count=0;
int i,j;
printf("\n_____%d_____\n",++count);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
s[i][j]='X';
for(i=0;i<n;i++)
s[i][m[i]]='Q';
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%2c",s[i][j]);
printf("\n\n");
printf("\n\n\n");
getch();
int i;
for(i=0;i<k;i++)
if(m[k]==m[i]||(abs(m[i]-m[k])==abs(i-k)))
return 0;
return 1;
int main()
int n,k=0,i;
int m[30];
scanf("%d",&n);
if(n==2||n==3)
printf("No solutions\n");
exit(0);
n--;
for(m[0]=0;k>=0;m[k]++)
while(m[k]<=n&&!place(m,k))
m[k]++;
if(m[k]<=n)
if(k==n)
display(m,n+1);
else k++,m[k]=-1;
else
k--;
Output:
_____1_____
XQXX
XXXQ
QXXX
XXQX
_____2_____
XXQX
QXXX
XXXQ
XQXX