Design and Analysis of Algorithms Lab
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>
#include <stdlib.h>
struct MinMax {
int min;
int max;
};
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
struct MinMax mm = findMinMax(arr, 0, n - 1);
printf("Minimum element: %d\n", mm.min);
printf("Maximum element: %d\n", mm.max);
return 0;
}
2. Sort a given set of elements using Merge 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 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>
int main() {
int n, arr[100000], choice;
FILE *fp;
while (1) {
printf("\n\n1. Merge Sort\n2. Graph Data\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of elements: ");
scanf("%d", &n);
genRandomArray(arr, n);
printf("Array before sorting: ");
printArray(arr, n);
clock_t start = clock();
mergesort(arr, 0, n - 1);
clock_t end = clock();
printf("Array after sorting: ");
printArray(arr, n);
printf("Time taken: %lf\n", getTime(start, end));
break;
case 2:
fp = fopen("mergesort.txt", "w");
for (int i = 10000; i <= 100000; i+= 10000) {
genRandomArray(arr, i);
clock_t start = clock();
mergesort(arr, 0, i - 1);
clock_t end = clock();
fprintf(fp, "%d\t%lf\n", i, getTime(start, end));
}
fclose(fp);
printf("Graph data written to file mergesort.txt");
break;
case 3:
exit(0);
default:
printf("Invalid choice");
break;
}
}
}
3. Sort a given set of elements using Quick 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 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>
int main() {
int n, arr[100000], choice;
FILE *fp;
while (1) {
printf("\n\n1. Quick Sort\n2. Graph Data\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of elements: ");
scanf("%d", &n);
genRandomArray(arr, n);
printf("Array before sorting: ");
printArray(arr, n);
clock_t start = clock();
quicksort(arr, 0, n - 1);
clock_t end = clock();
printf("Array after sorting: ");
printArray(arr, n);
printf("Time taken: %f\n", getTime(start, end));
break;
case 2:
fp = fopen("quicksort.txt", "w");
for (int i = 10000; i <= 100000; i += 10000) {
genRandomArray(arr, i);
clock_t start = clock();
quicksort(arr, 0, i - 1);
clock_t end = clock();
fprintf(fp, "%d\t%lf\n", i, getTime(start, end));
}
fclose(fp);
printf("Graph data written to quicksort.txt");
break;
case 3:
exit(0);
default:
printf("Invalid choice");
break;
}
}
}
int main() {
int a[MAX][MAX], num_ver;
char vertices[MAX];
printf("Enter the number of vertices: ");
scanf("%d", &num_ver);
printf("Enter the vertices: ");
for (int i = 0; i < num_ver; i++) {
scanf(" %c", &vertices[i]);
}
printf("Enter the adjacency matrix: \n");
for (int i = 0; i < num_ver; i++) {
for (int j = 0; j < num_ver; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nDFS Traversal: \n");
DFS(a, num_ver, vertices);
printf("\nTopological order: ");
for (int i = top; i >= 0; i--) {
printf("%c ", stack[i]);
}
printf("\n");
return 0;
}
Source Removal Method
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int indegree[MAX];
int main() {
int a[MAX][MAX], num_ver;
char vertices[MAX];
printf("Enter the number of vertices: ");
scanf("%d", &num_ver);
printf("Enter the vertices: ");
for (int i = 0; i < num_ver; i++) {
getchar();
scanf("%c", &vertices[i]);
}
printf("Enter the adjacency matrix: ");
for (int i = 0; i < num_ver; i++) {
for (int j = 0; j < num_ver; j++) {
scanf("%d", &a[i][j]);
}
}
for (int i = 0; i < num_ver; i++) {
int count = 0;
for (int j = 0; j < num_ver; j++) {
if (a[j][i] == 1) {
count++;
}
}
indegree[i] = count;
}
printf("\nTopological order: ");
for (int i = 0; i < num_ver; i++) {
int source = findSource(num_ver);
for (int j = 0; j < num_ver; j++) {
if (a[source][j] == 1) {
indegree[j]--;
}
}
indegree[source] = -1;
printf("%c ", vertices[source]);
}
return 0;
}
5. Print all the nodes reachable from a given star ng node in a given digraph using
Depth First Search method.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a[MAX][MAX], num_ver;
char vertices[MAX];
printf("Enter the number of vertices: ");
scanf("%d", &num_ver);
printf("Enter the vertices: ");
for (int i = 0; i < num_ver; i++) {
getchar();
scanf(" %c", &vertices[i]);
}
printf("Enter the adjacency matrix: ");
for (int i = 0; i < num_ver; i++) {
for (int j = 0; j < num_ver; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nDFS Traversal: \n");
DFS(a, num_ver, vertices);
return 0;
}
6. Print all the nodes reachable from a given star ng node in a given digraph using
breadth First Search method.
#include <stdio.h>
#include <stdlib.h>
int main() {
int num_ver, a[MAX][MAX];
char vertices[MAX];
printf("Enter the number of vertices: ");
scanf("%d", &num_ver);
printf("Enter the vertices: ");
for (int i = 0; i < num_ver; i++) {
scanf(" %c", &vertices[i]);
}
printf("Enter the adjacency matrix: \n");
for (int i = 0; i < num_ver; i++) {
for (int j = 0; j < num_ver; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nBFS Traversal: \n");
BFS(a, num_ver, vertices);
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 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>
int main() {
int n, arr[100000], choice;
FILE *fp;
while (1) {
printf("\n\n1. Heap Sort\n2. Graph Data\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of elements: ");
scanf("%d", &n);
genRandomArray(arr, n);
printf("Array before sorting: ");
printArray(arr, n);
clock_t start = clock();
heapsort(arr, n);
clock_t end = clock();
printf("Array after sorting: ");
printArray(arr, n);
printf("Time taken: %f\n", getTime(start, end));
break;
case 2:
fp = fopen("heapsort.txt", "w");
for (int i = 10000; i <= 100000; i += 10000) {
genRandomArray(arr, i);
clock_t start = clock();
heapsort(arr, i);
clock_t end = clock();
fprintf(fp, "%d\t%lf\n", i, getTime(start, end));
}
fclose(fp);
printf("Graph data written to heapsort.txt");
break;
case 3:
exit(0);
default:
printf("Invalid choice");
break;
}
}
}