AoA Exp 1 To 10 by Gaurav Ameta
AoA Exp 1 To 10 by Gaurav Ameta
Aim:- 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.
Program :-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Partition function for Quicksort
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Last element as pivot
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Quicksort function
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
// Function to generate random array
void generate_random_array(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 10000; // Random numbers between 0 and 9999
}
}
// Main function
int main() {
int sizes[] = {100, 500, 1000, 5000, 10000}; // Sizes for the experiment
int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
printf("Size\tTime (seconds)\tSorted Values (First 10)\n");
printf("---------------------------------------------------\n");
for (int i = 0; i < num_sizes; i++) {
int n = sizes[i];
int *arr = (int *)malloc(n * sizeof(int));
// Generate random array
generate_random_array(arr, n);
// Measure sorting time
clock_t start = clock();
quicksort(arr, 0, n - 1);
clock_t end = clock();
double time_taken = (double)(end - start) / CLOCKS_PER_SEC;
// Print size, time taken, and first 10 sorted values
printf("%d\t%.6f\t\t", n, time_taken);
for (int j = 0; j < (n < 10 ? n : 10); j++) { // Print first 10 or all values if n <
printf("%d ", arr[j]);
}
printf("\n");
free(arr); // Free allocated memory
}
return 0;
}
Output:-
Size Time (seconds) Sorted Values (First 10)
---------------------------------------------------
100 0.000009 12 27 59 336 364 492 540 545 846 886
500 0.000041 19 28 49 71 81 84 90 97 124 163
1000 0.000094 0 3 8 19 30 30 32 37 49 49
5000 0.000530 1 1 5 5 10 12 12 14 17 18
10000 0.000941 1 4 5 6 8 8 10 11 12 13
Experiment – 2
Aim :- 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 <stdlib.h>
#include <time.h>
// Merge two sorted halves
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
free(L);
free(R);
}
// Sequential Merge Sort
void merge_sort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
// Generate random array
void generate_random_array(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100000; // Random numbers between 0 and 99999
}
}
// Main function
int main() {
int sizes[] = {100, 500, 1000, 5000, 10000}; // Array sizes
int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
printf("Size\tTime (seconds)\tSorted Values (First 10)\n");
printf("-----------------------------------------------------------\n");
for (int i = 0; i < num_sizes; i++) {
int n = sizes[i];
int *arr = (int *)malloc(n * sizeof(int));
generate_random_array(arr, n);
// Measure sorting time
clock_t start = clock();
merge_sort(arr, 0, n - 1);
clock_t end = clock();
double time_taken = (double)(end - start) / CLOCKS_PER_SEC;
// Print size, time taken, and first 10 sorted values
printf("%d\t%.6f\t\t", n, time_taken);
for (int j = 0; j < (n < 10 ? n : 10); j++)
{ // Print first 10 or all values if n < 10
printf("%d ", arr[j]);
}
printf("\n");
free(arr);
}
return 0;
}
Output:-
Size Time (seconds) Sorted Values (First 10)
-----------------------------------------------------------
100 0.000016 545 2305 2362 2567 2651 3135 3526 5211 5403 6862
500 0.000089 81 237 569 606 681 723 973 1039 1255 1474
1000 0.000166 30 346 657 669 801 821 925 938 960 1052
5000 0.000955 10 34 44 77 93 96 113 117 131 178
10000 0.001874 4 13 14 29 44 64 154 158 161 168
Experiment – 3
Aim :- (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.
Program :-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void push(int v) {
stack[++top] = v;
}
int pop() {
return stack[top--];
}
push(v);
}
while (1) {
printf("\nMenu:\n");
printf("1. Find Topological Ordering\n");
printf("2. Compute Transitive Closure\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
topological_sort(adj, n);
break;
case 2:
transitive_closure(adj, n);
break;
case 3:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Output:-
Enter the number of vertices in the graph: 4
Enter the adjacency matrix (4x4):
1000
0010
0100
0001
Menu:
1. Find Topological Ordering
2. Compute Transitive Closure
3. Exit
Enter your choice: 1
Topological Order: 3 1 2 0
Menu:
1. Find Topological Ordering
2. Compute Transitive Closure
3. Exit
Enter your choice: 2
Transitive Closure:
1000
0110
0110
0001
Menu:
1. Find Topological Ordering
2. Compute Transitive Closure
3. Exit
Enter your choice: 3
Experiment – 4
Aim :- Implement 0/1 Knapsack problem using Dynamic Programming.
Program:-
#include<stdio.h>
void knapsack01(int v[],int w[],int n,int capacity)
{
int x=0,b[100][100],i=0;
for(x=0;x<=capacity;x++)
b[0][x]=0;
for(i=1;i<=n;i++)
b[i][0]=0;
for(i=1;i<=n;i++)
{
for(x=1;x<=capacity;x++)
{
if(w[i]<=x)
{
if( (v[i]+b[i-1][x-w[i]] ) > b[i-1][x] )
b[i][x]= (v[i]+b[i-1][x-w[i]] ) ;
else
b[i][x]=b[i-1][x];
}
else
b[i][x]=b[i-1][x];
}
}
printf("the matix is\n");
for(i=0;i<=n;i++)
{
for(x=0;x<=capacity;x++)
printf("\t%d",b[i][x]);
printf("\n");
}
printf("the benifits is \n%d",b[n][capacity]);
printf("\nthe selected items is\n");
i=n,x=capacity;
while(i>0 && x>0)
{
if(b[i][x]!=b[i-1][x])
{
printf("\t%d",i);
b[i][x]=b[i-1][x-w[i]];
x=x-w[i];
i=i-1;
}
else
{
b[i][x]=b[i-1][x];
i=i-1;
}
}
}
void main()
{
int i,n,capacity,v[100],w[100];
printf("enter the total capacity of knapsack \n");
scanf("%d",&capacity);
printf("enter the no of items\n");
scanf("%d",&n);//value of each item's
printf("enter the weight and value of items\n");
for(i=1;i<=n;i++)
scanf("%d %d",&w[i],&v[i]);
knapsack01(v,w,n,capacity);
}
Output:-
enter the total capacity of knapsack
50
enter the no of items
3
enter the weight and value of items
10 60
20 100
30 120
the matix is
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0 0 0 0 0 0 0 0 0 60 60 60
60 60 60 60 60 60 60 60 60 60 60 60
60 60 60 60 60 60 60 60 60 60 60 60
60 60 60 60 60 60 60 60 60 60 60 60
60 60
0 0 0 0 0 0 0 0 0 0 60 60 60
60 60 60 60 60 60 60 100 100 100 100
100 100 100 100 100 100 160 160 160 160 160
160 160 160 160 160 160 160 160 160 160 160
160 160 160 160 160
0 0 0 0 0 0 0 0 0 0 60 60 60
60 60 60 60 60 60 60 100 100 100 100
100 100 100 100 100 100 160 160 160 160 160
160 160 160 160 160 180 180 180 180 180 180
180 180 180 180 220
the benifits is
220
the selected items is
3 2
Experiment – 5
Aim :- From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
Program :-
#include <stdio.h>
#include <limits.h>
// Update the distance value of the adjacent vertices of the picked vertex
for (int v = 0; v < n; v++) {
// Update dist[v] if there is a shorter path from u to v
if (sptSet[v] == 0 && graph[u][v] && dist[u] != INF && dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
parent[v] = u; // Update the parent of v
}
}
}
int main() {
int n, src;
int graph[MAX][MAX];
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() {
printf("\n\n\tImplementation of Kruskal's Algorithm\n\n");
// Remove the selected edge from the graph (set its cost to 999)
cost[a][b] = cost[b][a] = 999;
}
int find(int i) {
while(parent[i]) {
i = parent[i];
}
return i;
}
Edge (1,2) = 10
Edge (1,3) = 20
Edge (1,4) = 30
Minimum cost = 60
Experiment – 7
Aim :- (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.
Program:-
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
int isConnected() {
// Start DFS from node 0
for (int i = 0; i < n; i++) visited[i] = 0; // Reset visited array
DFS(0);
// Check if all nodes were visited (graph is connected if all nodes are visited)
for (int i = 0; i < n; i++) {
if (!visited[i]) {
return 0; // Graph is not connected
}
}
return 1; // Graph is connected
}
void menu() {
int startNode, choice;
do {
printf("\n--- Menu ---\n");
printf("1. BFS to print reachable nodes from a given start node\n");
printf("2. Check if the graph is connected using DFS\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the starting node for BFS: ");
scanf("%d", &startNode);
for (int i = 0; i < n; i++) visited[i] = 0; // Reset visited array
BFS(startNode);
break;
case 2:
if (isConnected()) {
printf("The graph is connected.\n");
} else {
printf("The graph is not connected.\n");
}
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3); // Loop until user chooses to exit
}
int main() {
printf("Enter the number of nodes in the graph: ");
scanf("%d", &n);
// Display menu
menu();
return 0;
}
Output:-
Enter the number of nodes in the graph: 4
Enter the adjacency matrix (0 for no edge, 1 for an edge):
0101
1010
0101
1010
#define MAX 20
int graph[MAX][MAX], n;
void primsAlgorithm() {
int parent[MAX], key[MAX], visited[MAX];
int minCost = 0;
int main() {
// Input the number of vertices in the graph
printf("Enter the number of vertices: ");
scanf("%d", &n);
return 0;
}
Output:-
Enter the number of vertices: 5
Enter the adjacency matrix (0 for no edge, else enter weight):
02060
20385
03007
68090
05700
#define MAX 10
#define INF INT_MAX
int main() {
int graph[MAX][MAX], n, i, j;
return 0;
}
Output:-
Enter the number of vertices: 4
Enter the adjacency matrix (0 for no edge, otherwise enter edge weight):
0300
3020
0205
0050
#define MAX 10
return true;
}
// Consider this row and try placing the queen in all columns
for (int col = 0; col < n; col++) {
if (isSafe(board, row, col, n)) {
board[row][col] = 1; // Place the queen
return false; // If the queen cannot be placed in any column in this row
}
if (solveNQueens(board, 0, n)) {
printSolution(board, n); // Print the solution
} else {
printf("Solution does not exist\n");
}
}
int main() {
int n;
return 0;
}
Output:-
Enter the number of queens: 8
Q.......
....Q...
.......Q
.....Q..
..Q.....
......Q.
.Q......
...Q....