0% found this document useful (0 votes)
2 views11 pages

Design and Analysis of Algorithms Lab

The document outlines various algorithms and methods for sorting and searching data structures, including Divide and Conquer for finding min/max, Merge Sort, Quick Sort, and Heap Sort, along with their implementations in C. It also covers graph algorithms for Topological Ordering using DFS and Source Removal methods, and traversal techniques using Depth First Search (DFS) and Breadth First Search (BFS). Each section includes code examples, user input for elements, and methods to measure execution time and generate graph data.

Uploaded by

metharuntejas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Design and Analysis of Algorithms Lab

The document outlines various algorithms and methods for sorting and searching data structures, including Divide and Conquer for finding min/max, Merge Sort, Quick Sort, and Heap Sort, along with their implementations in C. It also covers graph algorithms for Topological Ordering using DFS and Source Removal methods, and traversal techniques using Depth First Search (DFS) and Breadth First Search (BFS). Each section includes code examples, user input for elements, and methods to measure execution time and generate graph data.

Uploaded by

metharuntejas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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;
};

struct MinMax findMinMax(int arr[], int low, int high) {


struct MinMax mm, mml, mmr;
if (low == high) {
mm.min = arr[low];
mm.max = arr[low];
return mm;
}
if (high - low == 1) {
mm.min = (arr[low] < arr[high]) ? arr[low] : arr[high];
mm.max = (arr[low] > arr[high]) ? arr[low] : arr[high];
return mm;
}
int mid = (low + high) / 2;
mml = findMinMax(arr, low, mid);
mmr = findMinMax(arr, mid + 1, high);
mm.min = (mml.min < mmr.min) ? mml.min : mmr.min;
mm.max = (mml.max > mmr.max) ? mml.max : mmr.max;
return mm;
}

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>

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
}
else {
arr[k++] = R[j++];
}
}
while (i < n1) {
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}

void mergesort(int arr[], int l, int r) {


if (l < r) {
int m = (l + r) / 2;
mergesort(arr, l, m);
mergesort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

void genRandomArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

double getTime(clock_t start, clock_t end) {


return (double)(end - start) / CLOCKS_PER_SEC;
}

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>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[low];
int i = low, j = high;
while (i < j) {
while (arr[i] <= pivot && i < high) {
i++;
}
while (arr[j] >= pivot && j > low) {
j--;
}
if (i < j) {
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
return j;
}

void quicksort(int arr[], int low, int high) {


if (low < high) {
int s = partition(arr, low, high);
quicksort(arr, low, s - 1);
quicksort(arr, s + 1, high);
}
}

void genRandomArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

double getTime(clock_t start, clock_t end) {


return (double)(end - start) / CLOCKS_PER_SEC;
}

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;
}
}
}

4. Obtain the Topological Ordering of ver ces in a given digraph.


DFS Method
#include <stdio.h>
#include <stdlib.h>

#define MAX 100


int count = 0, visited[MAX];
char stack[MAX];
int top = -1;

void dfs(int v, int a[][MAX], int num_ver, char vertices[]) {


visited[v] = ++count;
printf("Vertex %c is marked with %d\n", vertices[v], count);
for (int w = 0; w < num_ver; w++) {
if (a[v][w] == 1 && visited[w] == 0) {
dfs(w, a, num_ver, vertices);
}
}
stack[++top] = vertices[v];
}

void DFS(int a[][MAX], int num_ver, char vertices[]) {


for (int i = 0; i < num_ver; i++) {
visited[i] = 0;
}
for (int v = 0; v < num_ver; v++) {
if (visited[v] == 0) {
dfs(v, a, num_ver, vertices);
}
}
}

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 findSource(int num_ver) {


for (int i = 0; i < num_ver; i++) {
if (indegree[i] == 0) {
return i;
}
}
}

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>

#define MAX 100

int count = 0, visited[MAX];

void dfs(int v, int a[][MAX], int num_ver, char vertices[]) {


visited[v] = ++count;
printf("Vertex %c is marked with %d\n", vertices[v], count);
for (int w = 0; w < num_ver; w++) {
if (a[v][w] == 1 && visited[w] == 0) {
dfs(w, a, num_ver, vertices);
}
}
}

void DFS(int a[][MAX], int num_ver, char vertices[]) {


for (int i = 0; i < num_ver; i++) {
visited[i] = 0;
}
for (int v = 0; v < num_ver; v++) {
if (visited[v] == 0) {
dfs(v, a, num_ver, vertices);
}
}
}

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>

#define MAX 100

int count = 0, visited[MAX];


int queue[MAX], f = 0, r = -1;

void bfs(int v, int a[][MAX], int num_ver, char vertices[]) {


visited[v] = ++count;
printf("Vertex %c is marked with %d\n", vertices[v], count);
queue[++r] = v;
while (f <= r) {
int cur_ver = queue[f++];
for (int w = 0; w < num_ver; w++) {
if (a[cur_ver][w] == 1 && visited[w] == 0) {
visited[w] = ++count;
printf("Vertex %c is marked with %d\n", vertices[w], count);
queue[++r] = w;
}
}
}
}

void BFS(int a[][MAX], int num_ver, char vertices[]) {


for (int i = 0; i < num_ver; i++) {
visited[i] = 0;
}
for (int v = 0; v < num_ver; v++) {
if (visited[v] == 0) {
bfs(v, a, num_ver, vertices);
}
}
}

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>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if(left < n && arr[left] > arr[largest]) {
largest = left;
}
if(right < n && arr[right] > arr[largest]) {
largest = right;
}
if(largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

void heapsort(int arr[], int n) {


for(int i = n/2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for(int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

void genRandomArray(int arr[], int n) {


for(int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
}
void printArray(int arr[], int n) {
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

double getTime(clock_t start, clock_t end) {


return (double)(end - start) / CLOCKS_PER_SEC;
}

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;
}
}
}

You might also like