Laboratory Manual For
Laboratory Manual For
Laboratory Manual
for
ANALYSIS AND DESIGN OF ALGORITHMS LABORATORY
(CS4L01)
Course Outcomes
1. Implement the basic techniques of analyzing the algorithms using space and time complexity,
asymptotic notations..
2. Implement divide and conquer method and analyze the different algorithms like merge sort,
quick sort etc.
3. Apply the greedy strategy to design different algorithms like 0 /1 knapsack Problem, closest pair
of points etc.
4. Implement the dynamic programming method to solve the different problems like All pair
Shortest Path Problem, Non Crossing subset of Nets etc..
5. Implement the Backtracking method to solve many classic problems like N-Queens Problem,
DFS,BFS etc.
Evaluation Pattern
Evaluation Marks
Continuous Internal Evaluation 25
SET 25
Total 50
CO1 Implement Recursive Linear search and determine the time taken to search an
1. element. Repeat the experiment for different values of n.
CO1 Sort a given set of elements using Rank sort and Repeat the experiment for different
2. values of n, the number of elements in the list to be sorted
CO1 Sort a given set of elements using Insertion sort method and Repeat the experiment
3. for different values of n, the number of elements in the list to be sorted
CO1 Sort a given set of elements using Selection sort and hence find the time required to
sort elements. Repeat the experiment for different values of n, the number of
4.
elements in the list to be sorted and plot a graph of the time taken versus n.
CO2 Implement Recursive Binary search determine the time taken to search an element.
5. Repeat the experiment for different values of n.
CO2 Sort a given set of elements using Merge sort method and determine the time taken
6.
to sort the elements. Repeat the experiment for different values of n.
CO2 Sort a given set of elements using Quick sort method. Repeat the experiment for
7.
different values of n.
CO3 Obtain the Topological ordering of vertices in a given digraph.
8.
CO3 From a given vertex in a weighted connected graph, find shortest paths to other
9.
vertices using Dijkstra's algorithm.
CO3 Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
10. algorithm
CO3 Find Minimum Cost Spanning Tree of a given undirected graph using Prims
11.
algorithm.
12. CO4 Implement All Pair Shortest paths problem using Floyd’s algorithm.
CO4 Implement 0/1 Knapsack problem using dynamic programming.
13.
CO4 Find the Binomial Co-efficient using Dynamic Programming.
14.
CO5 Print all the nodes reachable from a given starting node in a digraph using Breadth
15.
First Search method.
CO5 Check whether a given graph is connected or not using DFS method.
16.
17. CO5 Implement N Queen's problem using Back Tracking
List of Programs
2. Sort a given set of elements using Rank sort and Repeat the experiment for different values of
n, the number of elements in the list to be sorted
#include<stdio.h>
int C=0;
int u[20];
for (int i=0;i<n;i++)
u[r[i]]=a[i];
for(int i =0;i<n;i++)
a[i]=u[i];
int main( )
{
int a[100], n, i, r[100];
Rank( a, n, r);
Rearrange(a, n, r);
printf(“The array elements after sorting are\n”);
Dept of CS&E, NIE Page 5
Analysis and Design of Algorithms Laboratory CS4L01
for (i=0; i<n; i++)
printf(“%d\n”, a[i]);
return 0;
}
Output:
Pass-1
Enter the value of n
5
Enter 5 integers
45
35
10
25
15
Time complexity = 10
The array elements after sorting are
10
15
25
35
45
3. Sort a given set of elements using Insertion sort method and Repeat the experiment for
different values of n, the number of elements in the list to be sorted
#include<stdio.h>
int main()
{
int a[20],i,n;
printf(" Enter number of elements in array : ");
scanf("%d",&n);
// Inputting elements
printf("Enter %d numbers: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
key=a[i];
j=i-1;
C++;
// Finding appropriate position to insert key
while((key<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
C++;
}
// Inserting key
a[j+1]=key;
}
printf(“Time Complexity:%d\n”,C);
}
Output: Pass-2
Pass-1
Enter number of elements in array : Enter number of elements in array :
6 6
Enter 6 numbers: Enter 6 numbers:
123456 10 9 7 8 6 5
Items in the array are : Items in the array are :
12345 6 10 9 7 8 5 6
Time Complexity: 6 Time Complexity:
Elements after insertion sort : Elements after insertion sort :
123456 5 6 7 8 9 10
#include<stdio.h>
int main()
{
int n, i, j, temp, min;
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++)
{
Output:
4 (Number of elements)
33 76 21 2
2 21 33 76
#include <stdio.h>
// A recursive binary search function. It returns location of x in
// given array arr[l..r] is present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
// If the element is present at the middle itself
if (arr[mid] == x) return mid;
// If element is smaller than mid, then it can only be present
// in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
// Else the element can only be present in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not present in array
return -1;
}
int main(void)
{
int arr[], n, x,i;
printf (“enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the %d integers in ascending order\n”,n);
for( i=0;i<n;i++)
scanf(“%d”, &arr[i])
printf(“Enter the key\n”);
scanf(“%d”,&key);
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Output:
Pass-1: Pass-2
enter the number of elements enter the number of elements
Dept of CS&E, NIE Page 10
Analysis and Design of Algorithms Laboratory CS4L01
5 5
Enter the %d integers in ascending order Enter the %d integers in ascending order
10 10
20 20
30 30
40 40
50 50
Enter the key Enter the key
40 7
Element is present at index 3 Element is not present in array
6. Sort a given set of elements using Merge sort method and determine the time taken to sort the
elements. Repeat the experiment for different values of n.
#include<stdio.h>
// Inputting elements
for(i=0;i<n;i++)
{
printf("Enter number %d: ",i+1);
scanf("%d",&a[i]);
}
else
{
mid=(i+j)/2;
count++;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,j);
}
}
int i=0;
int j=0;
int k=0;
for(i=0;i<n1;i++)
{
left[i]=a[p+i];
}
for(i=0;i<n2;i++)
{
right[i]=a[q+i+1];
}
left[n1]=9999;
right[n2]=9999;
Dept of CS&E, NIE Page 12
Analysis and Design of Algorithms Laboratory CS4L01
i=0;
j=0;
for(k=p;k<=r;k++)
{
if(left[i]<right[j])
{
a[k]=left[i];
i++;
}
else
{
a[k]=right[j];
j++;
}
}
}
Output:
Enter number of elements in array :
5
25
34
15
63
45
Elements after merge sort :
15 25 34 45 63
Time complexity: 4
7. Sort a given set of elements using Quick sort method. Repeat the experiment for different values
of n.
#include<stdio.h>
int main()
{
int a[20],i,n;
printf("Enter number of elements in array : ");
Dept of CS&E, NIE Page 13
Analysis and Design of Algorithms Laboratory CS4L01
scanf("%d",&n);
// Inputting elements
printf("Enter %d number: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return (i+1);
}
Output : Output :
Pass-1: Pass-2:
Enter number of elements in array : Enter number of elements in array :
5 6
Enter 5 numbers Enter 5 numbers
12345 30 10 40 60 20 50
Elements after quick sort : Elements after quick sort :
12345 10 20 30 40 50
Time complexity: Count= 15 Time complexity: Count=
#include <stdio.h>
int main(){
int i, j, k, n, a[10][10], indeg[10], flag[10], count=0;
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(i=0;i<n;i++){
printf("Enter row %d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
Dept of CS&E, NIE Page 15
Analysis and Design of Algorithms Laboratory CS4L01
}
for(i=0;i<n;i++){
indeg[i]=0;
flag[i]=0;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];
printf("\nThe topological order is:");
while(count<n){
for(k=0;k<n;k++){
if((indeg[k]==0) && (flag[k]==0)){
printf("%d ",(k+1));
flag [k]=1;
}
for(i=0;i<n;i++){
if(a[i][k]==1)
indeg[k]--;
}
}
count++;
}
return 0;
}
Output
Enter the no of vertices:
4
Enter the adjacency matrix:
Enter row 1
0110
Enter row 2
0001
Enter row 3
0001
Enter row 4
0000
The topological order is:1 2 3 4
9. From a given vertex in a weighted connected graph, find shortest paths to other vertices using
Dijkstra's algorithm.
#include<stdio.h>
main ()
{
int n, cost[15][15], i, j, s[15], v, u, w, dist[15],
num, min;
clrscr();
printf ("Enter the vertices please\n");
scanf ("%d", &n);
printf ("Enter the cost of the edges please\n");
printf ("Enter 999 if the edgeis not present or for the
self loop\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf ("%d", &cost[i][j]);
printf ("Enter the Source vertex please\n");
scanf ("%d", &v);
s[v] = 1;
dist[v] = 0;
s[u] = 1;
printf ("VERTEX\tDESTINATION\tCOST\n");
for (i = 1; i <= n; i++)
printf (" %d\t %d\t\t %d\n", v, i, dist[i]);
getch();
}
OUTPUT
Enter the vertices please
n=5
10. Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm
#include<stdio.h>
#include<conio.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()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
Output:
Implementation of Kruskal's algorithm
Enter the no. of vertices:
4
Minimum Cost= 6
11. Find Minimum Cost Spanning Tree of a given undirected graph using Prims algorithm.
#include<stdio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
int main()
clrscr();
scanf("%d",&n);
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
visited[1]=1;
printf("\n");
while(ne < n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0)
mincost+=min;
cost[a][b]=cost[b][a]=999;
return 0;
}
Output:
Minimum Cost= 6
12. Implement All Pair Shortest paths problem using Floyd’s algorithm.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],a[10][10];
void all_paths(int [10][10],int [10][10],int);
int min1(int,int);
void main()
{
int i,j,n;
clrscr();
printf("\n enter the number of vertices\n");
scanf("%d",&n);
printf("\n enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
#include<stdio.h>
#include<conio.h>
int v[20][20];
int max1(int a,int b)
{
return(a>b)?a:b;
}
void main()
{
int i,j,p[20],w[20],n,max;
clrscr();
printf("\n enter the number of items\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the weight and profit of the
item %d:",i);
scanf("%d %d",&w[i],&p[i]);
}
printf("\n enter the capacity of the knapsack");
scanf("%d",&max);
for(i=0;i<=n;i++)
v[i][0]=0;
for(j=0;j<=max;j++)
v[0][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=max;j++)
{
if(w[i]>j)
v[i][j]=v[i-1][j];
else
v[i][j]=max1(v[i-1][j],v[i-1][j-w[i]]+p[i]);
}
printf("\n\nThe table is\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=max;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("\nThe maximum profit is %d",v[n][max]);
printf("\nThe most valuable subset is:{");
j=max;
for(i=n;i>=1;i--)
if(v[i][j]!=v[i-1][j])
{
printf("\t item %d:",i);
j=j-w[i];
}
printf("}");
getch();
}
OUTPUT:
enter the number of items
4
The table is
0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n,c[50][50];
clrscr();
printf("\n enter the value of n & k\n");
scanf("%d%d",&n,&k);
for(i=0;i<=n;i++)
Dept of CS&E, NIE Page 25
Analysis and Design of Algorithms Laboratory CS4L01
for(j=0;j<=k;j++)
c[i][j]=0;
for(i=0;i<=n;i++)
{
c[i][0]=1;
c[i][i]=1;
}
for(i=2;i<=n;i++)
for(j=1;j<=i-1;j++)
c[i][j]=c[i-1][j-1]+c[i-1][j];
printf("\n The table for valuation is\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=k;j++)
if(c[i][j]!=0)
printf("\t%d",c[i][j]);
printf("\n");
}
printf("\n\t The binomial coefficient of C(%d,%d) is %d\n",n,k,c[n][k]);
getch();
}
OUTPUT:
enter the value of n & k
6 3
#include<stdio.h>
int s[20], source;
void BFS( int A[20][20], int n)
{
int f, r, q[20], u, v, i;
for(i=0; i<n; i++)
s[i]=0;
f = r = 0;
q[20]=source;
s[source]=1;
while( f<=r)
{
u=q[f++];
for(v=1;v<=n;v++)
{
if (A[u][v] ==1 && s[v]==0)
{
s[v]=1;
q[++r]=v;
}
}
}
}
int main( )
{
int n, A[20[20], i, j;
printf(“ Enter the number of nodes\n”);
scanf(“%d”,&n);
printf(“ Enter the Adjacency Matrix\n”);
for(i =0;i<n; i++)
{
for( j=0; j<n; j++)
scanf(“%d”,&A[i][j]);
}
for( i=0;i<n;i++)
Dept of CS&E, NIE Page 27
Analysis and Design of Algorithms Laboratory CS4L01
{
if(s[i]==0)
printf(“ Node %d is not reachable\n”,i);
else
printf(“Node %d is reachable\n”,i);
}
return 0;
}
Output:
Enter the number of nodes:
4
16. Check whether a given graph is connected or not using DFS method.
#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]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
DFS(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);
}
Output: Pass 2:
Pass 1:
Enter number of vertices: Enter number of vertices:
4 4
enter the adjacency matrix enter the adjacency matrix
0001 1111
0000 1111
0010 1111
0001 1111
Graph is not connected Graph is connected
#include<stdio.h>
int a[30],count=0;
int place(int pos) {
Output: