DAA Lab
DAA Lab
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
};
temp->key = item;
return temp;
// In-order traversal
inorder(root->left);
inorder(root->right);
// Insert a node
return node;
return root;
}
// Find the in-order successor
current = current->left;
return current;
// Delete a node
else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
return root;
int main() {
inorder(root);
printf("\n");
else
inorder(root);
printf("\n");
return 0;
#include <stdio.h>
#include <string.h>
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
strcpy(arr[k], L[i]);
i++;
} else {
strcpy(arr[k], R[j]);
j++;
k++;
strcpy(arr[k], L[i]);
i++;
k++;
strcpy(arr[k], R[j]);
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
int arr_size = 5;
printf("\n");
printf("\n");
return 0;
}
2 A. Write a Program to implement Quick Sort for Sorting an Array of Structures
for given input.
Array of Structures:
1. Name: "Alice", Score: 50
2. Name: "Bob", Score: 80
3. Name: "Charlie", Score: 70
4. Name: "David", Score: 90
A. Write a Program to implement Quick Sort for Sorting an Array of Structures for
given input.
Array of Structures:
1. Name: "Alice", Score: 50
2. Name: "Bob", Score: 80
3. Name: "Charlie", Score: 70
4. Name: "David", Score: 90
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int score;
};
int main() {
struct Student arr[] = {{"Alice", 50}, {"Bob", 80}, {"Charlie", 70}, {"David", 90}};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
B.Write a program to implement the Towers of Hanoi problem using recursion.
#include <stdio.h>
if (n == 1) {
return;
int main() {
return 0;
}
3. A. Write a program to solve the Fractional Knapsack Problem for multiple test
cases.
Number of test cases: 2
Test case 1:
Number of items: 3
Items (Value, Weight): [(60, 10), (100, 20), (120, 30)]
Knapsack capacity: 50
Test case 2:
Number of items: 4
Items (Value, Weight): [(60, 10), (40, 40), (100, 20), (120, 30)]
Knapsack capacity: 50
Source vertex: 0
A. Write a program to solve the Fractional Knapsack Problem for multiple test cases.
Test case 2:
Number of items: 4
Items (Value, Weight): [(60, 10), (40, 40), (100, 20), (120, 30)]
Knapsack capacity: 50
#include <stdio.h>
#include <stdlib.h>
struct Item {
int value;
int weight;
};
int main() {
struct Item items1[] = {{60, 10}, {100, 20}, {120, 30}};
int capacity1 = 50;
int n1 = sizeof(items1) / sizeof(items1[0]);
printf("Test Case 1 - Maximum value in Knapsack = %.2f\n",
fractionalKnapsack(items1, n1, capacity1));
struct Item items2[] = {{60, 10}, {40, 40}, {100, 20}, {120, 30}};
int capacity2 = 50;
int n2 = sizeof(items2) / sizeof(items2[0]);
printf("Test Case 2 - Maximum value in Knapsack = %.2f\n",
fractionalKnapsack(items2, n2, capacity2));
return 0;
}
B. Write a program to implement Dijkstra’s Algorithm to find the shortest path from a
source vertex to all other vertices in a weighted, directed graph.
Number of vertices: 5
Graph (Adjacency Matrix):
0 10 0 0 20
10 0 10 0 0
0 10 0 30 0
0 0 30 0 0
20 0 0 0 0
Source vertex: 0
#include <stdio.h>
#include <limits.h>
#define V 5
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
int main() {
int graph[V][V] = {
{0, 10, 0, 0, 20},
{10, 0, 10, 0, 0},
{0, 10, 0, 30, 0},
{0, 0, 30, 0, 0},
{20, 0, 0, 0, 0}
};
int src = 0;
dijkstra(graph, src);
return 0;
}
4. A. Write a program to verify whether the given graph is connected before finding
the Minimum Spanning Tree using Prim’s Algorithm or Kruskal’s Algorithm.
Number of vertices: 4
Graph (Adjacency Matrix):
0100
1010
0101
0010
B. Write a program to implement Huffman Coding for a given set of characters
and their frequencies.
Character and frequency pairs:
A: 5, B: 9, C: 12, D: 13, E: 16, F: 45
A. Write a program to verify whether the given graph is connected before finding the
Minimum Spanning Tree using Prim’s Algorithm or Kruskal’s Algorithm.
Number of vertices: 4
Graph (Adjacency Matrix):
0100
1010
0101
0010
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define V 4
int main() {
int graph[V][V] = {
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1},
{0, 0, 1, 0}
};
if (isConnected(graph)) {
printf("The graph is connected.\n");
printf("Minimum Spanning Tree using Prim's Algorithm:\n");
primMST(graph);
} else {
printf("The graph is not connected. MST cannot be found.\n");
}
return 0;
}
B.Write a program to implement Huffman Coding for a given set of characters and
their frequencies.
Character and frequency pairs:
A: 5, B: 9, C: 12, D: 13, E: 16, F: 45
#include <stdio.h>
#include <stdlib.h>
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
int main() {
char arr[] = {'A', 'B', 'C', 'D', 'E', 'F'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Huffman Codes:\n");
HuffmanCodes(arr, freq, size);
return 0;
}
A. Find the Transitive closure of a given directed graph using Warshall’s algorithm.
#include <stdio.h>
#define V 4
printf("\n");
int main() {
int graph[V][V] = {
{0, 1, 0, 1},
{0, 0, 1, 0},
{1, 0, 0, 1},
{0, 0, 0, 0}
};
printMatrix(graph);
warshall(graph);
printMatrix(graph);
return 0;
#include <stdio.h>
#define V 4
#define INF 99999
int main() {
int graph[V][V] = {
{0, 15, INF, 7},
{5, 0, 50, INF},
{INF, 30, 0, 5},
{INF, INF, 15, 0}
};
floydWarshall(graph);
return 0;
}
6.Write a program to solve multiple instances of the 0/1 Knapsack problem using the
Dynamic Programming approach.
Test case 1:
Number of items: 4
Item Weights: [1, 3, 4, 5]
Item Values: [1, 4, 5, 7]
Knapsack Capacity: 7
Test case 2:
Number of items: 3
Item Weights: [2, 3, 4]
Item Values: [3, 4, 5]
Knapsack Capacity: 5
#include <stdio.h>
int main() {
// Test Case 1
int weights1[] = {1, 3, 4, 5};
int values1[] = {1, 4, 5, 7};
int capacity1 = 7;
int n1 = sizeof(weights1) / sizeof(weights1[0]);
// Test Case 2
int weights2[] = {2, 3, 4};
int values2[] = {3, 4, 5};
int capacity2 = 5;
int n2 = sizeof(weights2) / sizeof(weights2[0]);
return 0;
}
7.Print all the nodes reachable from a given starting node “B” in a given Undirected
graph using BFS, DFS method.
#include <stdio.h>
#include <stdlib.h>
int graph[MAX_NODES][MAX_NODES];
int visited[MAX_NODES];
int n;
void initializeVisited() {
visited[i] = 0;
queue[rear++] = start;
printf("BFS: ");
visited[i] = 1;
queue[rear++] = i;
printf("\n");
visited[start] = 1;
int main() {
n = 5;
graph[0][1] = graph[1][0] = 1;
graph[0][4] = graph[4][0] = 1;
graph[1][2] = graph[2][1] = 1;
graph[1][3] = graph[3][1] = 1;
graph[1][4] = graph[4][1] = 1;
graph[3][4] = graph[4][3] = 1;
initializeVisited();
BFS(1);
printf("DFS: ");
initializeVisited();
DFS(1);
printf("\n");
return 0;