Daa Lab
Daa Lab
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int sizes[] = {100, 500, 2000, 10000, 50000, 100000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
start = clock();
linearSearchRecursive(arr, size, target, 0);
end = clock();
free(arr);
}
return 0;
}
Output:
Input Size (n) Linear Search Time (ms)
100 0
500 0
2000 0
10000 0
50000 1
100000 2
Objective-1.1: Implementation and analysis of Binary Search and using
recursive approach.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int sizes[] = {100, 500, 2000, 10000, 50000, 100000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
start = clock();
binarySearchRecursive(arr, 0, size - 1, target);
end = clock();
free(arr);
}
return 0;
}
Output:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int sizes[] = {500, 2000, 10000, 20000, 50000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
generateArray(arr, size);
double time_taken = measureTime(insertSort, arr, size);
free(arr);
}
return 0;
}
Output:
Input Size (n) Insertion Sort Time (ms)
500 0
2000 2
10000 52
50000 1245
100000 4920
Objective-2.1 : Implementation and analysis of Selection sort.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int sizes[] = {500, 2000, 10000, 50000, 100000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
free(arr);
}
return 0;
}
Output:
Input Size (n) Selection Sort Time (ms)
500 1
2000 3
10000 72
50000 1872
100000 7248
Objective-2.1 : Implementation and analysis of Bubble sort.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int sizes[] = {500, 2000, 10000, 50000, 100000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
printf(" Input Size (n) Bubble Sort Time (ms) \n");
generateArray(arr, size);
int time_taken_bubble = measureTime(bubbleSort, arr, size);
return 0;
}
Output:
Input Size (n) Bubble Sort Time (ms)
500 0
2000 6
10000 135
50000 5737
100000 26844
Experiment No. 3
Objective : Implementation and analysis of Merge sort and Quick sort.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
return i + 1;
}
int main() {
int sizes[] = {500, 1000, 5000, 20000, 50000, 100000};
int n_sizes = sizeof(sizes) / sizeof(sizes[0]);
printf(" Input Size (n) Merge Sort Time (s) Quick Sort Time (s) \n");
generate_random_array(arr1, size);
int time_taken_merge = measure_time(mergeSort, arr1, size);
generate_random_array(arr2, size);
int time_taken_quick = measure_time(quickSort, arr2, size);
Output :
Experiment No. 4
Objective : Implementation and analysis of Heap sort, Counting sort and Radix
sort.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int sizes[] = {500, 1000, 5000, 20000, 50000, 100000};
int nSizes = sizeof(sizes) / sizeof(sizes[0]);
printf(" Input Size (n) Heap Sort Time (ms) Counting Sort Time (ms) Radix Sort
Time (ms) \n");
generateRandomArray(arr1, size);
int heapTime = measureTime(heapSort, arr1, size);
generateRandomArray(arr2, size);
int countingTime = measureTime(countingSort, arr2, size);
generateRandomArray(arr3, size);
int radixTime = measureTime(radixSort, arr3, size);
printf(" %-18d %-24d %-23d %-23d \n", size, heapTime, countingTime, radixTime);
}
return 0;
}
Output :
Experiment No. 5
Objective : Implementation of Shell sort.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Output :
Experiment No. 6
Objective-6.1 : Implementation of Activity Selection Problem.
Code :
#include <stdio.h>
typedef struct {
int id;
int start;
int finish;
} Activity;
printf("Selected Activities:\n");
printf(" Activity ID Start Time Finish Time|\n");
int i = 0;
printf(" a%-15d %-10d %-10d \n", activities[i].id, activities[i].start, activities[i].finish);
int main() {
int n;
Activity activities[n];
printf("Enter activity ID, start time, and finish time for activity\n");
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &activities[i].id, &activities[i].start, &activities[i].finish);
}
selectActivities(activities, n);
return 0;
}
Output :
Objective-6.2 : Implementation of Knapsack Problem Using Greedy Solution.
Code :
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int profit;
int weight;
float ratio;
int id;
} Item;
int currentWeight = 0;
float totalProfit = 0.0;
return totalProfit;
}
int main() {
int n, W;
Item items[n];
printf("Enter details for each item (Item no., weight, profit):\n");
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &items[i].id, &items[i].weight, &items[i].profit);
}
Output :
Experiment No. 7
Objective-7.1 : Implementation and Analysis of the 0/1 Knapsack problem
using Dynamic Programming method.
Code :
#include <stdio.h>
void knapsack(int n, int W, int weights[], int values[]) {
int dp[n+1][W+1];
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else if (weights[i-1] <= w) {
dp[i][w] = (values[i-1] + dp[i-1][w-weights[i-1]] > dp[i-1][w]) ?
values[i-1] + dp[i-1][w-weights[i-1]] : dp[i-1][w];
} else {
dp[i][w] = dp[i-1][w];
}
}
}
printf("\nDP Table (Maximum Profit Calculation):\n");
printf(" n\\w ");
for (int w = 0; w <= W; w++) {
printf("%4d", w);
}
printf("\n");
for (int i = 0; i <= n; i++) {
printf("%4d ", i);
for (int w = 0; w <= W; w++) {
printf("%4d", dp[i][w]);
}
printf("\n");
}
printf("\nMaximum value that can be obtained: %d\n", dp[n][W]);
int w = W;
printf("\nSelected Items:\n");
printf("Item Number | Weight | Profit | Included\n");
printf("-----------------------------------------\n");
Output :
Objective-7.2 : Implementation and Analysis of LCS.
Code :
#include <stdio.h>
#include <string.h>
int i = m, j = n;
while (i > 0 && j > 0) {
if (X[i-1] == Y[j-1]) {
lcs[index-1] = X[i-1];
i--;
j--;
index--;
} else if (dp[i-1][j] > dp[i][j-1]) {
i--;
} else {
j--;
}
}
printf("Longest Common Subsequence: %s\n", lcs);
}
int main() {
char X[100], Y[100];
printf("Enter the first string: ");
scanf("%s", X);
printf("Enter the second string: ");
scanf("%s", Y);
int m = strlen(X);
int n = strlen(Y);
int length = lcs_length(X, Y, m, n);
printf("Length of Longest Common Subsequence: %d\n", length);
print_lcs(X, Y, m, n);
return 0;
}
Output:
Experiment No. 8
Objective-8.1 : Implementation of Kruskal’s Algorithm to Find MST.
Code :
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int u, v, weight;
} Edge;
typedef struct {
int parent[MAX_VERTICES];
int rank[MAX_VERTICES];
} DisjointSet;
Edge edges[MAX_EDGES];
DisjointSet ds;
int numEdges = 0, numVertices = 0;
void initDisjointSet(int n) {
for (int i = 0; i < n; i++) {
ds.parent[i] = i;
ds.rank[i] = 0;
}
}
int find(int u) {
if (ds.parent[u] != u) {
ds.parent[u] = find(ds.parent[u]);
}
return ds.parent[u];
}
if (rootU != rootV) {
if (ds.rank[rootU] > ds.rank[rootV]) {
ds.parent[rootV] = rootU;
} else if (ds.rank[rootU] < ds.rank[rootV]) {
ds.parent[rootU] = rootV;
} else {
ds.parent[rootV] = rootU;
ds.rank[rootU]++;
}
}
}
void kruskalMST() {
qsort(edges, numEdges, sizeof(Edge), compareEdges);
int mstWeight = 0;
printf("\nEdges in MST:\n");
if (find(u) != find(v)) {
printf("%d -- %d == %d\n", u, v, weight);
mstWeight += weight;
unionSets(u, v);
}
}
int main() {
int e, u, v, weight;
numEdges = e;
initDisjointSet(numVertices);
kruskalMST();
return 0;
}
Output:
Objective-8.2 : Implementation of Prim’s Algorithm to Find MST.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 20
#define INF 999999
int graph[MAX_VERTICES][MAX_VERTICES];
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
int visited[MAX_VERTICES];
int numVertices;
void primMST() {
for (int i = 0; i < numVertices; i++) {
key[i] = INF;
visited[i] = 0;
parent[i] = -1;
}
key[0] = 0;
int totalWeight = 0;
Output:
Experiment No. 9
Objective-9.1 : Implementation of Warshal’s Algorithm for all pair Shortest
path.
Code :
#include <stdio.h>
#include <stdlib.h>
#define INF 999999
#define MAX_VERTICES 20
int dist[MAX_VERTICES][MAX_VERTICES];
int n;
void floydWarshall() {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
void printSolution() {
printf("\nShortest distances between every pair of vertices:\n");
printf(" ");
for (int i = 0; i < n; i++) {
printf("%3d ", i + 1);
}
printf("\n");
for (int i = 0; i < n; i++) {
printf("%3d ", i + 1);
for (int j = 0; j < n; j++) {
if (dist[i][j] == INF) {
printf(" INF ");
} else {
printf("%3d ", dist[i][j]);
}
}
printf("\n");
}
}
int main() {
int e, u, v, weight;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
dist[i][j] = 0;
} else {
dist[i][j] = INF;
}
}
}
printf("Enter the number of edges: ");
scanf("%d", &e);
printf("Enter the edges (u v weight) as space-separated values:\n");
for (int i = 0; i < e; i++) {
scanf("%d %d %d", &u, &v, &weight);
u--; v--;
dist[u][v] = weight;
}
floydWarshall();
printSolution();
return 0;
}
Output:
Objective-9.2 : Implementation of Dijkstra’s Algorithm for Single Source
Shortest path.
Code:
#include <stdio.h>
#include <stdlib.h>
#define INF 999999
#define MAX_VERTICES 20
int graph[MAX_VERTICES][MAX_VERTICES];
int dist[MAX_VERTICES];
int prev[MAX_VERTICES];
int n;
void dijkstra(int src) {
int visited[MAX_VERTICES] = {0};
for (int i = 0; i < n; i++) {
dist[i] = INF;
prev[i] = -1;
}
dist[src] = 0;
for (int count = 0; count < n - 1; count++) {
int u = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && (u == -1 || dist[i] < dist[u])) {
u = i;
}
}
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (graph[u][v] != 0 && !visited[v] && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
prev[v] = u;
}}}}
void printSolution(int src) {
printf("\nShortest distances and corresponding edges from source %d:\n", src + 1);
for (int i = 0; i < n; i++) {
if (dist[i] == INF) {
printf("Vertex %d: INF\n", i + 1);
} else {
printf("Vertex %d: %d (Path: ", i + 1, dist[i]);
int current = i;
while (current != src) {
printf("%d <- ", current + 1);
current = prev[current];
}
printf("%d)", src + 1);
printf("\n");
}
}
}
int main() {
int e, u, v, weight, src;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j] = 0;
}
}
printf("Enter the number of edges: ");
scanf("%d", &e);
printf("Enter the edges (u v weight) as space-separated values:\n");
for (int i = 0; i < e; i++) {
scanf("%d %d %d", &u, &v, &weight);
u--; v--;
graph[u][v] = weight;
graph[v][u] = weight;
}
printf("Enter the source vertex: ");
scanf("%d", &src);
src--;
dijkstra(src);
printSolution(src);
return 0;
}
Output:
Experiment No. 10
Objective-10.1 : Implementation of N Queen Problem using Backtracking.
Code :
#include <stdio.h>
#include <stdbool.h>
#define MAX 20
int board[MAX][MAX];
int n;
void printSolution() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
printf("Enter the value of N: ");
scanf("%d", &n);
if (solveNQueens(0) == true) {
printf("Solution to the N-Queens problem is:\n");
printSolution();
} else {
printf("Solution does not exist for N = %d\n", n);
}
return 0;
}
Output:
Objective-9.2 : Implementation of Sum of Subset Problem using Backtracking
Code:
#include <stdio.h>
#define MAX 20
int set[MAX];
int n, targetSum;
int solution[MAX];
void printSolution() {
printf("Subset with sum %d: { ", targetSum);
for (int i = 0; i < n; i++) {
if (solution[i]) {
printf("%d ", set[i]);
}
}
printf("}\n");
}
void sumOfSubset(int i, int currentSum) {
if (currentSum == targetSum) {
printSolution();
return;
}
if (i == n || currentSum > targetSum) {
return;
}
solution[i] = 1;
sumOfSubset(i + 1, currentSum + set[i]);
solution[i] = 0;
sumOfSubset(i + 1, currentSum);
}
int main() {
printf("Enter the number of elements in the set: ");
scanf("%d", &n);
printf("Enter the elements of the set: ");
for (int i = 0; i < n; i++) {
scanf("%d", &set[i]);
}
printf("Enter the target sum: ");
scanf("%d", &targetSum);
for (int i = 0; i < n; i++) {
solution[i] = 0;
}
sumOfSubset(0, 0);
return 0;
}
Output: