Manual
Manual
S.NO EXPERIMENTS
NO
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
1. connected undirected graph using Kruskal's algorithm. 4
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
2. 6
connected undirected graph using Prim's algorithm.
a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
3. Floyd's algorithm. b. Design and implement C/C++ Program to find the transitive closure 9
using Warshal's algorithm.
Design and implement C/C++ Program to find shortest paths from a given vertex in a
4. 12
weighted connected graph to other vertices using Dijkstra's algorithm.
Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
5. 15
given digraph
Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
6. 17
Programming method.
Design and implement C/C++ Program to solve discrete Knapsack and continuous
7. 18
Knapsack problems using greedy approximation method.
Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn}
8. 20
of n positive integers whose sum is equal to a given positive integer d.
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
9. 21
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.
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
10 12
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.
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
11. of n> 5000, and record the time taken to sort. Plot a graph of the time taken versus n. 23
The elements can be read from a file or can be generated using the random number
generator.
12 Design and implement C/C++ Program for N Queen's problem using Backtracking. 24
Program:1
/* Program to implement Kruskal’s Algorithm */
#include<stdio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
printf("\n\t Implementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\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;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Input Graph:
Output 1:
Implementation of Kruskal's algorithm
Enter the no. of vertices: 4
Enter the cost adjacency matrix:
999 5 7 3
5 999 6 4
7 6 999 8
3 4 6 999
The edges of Minimum Cost Spanning Tree are
1 edge (1,4) =3
2 edge (2,4) =4
3 edge (2,3) =6
Minimum cost = 13
Program: 2 //*prims Algorithm*//
#include<stdio.h>
// #include<conio.h>
#define INF 999
void main()
{
int c[10][10],i,j,res,s,n;
clrscr();
printf("\nEnter n value:");
scanf("%d",&n);
res=prim(c,n,s);
printf("\nCost=%d",res);
getch();
}
Input/output:
Enter n value:3
Cost=7
Program 3.a: Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem
using Floyd's algorithm.
#include<stdio.h>
void readf();
void amin();
int cost[20][20],a[20][20];
int i,j,k,n;
void readf()
{
printf("\n Enter the number of vertices :");
scanf("%d",&n);
printf("\n Enter the weighted matrix - 999 for infinity:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0 && (i!=j))
cost[i][j]=999;
a[i][j]=cost[i][j];
}
}
}
void amin()
{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
printf("\n The All pair shortest path is:");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
}
void main()
{
readf();
amin();
}
Input:
OUTPUT:
Enter the number of vertices:
4
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
Program 3.b: Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm.
#include<stdio.h>
#include<math.h>
void warshal(int p[10][10], int n)
{
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] = p[i][j] || (p[i][k] && p[k][j]);
}
void main()
{
int p[10][10] = { 0 }, n, e, u, v, i, j;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
warshal(p, n);
Output:
int main()
{
int c[10][10],d[10],i,j,s,sum,n;
printf("\nEnter n value:");
scanf("%d",&n);
dijkstra(c,n,s,d);
for(i=1;i<=n;i++)
printf("\nShortest distance from %d to %d is %d",s,i,d[i]);
return 0;
}
Input/Output
Enter n value:6
#include<stdio.h>
int temp[10],k=0;
void sort(int a[][10],int id[],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
if(id[i]==0)
{
id[i]=-1;
temp[++k]=i;
for(j=1;j<=n;j++)
{
if(a[i][j]==1 && id[j]!=-1)
id[j]--;
}
i=0;
}
}
}
void main()
{
int a[10][10],id[10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
id[i]=0;
if(k!=n)
printf("\nTopological ordering not possible");
else
{
printf("\nTopological ordering is:");
for(i=1;i<=k;i++)
printf("%d ",temp[i]);
}
}
Input/output:
Enter the n value:6
Enter the graph data:
001100
000110
000101
000001
000001
000000
Topological ordering is:1 2 3 4 5 6
Program 6 Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
#include<stdio.h>
int w[10],p[10],n;
return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
}
int main()
{
int m,i,max_profit;
printf("\nEnter the no. of objects:");
scanf("%d",&n);
max_profit=knap(1,m);
printf("\nMax profit=%d",max_profit);
return 0;
}
Input/Output:
Enter the no. of objects:4
Max profit=170
Program 7 Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.
#include <stdio.h>
void main()
{
int cur_w,n;
float tot_v;
int p[10],w[10],W;
int i, maxi;
int used[10];
if ((used[i] == 0) &&
((maxi == -1) || ((float)w[i]/p[i] > (float)w[maxi]/p[maxi])))
maxi = i;
used[maxi] = 1;
cur_w -= p[maxi];
tot_v += w[maxi];
if (cur_w >= 0)
printf("Added object %d (%d, %d) completely in the bag. Space left: %d.\n", maxi + 1, w[maxi],
p[maxi], cur_w);
else
{
printf("Added %d%% (%d, %d) of object %d in the bag.\n", (int)((1 + (float)cur_w/p[maxi]) * 100),
w[maxi], p[maxi], maxi + 1);
tot_v -= w[maxi];
#include<stdio.h>
#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1;i<=k;i++)
if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
}
else
if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
return 0;
}
Input/output:
Enter the n value:9
Enter the set in increasing order:1 2 3 4 5 6 7 8 9
Enter the max subset value:9
1 2 6
1 3 5
1 8
2 3 4
2 7
3 6
4 5
9
Program 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.
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
void swap(long int*a,long int*b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}
void selectionsort (long int arr[],long int n)
{
long int i,j,midx;
for(i=0;i<n-1;i++)
{
midx=i;
for(j=i+1;j<n;j++)
if(arr[j]<arr[midx])
midx=j;
swap(&arr[midx],&arr[i]);
}
}
void main()
{
long int n=1000;
int it=0;
double tim1[10];
printf("Input Size, Selection Sorting time \n");
while(it++<5)
{
long int a[n];
for(int i=0;i<n;i++)
{
long int no=rand()%n+1;
a[i]=no;
}
//using clock t to store time
clock_t start,end;
start=clock();
selectionsort(a,n);
end=clock();
tim1[it]=(double)(end-start)/1000;
printf(" %ld = %ld ms\n",n,(long int)tim1[it]) n+=1000;
}
}
Output:
Input Size, Selection Sorting time
1000 = 1 ms
2000 = 5 ms
3000 = 13 ms
4000 = 23 ms
5000 = 35 ms
Program 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.
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
static int max= 5000;
static int partition(long int arr[],int low,int high)
{
int pivot = arr[low];
int i = low;
int j= high+1;
while(i<=j)
{
do
{
i++;
}while(pivot>=arr[i] && i<=high);
do
{
j--;
}
while(pivot<arr[j])
if(i<j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
return j;
}
static void qs(long int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid = partition(arr, low, high);
qs(arr,low,mid-1);
qs(arr,mid+1,high);
}
}
void main()
{
int n,i;
long int a[5000], no;
double tm;
//using clock t to store time
clock_t start,end;
printf("\n Enter the number of elements:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
no=rand()%n+1;
a[i]=no;
}
start=clock();
qs(a,0,n-1);
end=clock();
tm = (end - start);
printf(" %d = %lf\n Nano Seconds",n,tm);
}
Output:
Enter the number of elements:
1000
1000 = 128.000000
Nano Second
Program 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.
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#define max 5000
int array[max];
void merge(int low, int mid, int high)
{
int temp[max];
int i = low;
int j = mid +1;
int k = low ;
while((i <= mid) && (j <=high))
{
if(array[i] <= array[j])
temp[k++] = array[i++] ;
else
temp[k++] = array[j++] ;
}
while( i <= mid )
temp[k++]=array[i++];
while( j <= high )
temp[k++]=array[j++];
for(i= low; i <= high ; i++)
array[i]=temp[i];
}
void merge_sort(int low, int high)
{
int mid;
if( low != high )
{
mid = (low+high)/2;
merge_sort(low , mid);
merge_sort(mid+1, high);
merge(low, mid, high);
}
}
void main()
{
int i,n, no;
double tm;
clock_t start,end;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
no=rand()%n+1;
array[i]=no;
}
printf("Unsorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", array[i]);
start=clock();
merge_sort(0, n-1);
printf("\nSorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", array[i]);
printf("\n");
end=clock();
tm = (end - start);
printf(" %d = %lf Nano Seconds \n",n,tm);
printf("\n");
}/*End of main()*
Output
Enter the number of elements : 20
Unsorted list is :
4 7 18 16 14 16 7 13 10 2 3 8 11 20 4 7 1 7 13 17
Sorted list is :
1 2 3 4 4 7 7 7 7 8 10 11 13 13 14 16 16 17 18 20
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
void n_queens(int n)
{
int r;
int c[MAX];
c[0]=-1;
r=0;
while(r>=0)
{ c[r]++;
while(c[r]<n && !can_place(c,r))
c[r]++;
if(c[r]<n)
{ if(r==n-1)
{ display(c,n);
printf("\n\n");
}
else
{ r++;
c[r]=-1;
}
}
else
r--;
}
}
void main()
{
int n;
clrscr();
printf("\nEnter the no. of queens:");
scanf("%d",&n);
n_queens(n);
getch();
}
Input/Output:
Enter the no. of queens:4
-Q--
---Q
Q---
--Q-
--Q-
Q---
---Q
-Q--