Design And Analysis Of Algorithms Lab
1. Use divide and conquer method to recursively find the maximum and minimum
elements in a given list of n elements.
#include<stdio.h>
struct temp{
int max,min;
};
struct temp maxmin(int a[],int l,int r){
struct temp mm,mml,mmr;
if(l==r){
mm.max=mm.min=a[l];
return mm;
}
else if(r-l==1){
mm.max= (a[l]<a[r])?a[r]:a[l];
mm.min= (a[l]<a[r])?a[l]:a[r];
return mm;
}
else {
int mid=(r+l)/2;
mml=maxmin(a,l,mid);
mmr=maxmin(a,mid+1,r);
mm.max= (mml.max>mmr.max)?mml.max:mmr.max;
mm.min= (mml.min<mmr.min)?mml.min:mmr.min;
return mm;
}
}
int main(){
int n;
printf("Enter size of arry:");
scanf("%d",&n);
int a[n];
printf("Enter those Elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
struct temp fmm=maxmin(a,0,n-1);
printf("Maximmum element in arry is %d\n",fmm.max);
printf("Minimmum element in arry is %d\n",fmm.min);
return 0;
}
2. Sort a given set of elements using Merge Sort method and determine the time required to sort
the elements. Repeat the experiment for different values of n, thenumber of elements in the list to
be sorted and plot a graph of me 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>
// Global variables
int t = 0;
int n;
// Function prototypes
void divarr(int arry[], int low, int high);
void mergesort(int a[], int l, int m, int h);
int main() {
int ch;
while(1){
printf("\n1.Merge Sort\n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d", &ch);
switch(ch){
case 1:{
printf("Enter the size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements of array: ");
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
divarr(a, 0, n - 1);
printf("Elements of array in sorted order: ");
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
break;
case 2: {
FILE *fp = fopen("mergesort.txt", "w");
for (int i = 100; i < 10000; i += 100) {
int a[i];
srand(time(NULL));
for (int j = 0; j < i; j++) {
a[j] = rand() % 1000;
}
clock_t s = clock();
divarr(a, 0, n - 1);
clock_t e = clock();
double tk = ((double)(e - s)) / CLOCKS_PER_SEC;
fprintf(fp, "%d\t%lf\n", i, tk);
}
printf("\nData File generated and stored in file <mergesort.txt >.\n Use a
plotting utility\n");
fclose(fp);
break;
case 3:exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
}
return 0;
}
void divarr(int arry[], int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
divarr(arry, low, mid);
divarr(arry, mid + 1, high);
mergesort(arry, low, mid, high);
}
}
void mergesort(int a[], int l, int m, int h) {
int i, j, k;
int temp[n]; // Use n as the size for the temporary array
i = l;
j = m + 1;
k = l;
// Merge the two halves into temp array
while (i <= m && j <= h) {
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
// Copy the remaining elements of left half, if any
while (i <= m)
temp[k++] = a[i++];
// Copy the remaining elements of right half, if any
while (j <= h)
temp[k++] = a[j++];
k--;
// Copy the merged elements back into the original array
for (i = l; i <=k; i++)
a[i] = temp[i];
// Print the array and temp array for each iteration
}
3. Sort a given set of elements using Quick Sort 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 me 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(int *a,int *b){
int temp=*a;
*a=*b;
*b=temp;
}
/*int partision(int a[],int l,int r){
int p=a[l];
int i=l+1;
int j=r;
while(i<=j){
while(p>=a[i] && i<=r)
i++;
while(p<=a[j] && j>=l)
j--;
if(i<j)
swap(&a[i],&a[j]);
else{
swap(&a[l],&a[j]);
return j;
}
}
} */
int partision(int a[], int l, int r) {
int p = a[l];
int i = l+1;
int j = r;
while (i<j) {
while (a[i] <= p && i < r)
i++;
while (a[j] >= p && j > l)
j--;
if (i < j)
swap(&a[i], &a[j]);
else
swap(&a[l], &a[j]);
return j;
}
}
void quicksort(int a[],int l,int r){
if(l<r){
int s=partision(a,l,r);
quicksort(a,l,s-1);
quicksort(a,s+1,r);
}
}
int main(){
while(1){
int ch;
int a[10000];
printf("1.quick sort \n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d",&ch);
fflush(stdin);
switch(ch){
case 1:
int n;
printf("Enter the Size of arry:");
scanf("%d",&n);
printf("Enter those Elementes:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
clock_t s=clock();
quicksort(a,0,n-1);
clock_t e=clock();
double t=((double)(e-s))/CLOCKS_PER_SEC;
printf("time taken is %lf\n",t);
printf("Elementes in sorted order are:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
break;
case 2:
FILE *fp=fopen("quicksort_graph.txt","w");
srand(time(0));
for(int i=100;i<=10000;i+=100){
for(int j=0;j<i;j++)
a[j]=rand()%1000;
clock_t s=clock();
quicksort(a,0,i-1);
clock_t e=clock();
double t=((double) (e-s))/CLOCKS_PER_SEC;
fprintf(fp,"%d\t%lf\n",i,t);
}
printf("\nData File generated and stored in file <quicksort_graph.txt >.\nUse
a plotting utility\n");
fclose(fp);
break;
case 3:exit(0);
default:printf("Invalid choice");
}
}
return 0;
}
4. Obtain the Topological Ordering of vertices in a given digraph
DFS:
#include <stdio.h>
#include <stdlib.h>
#define max 100
int v[max], count = 0, top = -1;
char stack[max]; /* GLOBAL VARIABLES */
void dfs(int i, int n, int g[][max], char vertices[]) { /* DFS */
v[i] = count++;
printf("Vertex %c is marked with %d\n", vertices[i], count);
for (int j = 0; j < n; j++) {
if (g[i][j] == 1 && v[j] == 0) {
dfs(j, n, g, vertices);
}
}
stack[++top] = vertices[i];
}
void topological_order(int n, int g[][max], char vertices[]) { /* TO FIND TOPOLOGICAL
ORDER */
for (int i = 0; i < n; i++) {
v[i] = 0;
}
for (int i = 0; i < n; i++) {
if (v[i] == 0) {
dfs(i, n, g, vertices);
}
}
return;
}
int main() {
int n;
printf("Enter number of vertices\n"); /* READ NUMBER OF VERTICES */
scanf("%d", &n);
int g[max][max];
printf("Enter the adjacency matrix :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &g[i][j]);
}
}
char vertices[max];
printf("Enter the vertices:\n");
for (int i = 0; i < n; i++) {
scanf(" %c", &vertices[i]); // Add space before %c to consume any newline
characters
}
printf("\nTopological order:\n");
topological_order(n, g, vertices);
for (int i = top; i >= 0; i--) {
printf("-->%c", stack[i]);
}
printf("\n");
return 0;
}
Source Removal:
#include <stdio.h>
#include <stdlib.h>
#define max 100
int id[max];
int findsource(int n){
for(int i=0;i<n;i++)
if(id[i]==0)
return i;
int main() {
int n;
printf("Enter number of vertices\n"); /* READ NUMBER OF VERTICES */
scanf("%d", &n);
int g[max][max];
printf("Enter the adjacency matrix :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &g[i][j]);
}
}
char vertices[max];
printf("Enter the vertices:\n");
for (int i = 0; i < n; i++) {
scanf(" %c", &vertices[i]); // Add space before %c to consume any newline
characters
}
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++){
if(g[j][i]==1)
count++;
}
id[i]=count;
}
printf("\n Topological order:");
for(int i=0;i<n;i++){
int s=findsource(n);
for(int j=0;j<n;j++){
if(g[s][j]==1){
id[j]--;
}
}
id[s]=-1;
printf("%c ",vertices[s]);
}
printf("\n");
return 0;
}
5. Print all the nodes reachable from a given starng node in a given digraph using Depth First
Search method
#include <stdio.h>
int v[100];
void dfs(int n,int s, int v[n], int g[n][n]) {
v[s] = 1;
for (int i = 1; i <= n; i++) {
if (g[s][i] && !v[i]) {
dfs(n,i, v, g);
}
}
}
int main() {
int n;
printf("Enter the number of vertices : ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
v[i] = 0;
int g[n][n];
printf("Enter the adjacency matrix :\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &g[i][j]);
int s;
printf("Enter the source vertex : ");
scanf("%d", &s);
dfs(n,s, v, g);
for (int k = 1; k <= n; k++) {
if (v[k]) {
printf("\nVertex %d reachable \n", k);
} else {
printf("\nVertex %d not reachable \n", k);
}
}
return 0;
}
6. Print all the nodes reachable from a given starng node in a given digraph using breadth First
Search method.
#include <stdio.h>
#define max 100
int n,v[max], q[max], f=1, r= 0;
void bfs(int s,int g[n][n])
{
for (int i = 1; i <= n; i++)
if (g[s][i] && !v[i]){
q[++r] = i;
}
if (f <= r)
{
v[q[f]] = 1;
bfs(q[f++],g);
}
}
int main()
{
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
q[i] = 0;
v[i] = 0;
}
int g[n][n];
printf("Enter graph data in matrix form: \n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &g[i][j]);
int s;
printf("Enter the starting vertex: ");
scanf("%d", &s);
v[s]=1;
bfs(s,g);
printf("Vertices which can be reached from vertex %d are\n",s);
for (int i = 1; i <= n; i++)
if (v[i])
printf("%d is reachabl\n", i);
else
printf("%d is not reachabl\n", i);
return 0;
}
7. Sort a given set of elements using Heap Sort method and determine the me 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 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>
// Function prototypes
void heapfy(int a[], int n);
void heapsort(int a[], int n);
int main(){
int ch;
while(1){
printf("\n1.Heap Sort\n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d", &ch);
switch(ch){
case 1:{
int n;
printf("Enter the value of n:");
scanf("%d", &n);
int a[n];
printf("Enter the Elements:");
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
heapfy(a, n);
heapsort(a, n);
printf("Sorted elements are:\n");
for(int i = 1; i <= n; i++)
printf("%d ", a[i]);
printf("\n");
break;
}
case 2:
FILE *fp = fopen("heapsort.txt", "w");
for (int i = 100; i < 10000; i += 100) {
int a[i];
srand(time(NULL));
for (int j = 1; j <= i; j++) {
a[j] = rand() % 1000;
}
clock_t s = clock();
heapfy(a, i);
heapsort(a, i);
clock_t e = clock();
double tk = ((double)(e - s)) / CLOCKS_PER_SEC;
fprintf(fp, "%d\t%lf\n", i, tk);
}
printf("\nData File generated and stored in file <heapsort.txt >.\n Use a
plotting utility\n");
fclose(fp);
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
void heapfy(int a[],int n){
for(int i=n/2;i>=1;i--){
int k=i;
int v=a[k];
int flag=0;
while(!flag && 2*k<=n){
int j=2*k;
if(j<n){
if(a[j]<a[j+1])
j=j+1;
}
if(v>=a[j])
flag=1;
else{
a[k]=a[j];
k=j;
}
}
a[k]=v;
}
}
void heapsort(int a[],int n){
for(int i=n;i>1;i--){
int temp=a[1];
a[1]=a[i];
a[i]=temp;
heapfy(a,i-1);
}
}