Design and Analysis of Algorithms Lab
SESSION 2023-24
SUBMITTED TO: SUBMITTED BY:- Satveer Singh
Mr.Rishi Raj Vyas BRANCH:- COMPUTER SCIENCE
SEMESTER:- 6TH sem
ROLL NO. =21EEBCS096
ENGINEERING COLLEGE BIKANER
List of Experiments
1. Sort a given set of elements using the Quicksort method and
determine the time required to sort the elements. Repeat the
experiment for different values of n, the number of elements in
the list to be sorted and 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.
2. Implement a parallelized Merge Sort algorithm to sort a given set
of elements and determine the time required to sort the elements.
Repeat the experiment for different values of n,
the number of elements in the list to be sorted and 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.
3. a. Obtain the Topological ordering of vertices in a given digraph.
b. Compute the transitive closure of a given directed graph
using Warshall's algorithm.
4. Implement 0/1 Knapsack problem using Dynamic Programming.
5. From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
6. Find Minimum Cost Spanning Tree of a given undirected graph
using Kruskal's algorithm.
7. a. Print all the nodes reachable from a given starting node in a
digraph using BFS method.
b. Check whether a given graph is connected or not using DFS
method.
8.Find Minimum Cost Spanning Tree of a given undirected graph
using Prim’s algorithm.
Experiment 1
Sort a given set of elements using the Quicksort method and determine the
time required to sort the elements. Repeat the experiment for di erent values
of n, the number of elements in the list to be sorted and plot a graph of the
time taken versus n. The elements can be read from a le or can be
generated using the random number generator.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <time.h>
void Exch(int *p, int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
void QuickSort(int a[], int low, int high)
{
int i, j, key, k;
if (low >= high)
return;
key = low;
i = low + 1;
j = high;
while (i <= j)
{
while (a[i] <= a[key])
i = i + 1;
while (a[j] > a[key])
j = j - 1;
if (i < j)
Exch(&a[i], &a[j]);
}
Exch(&a[j], &a[key]);
QuickSort(a, low, j - 1);
QuickSort(a, j + 1, high);
}
void main()
{
int n, a[1000], i;
fi
ff
clock_t st, et;
double ts;
printf("\n Enter How many Numbers: ");
scanf("%d", &n);
printf("\nEnter array elements\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
st = clock();
QuickSort(a, 0, n - 1);
et = clock();
ts = (double)(et - st) / CLOCKS_PER_SEC;
printf("\nSorted Numbers are: \n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
printf("\nThe time taken is %e", ts);
}
Output :
Experiment 2
Implement a parallelized Merge Sort algorithm to sort a given set of elements and
determine the time required to sort the elements. Repeat the experiment for different
values of n, the number of elements in the list to be sorted and 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.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
void Merge(int a[], int low, int mid, int high)
int i, j, k, b[20];
i = low;
j = mid + 1;
k = low;
while (i <= mid && j <= high)
if (a[i] <= a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
while (i <= mid)
b[k++] = a[i++];
while (j <= high)
b[k++] = a[j++];
for (k = low; k <= high; k++)
a[k] = b[k];
void MergeSort(int a[], int low, int high)
int mid;
if (low >= high)
return;
mid = (low + high) / 2;
MergeSort(a, low, mid);
MergeSort(a, mid + 1, high);
Merge(a, low, mid, high);
void main()
int n, a[2000], i;
clock_t st, et;
double ts;
printf("\n Enter How many Numbers:");
scanf("%d", &n);
printf("\nEnter array elements\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
st = clock();
MergeSort(a, 0, n - 1);
et = clock();
ts = (double)(et - st) / CLOCKS_PER_SEC;
printf("\n Sorted Numbers are : \n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
printf("\nThe time taken is %e", ts);
OUTPUT:
Experiment 3
a. Obtain the Topological ordering of vertices in a given digraph.
b. Compute the transitive closure of a given directed graph using Warshall's
algorithm.
PROGRAMS:
(a)
#include <stdio.h>
#include<conio.h>
void ndIndegree(int[10][10], int[10], int);
void topological(int, int[10][10]);
void main()
int a[10][10], i, j, n;
printf("\nEnter the number of nodes:");
scanf("%d", &n);
printf("\nEnter the adjacency matrix\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("\nThe adjacency matrix is:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
fi
printf("%d\t", a[i][j]);
printf("\n");
topological(n, a);
getch();
void ndIndegree(int a[10][10], int indegree[10], int n)
int i, j, sum;
for (j = 1; j <= n; j++)
sum = 0;
for (i = 1; i <= n; i++)
sum = sum + a[i][j];
indegree[j] = sum;
void topological(int n, int a[10][10])
int k, top, t[100], i, stack[20], u, v, indegree[20];
k = 1;
top = -1;
ndIndegree(a, indegree, n);
fi
fi
for (i = 1; i <= n; i++)
if (indegree[i] == 0)
stack[++top] = i;
while (top != -1)
u = stack[top--];
t[k++] = u;
for (v = 1; v <= n; v++)
if (a[u][v] == 1)
indegree[v]--;
if (indegree[v] == 0)
stack[++top] = v;
printf("\nTopological sequence is\n");
for (i = 1; i <= n; i++)
printf("%d\t", t[i]);
}
OUTPUT:
(B)
#include <stdio.h>
#include <conio.h>
void warshall(int[10][10], int);
void main()
int a[10][10], i, j, n;
printf("\nEnter the number of nodes:");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("The adjacency matrix is:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
printf("%d\t", a[i][j]);
printf("\n");
warshall(a, n);
}
void warshall(int p[10][10], int n)
int i, j, k;
for (k = 1; k <= n; k++)
for (j = 1; j <= n; j++)
for (i = 1; i <= n; i++)
if ((p[i][j] == 0) && (p[i][k] == 1) && (p[k][j] == 1))
p[i][j] = 1;
printf("\nThe path matrix is:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
printf("%d\t", p[i][j]);
printf("\n");
}
}
OUTPUT:
Experiment 4
Implement 0/1 Knapsack problem using Dynamic Programming.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#de ne MAX 50
int p[MAX], w[MAX], n;
int knapsack(int, int);
int max(int, int);
void main()
int m, i, optsoln;
printf("\nEnter no. of objects: ");
scanf("%d", &n);
printf("\nEnter the weights:\n");
for (i = 1; i <= n; i++)
scanf("%d", &w[i]);
printf("\nEnter the pro ts:\n");
for (i = 1; i <= n; i++)
scanf("%d", &p[i]);
printf("\nEnter the knapsack capacity:");
scanf("%d", &m);
optsoln = knapsack(1, m);
printf("\nThe optimal solution is:%d", optsoln);
fi
fi
getch();
int knapsack(int i, int m)
if (i == n)
return (w[n] > m) ? 0 : p[n];
if (w[i] > m)
return knapsack(i + 1, m);
return max(knapsack(i + 1, m), knapsack(i + 1, m - w[i]) + p[i]);
int max(int a, int b)
if (a > b)
return a;
else
return b;
OUTPUT:
Experiment 5
From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void dij(int, int[20][20], int[20], int[20], int);
void main()
int i, j, n, visited[20], source, cost[20][20], d[20];
printf("\nEnter no. of vertices: ");
scanf("%d", &n);
printf("Enter the cost adjacency matrix\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &cost[i][j]);
printf("\nEnter the source node: ");
scanf("%d", &source);
dij(source, cost, visited, d, n);
for (i = 1; i <= n; i++)
if (i != source)
printf("\nShortest path from %d to %d is %d", source, i, d[i]);
getch();
void dij(int source, int cost[20][20], int visited[20], int d[20], int n)
int i, j, min, u, w;
for (i = 1; i <= n; i++)
visited[i] = 0;
d[i] = cost[i];
visited = 1;
d = 0;
for (j = 2; j <= n; j++)
min = 999;
for (i = 1; i <= n; i++)
if (!visited[i])
if (d[i] < min)
min = d[i];
u = i;
}
visited[u] = 1;
for (w = 1; w <= n; w++)
if (cost[u][w] != 999 && visited[w] == 0)
if (d[w] > cost[u][w] + d[u])
d[w] = cost[u][w] + d[u];
} // for w
} // for j
OUTPUT:
Experiment 6
Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm.
PROGRAM:
#include <stdio.h>
#include <conio.h>
int ne = 1, min_cost = 0;
void main()
int n, i, j, min, a, u, b, v, cost[20][20], parent[20];
printf("\nEnter the no. of vertices:");
scanf("%d", &n);
printf("\nEnter the cost matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &cost[i][j]);
for (i = 1; i <= n; i++)
parent[i] = 0;
printf("\nThe edges of spanning tree are\n");
while (ne < n)
min = 999;
for (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 (u != v)
printf("Edge %d\t(%d->%d)=%d\n", ne++, a, b, min);
min_cost = min_cost + min;
parent[v] = u;
cost[a][b] = cost[a][b] = 999;
printf("\nMinimum cost=%d\n", min_cost);
getch();
}
OUTPUT:
Experiment 7
a. Print all the nodes reachable from a given starting node in a digraph using BFS
method.
b. Check whether a given graph is connected or not using DFS method.
PROGRAMS:
(A)
#include <stdio.h>
#include <conio.h>
void BFS(int[20][20], int, int[20], int);
void main()
int n, a[20][20], i, j, visited[20], source;
printf("\nEnter the number of vertices:");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
for (i = 1; i <= n; i++)
visited[i] = 0;
printf("\nEnter the source node:");
scanf("%d", &source);
visited = 1;
BFS(a, source, visited, n);
for (i = 1; i <= n; i++)
if (visited[i] != 0)
printf("\n Node %d is reachable", i);
else
printf("\n Node %d is not reachable", i);
getch();
void BFS(int a[20][20], int source, int visited[20], int n)
int queue[20], f, r, u, v;
f = 0;
r = -1;
queue[++r] = source;
while (f <= r)
u = queue[f++];
for (v = 1; v <= n; v++)
if (a[u][v] == 1 && visited[v] == 0)
queue[++r] = v;
visited[v] = 1;
} // for v
} // while
OUTPUT:
(B)
#include<stdio.h>
void DFS(int[20][20], int, int[20], int);
int main()
int n, a[20][20], i, j, visited[20], source;
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
for (i = 1; i <= n; i++)
visited[i] = 0;
printf("\nEnter the source node: ");
scanf("%d", &source);
DFS(a, source, visited, n);
for (i = 1; i <= n; i++)
if (visited[i] == 0)
printf("\nGraph is not connected");
break;
}
if(i>n)
printf("\nGraph is connected\n");
return 0;
void DFS(int a[20][20], int u, int visited[20], int n)
int v;
visited[u] = 1;
for (v = 1; v <= n; v++)
if (a[u][v] == 1 && visited[v] == 0)
DFS(a, v, visited, n);
OUTPUT:
Experiment 8
Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
PROGRAM:
// Prim's Algorithm in C
#include <stdio.h>
#include <stdbool.h>
#de ne INF 9999999
// number of vertices in graph
#de ne V 5
// create a 2d array of size 5x5
// for adjacency matrix to represent graph
int G[V][V] = {
{0, INF, 75, 0, 0},
{9, 0, 95, 19, 42},
{75, 95, 0, 51, 66},
{0, 19, 51, 0, 31},
{0, INF, 66, 31, 0}};
int main()
int no_edge; // number of edge
// create a array to track selected vertex
// selected will become true otherwise false
int selected[V];
// set selected false initially
memset(selected, false, sizeof(selected));
fi
fi
// set number of edge to 0
no_edge = 0;
// the number of egde in minimum spanning tree will be
// always less than (V -1), where V is number of vertices in
// graph
// choose 0th vertex and make it true
selected[0] = true;
int x; // row number
int y; // col number
// print for edge and weight
printf("\nEdge : Weight\n");
while (no_edge < V - 1)
// For every vertex in the set S, nd the all adjacent vertices
// , calculate the distance from the vertex selected at step 1.
// if the vertex is already in the set S, discard it otherwise
// choose another vertex nearest to selected vertex at step 1.
int min = INF;
x = 0;
y = 0;
for (int i = 0; i < V; i++)
if (selected[i])
for (int j = 0; j < V; j++)
{
fi
if (!selected[j] && G[i][j])
{ // not in selected and there is an edge
if (min > G[i][j])
min = G[i][j];
x = i;
y = j;
printf("%d - %d : %d\n", x, y, G[x][y]);
selected[y] = true;
no_edge++;
return 0;
OUTPUT: