ADA Lab Manual Jan-June 2025
ADA Lab Manual Jan-June 2025
Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To: Prof. Sumit Jain (Analysis & Design of Algorithms)
Submitted By:
Aditya anant patil
0827AL231013
ALS-1/2nd Yr/ 4th Sem
CERTIFICATE
This is to certify that the experimental work entered in this journal as per
the B. TECH. II year syllabus prescribed by the RGPV was done by Mr./
In this lab, students will be able to build solid foundation in algorithms and their applications.
Students can expand their skill set by learning and solving basic problems of divide and conquer
technique. In this laboratory students will develop and code program for algorithms and analyze it to
determine its computational complexity. In this students can model given engineering problem using
different data structures and apply algorithm designing to write the corresponding algorithm to solve
the problems. With the help of this laboratory students can strengthen the ability to identify and
apply the suitable algorithm for the given real world problems. This lab is an overview of how to
design algorithm and apply the same for any given problem.
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES
DO’S
While entering into the LAB students should wear their ID cards.
Students should sign in the LOGIN REGISTER before entering into the
laboratory.
Students should come with observation and record note book to the laboratory.
After completing the laboratory exercise, make sure to shutdown the system
properly.
DONT’S
Module1: Definitions of algorithms and complexity, Time and Space Complexity; Time space
tradeoff, various bounds on complexity, Asymptotic notation, Recurrences and Recurrences solving
techniques, Introduction to divide and conquer technique, example: binary search, merge sort, quick
sort, heap sort, Strassen’s matrix multiplication etc, Code tuning techniques: Loop Optimization,
Data Transfer Optimization, Logic Optimization, etc.
Module2: Study of Greedy strategy, examples of greedy method like optimal merge patterns,
Huffman coding, minimum spanning trees, knapsack problem, job sequencing with deadlines,
single source shortest path algorithm etc. Correctness proof of Greedy algorithms.
Module3: Concept of dynamic programming, problems based on this approach such as 0/1
knapsack, multistage graph, reliability design, Floyd-Warshall algorithm etc.
Module4: Backtracking concept and its examples like 8 queen’s problem, Hamiltonian cycle,
Graph colouring problem etc. Introduction to branch & bound method, examples of branch and
bound method like travelling salesman problem etc. Meaning of lower bound theory and its use in
solving algebraic problem, introduction to parallel algorithms.
Module5: Advanced tree and graph algorithms, NP-hard and NP-complete problems,
Approximations Algorithms, Data Stream Algorithms, Introduction to design and complexity of
Parallel Algorithms
PREREQUISITE:-
Course Objectives
Course Outcomes
1. Able to perform Sorting, searching, and analyze the problem and apply divide and
conquer techniques.
2. To analyze the problem and apply greedy methods.
3. To apply dynamic programming on various problems like multistage graph, knapsack
problem etc
4. To apply backtracking and branch and bound concept to various conceptual problems
and use the concept of lower bound theory to solve algebraic problems.
5. To perform efficient traversing on data structure like tree and graph and students also
able to differentiate between NP-hard and NP-completeness problem.
Index
Date of Page Date of Grade & Sign
S.No Exp. Name of the Experiment No. Submission of the Faculty
Additional remarks
Tutor
1 Title
Program for Iterative and Recursive Binary Search
2 Neatly Drawn and labeled experimental setup
3 Theoretical solution of the instant problem
3.1 Algorithm
1. Start with a sorted array, a target element to search, and initialize two pointers: low (start of the
array, index 0) and high (end of the array, index n-1, where n is the array length).
2. While low is less than or equal to high:
• Calculate the middle index: mid = (low + high) / 2.
• If the element at mid is equal to the target, return mid (index of the target).
• If the element at mid is greater than the target, set high = mid - 1 (search in the left half).
• If the element at mid is less than the target, set low = mid + 1 (search in the right half).
3. If the loop ends without finding the element, return -1 (element not found).
Recursive:
1. Base case: If low is greater than high, return -1 (element not found).
2. Calculate the middle index: mid = (low + high) / 2.
3. If the element at mid is equal to the target, return mid (index of the target).
4. If the element at mid is greater than the target, recursively call the function with the
left half: low and high = mid - 1.
5. If the element at mid is less than the target, recursively call the function with the
right half: low = mid + 1 and high.
6. Return the result of the recursive call.
3.2 Program
#include <iostream>
using namespace std;
Page 10
}
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // Target found, return index
} else if (arr[mid] > target) {
return recursiveBinarySearch(arr, low, mid - 1, target); // Search left half
} else {
return recursiveBinarySearch(arr, mid + 1, high, target); // Search right half
}
}
int main() {
int size, target;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " elements in sorted order (ascending): ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
return 0;
}
Page 11
4 Tabulation Sheet
5
INPUT OUTPUT
Sorted array: [2, 4, 6, 8, 10], Target: 6
Iterative: Element found at index 2
Recursive: Element found at index 2
Results
• Test Case 1 (Target = 6): Both Iterative and Recursive Binary Search
theoretically find the element at index 2 because the target 6 matches the
element at arr[2].
• Test Case 2 (Target = 7): Both Iterative and Recursive Binary Search
theoretically return “Element not found” because 7 is not present in the array.
The search space is repeatedly halved until no further division is possible (low
> high).
Page 12
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Lab: Analysis & design of Group / Title: Sort a given set of elements using the Quick sort
algorithm (AL402) method and determine the time required to sort the elements.
EVALUATION RECORD Type/ Lab Session: Divide and Conquer
Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular
Additional remarks
Tutor
1 Title
Sort a given set of elements using the Quick sort method and determine the time required to sort
the elements.
2 Neatly Drawn and labeled experimental setup
Page 13
3 Theoretical solution of the instant problem
3.1 Algorithm
1. Choose a pivot element from the array (e.g., the last element).
2. Partition the array around the pivot:
a. Rearrange elements so that all elements less than the pivot are on the left, and all
elements greater than the pivot are on the right.
b. Place the pivot in its correct position (the partition index).
3. Recursively apply Quick Sort to the subarray on the left of the pivot (elements less than the
pivot).
4. Recursively apply Quick Sort to the subarray on the right of the pivot (elements greater than
the pivot).
5. Base case: If the subarray has 1 or fewer elements, it is already sorted (no further action
needed).
3.2 Program
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {50, 65, 80, 70, 42, 11, 90, 1, 5, 10};
int n = 10;
Page 14
quicksort(arr, 0, n - 1);
cout << "Sorted array= ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
4 Tabulation Sheet
5
INPUT OUTPUT
Unsorted array: [50, 65, 80, 70, 42, 11,
90, 1, 5, 10] Sorted array: [1, 5, 10, 11, 42, 50, 65, 70,
80, 90]
Results
The Quick Sort algorithm uses the last element as the pivot, partitions the array
(smaller elements left, larger right), and recursively sorts subarrays. For the input
[50, 65, 80, 70, 42, 11, 90, 1, 5, 10], it sorts to [1, 5, 10, 11, 42, 50, 65, 70, 80, 90],
as expected.
Page 15
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implement Merge sort algorithm to sort a given
Lab: Analysis & design of set of elements and determine the time required to sort the
algorithm (AL402) elements
Additional remarks
Tutor
1 Title
Implement Merge sort algorithm to sort a given set of elements and determine the time
required to sort the elements
2 Neatly Drawn and labeled experimental setup
Page 16
3 Theoretical solution of the instant problem
3.1 Algorithm
1. Start
4. End
Time Complexity:
3.2 Program
#include <iostream>
using namespace std;
void merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], R[n2];
for(int i=0; i<n1; i++)
{ L[i] = arr[p + i]; }
for(int j=0; j<n2; j++)
{ R[j] = arr[q + 1 + j]; }
int i=0, j=0;
int k = p;
Page 17
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 merge_sort(int arr[], int p, int r)
{
if (p < r)
{
int q = (p + r) / 2;
merge_sort(arr, p, q);
merge_sort(arr, q+1, r);
merge(arr, p, q, r);
}
}
int main()
{
int arr[] = {5, 6, 8, 7, 4, 1, 9, 11, 2, 10};
int n = 10;
merge_sort(arr, 0, n-1);
cout<<"Sorted array= ";
for (int i=0; i<n; i++)
{ cout<<arr[i]<<" "; }
return 0;
}
}
Page 18
4 Tabulation Sheet
5
INPUT OUTPUT
int arr[] = {5, 6, 8, 7, 4, 1, 9, 11, 2, 10};
Sorted array= 1 2 4 5 6 7 8 9 10 11
Results
Merge Sort efficiently sorts an array using a divide-and-conquer strategy with consistent time
complexity of O(n log n), making it suitable for large datasets.
Page 19
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Find Minimum Cost Spanning Tree of any given
Lab: Analysis & design of
undirected graph using Prim’s algorithm.
algorithm (AL402)
EVALUATION RECORD Type/ Lab Session: Divide and Conquer
Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular
Additional remarks
Tutor
1 Title
Find Minimum Cost Spanning Tree of any given undirected graph using Prim’s algorithm.
2 Neatly Drawn and labeled experimental setup
Page 20
3 Theoretical solution of the instant problem
3.1 Algorithm
Initialization:
Iteration:
Select the vertex not in MST with the smallest key value.
Add it to MST.
Update keys and parent for adjacent vertices.
3.2 Program
#include<iostream>
#include <limits.h>
using namespace std;
#define N 5
int minkey(int key[], int mstset[])
{
int min = INT_MAX, minindex = -1;
for (int v = 0; v < N; v++)
Page 21
{
if (mstset[v] == 0 && key[v] < min)
{
min = key[v];
minindex = v;
}
}
return minindex;
}
void printMST(int parent[], int graph[N][N])
{
int cost = 0;
cout<<"Edge \tWeight\n";
for (int i = 1; i < N; i++)
{
cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n";
cost += graph[i][parent[i]];
}
cout<<"Total cost of MST= "<<cost<<endl;
}
void prim(int graph[N][N])
{
int parent[N];
int key[N];
int mstset[N];
for (int i = 0; i < N; i++)
{
key[i] = INT_MAX;
mstset[i] = 0;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < N - 1; count++)
{
int u = minkey(key, mstset);
mstset[u] = 1;
for (int v = 0; v < N; v++)
{
if (graph[u][v] && mstset[v] == 0 && graph[u][v] < key[v])
{
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printMST(parent, graph);
Page 22
}
int main()
{
int graph[N][N] = {
{0, 4, 0, 8, 0},
{4, 0, 2, 6, 5},
{0, 2, 0, 3, 7},
{8, 6, 3, 0, 1},
{0, 5, 7, 1, 0}
};
prim(graph);
return 0;
}
4 Tabulation Sheet
5
INPUT OUTPUT
{
{0, 4, 0, 8, 0}, Output
{4, 0, 2, 6, 5}, Edge Weight
{0, 2, 0, 3, 7}, 0-1 4
1-2 2
{8, 6, 3, 0, 1},
2-3 3
{0, 5, 7, 1, 0} 3-4 1
};
Total cost of MST = 10
Results
This experiment successfully demonstrated how Prim’s algorithm efficiently finds the
minimum cost spanning tree of an undirected graph by progressively selecting the least
weighted edges without forming cycles.
Page 23
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Find the Minimum Cost Spanning Tree of
Lab: Analysis & design of a Sample Undirected Graph Using Kruskal’s Algorithm
algorithm (AL402) .
Additional remarks
Tutor
6 Title
Find the Minimum Cost Spanning Tree of a Sample Undirected Graph Using
Kruskal’s Algorithm
Page 24
7 Neatly Drawn and labeled experimental setup
8.2 Program
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int src, dest, weight;
};
Page 25
}
if (x != y) {
cout << char('A' + e.src) << " - " << char('A' + e.dest) << " : " <<
e.weight << "\n";
minCost += e.weight;
unionSet(parent, x, y);
}
}
cout << "Minimum Cost = " << minCost << "\n";
delete[] parent;
}
int main() {
int V = 5; // A, B, C, D, E -> 0,1,2,3,4
vector<Edge> edges = {
{0, 1, 4}, {0, 2, 3}, {0, 3, 1},
{1, 4, 2}, {2, 4, 5}, {3, 4, 6}
};
kruskalMST(edges, V);
return 0;
}
9 Tabulation Sheet
Page 26
10 Re
INPUT OUTPUT
AB(4), AC(3), AD(1), su
BE(2), CE(5), DE(6) AD(1), BE(2), AC(3), AB(4) → lts
Cost = 10
Kruskal’s algorithm was used to find the Minimum Cost Spanning Tree. The MST includes edges AD,
BE, AC, and AB with a total cost of 10.
Additional remarks
Tutor
Page 27
11 Title
Implementation of Strassen’s Matrix Multiplication for 2x2 Matrices
12 Neatly Drawn and labeled experimental setup
13.2 Program
#include <iostream>
using namespace std;
Page 28
void strassen(int A[2][2], int B[2][2], int C[2][2]) {
int M1 = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1]);
int M2 = (A[1][0] + A[1][1]) * B[0][0];
int M3 = A[0][0] * (B[0][1] - B[1][1]);
int M4 = A[1][1] * (B[1][0] - B[0][0]);
int M5 = (A[0][0] + A[0][1]) * B[1][1];
int M6 = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1]);
int M7 = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1]);
C[0][0] = M1 + M4 - M5 + M7;
C[0][1] = M3 + M5;
C[1][0] = M2 + M4;
C[1][1] = M1 - M2 + M3 + M6;
}
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
strassen(A, B, C);
return 0;
}
14 Tabulation Sheet
15 Re
INPUT OUTPUT
12 56 su
34 78 19 22 lts
43 50
Page 29
Strassen’s algorithm reduces the number of multiplications required in matrix
multiplication from 8 to 7, improving computational efficiency.
It applies the divide-and-conquer approach to compute the product of matrices
more effectively.
Additional remarks
Tutor
Title
Page 30
Implementation of the 0/1 Knapsack Problem Using Dynamic Programming
.
Neatly Drawn and labeled experimental setup
Initialization:
Program
#include <iostream>
#include <vector>
using namespace std;
Page 31
dp[i][w] = max(dp[i - 1][w], val[i - 1] + dp[i - 1][w - wt[i - 1]]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
vector<int> values = {15, 20, 30};
vector<int> weights = {1, 3, 4};
int capacity = 4;
int n = values.size();
return 0;
}
Tabulation Sheet
INPUT OUTPUT
values = {15, 20, 30}
weights = {1, 3, 4} Maximum value in Knapsack = 35
capacity = 4
Results
The 0/1 Knapsack problem was solved using dynamic programming by filling a DP table that stores
maximum value for each subproblem. This approach avoids recomputation and improves efficiency
compared to recursion.
Page 32
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Implementation of the 0/1
Group / Title:
Lab: Analysis & design of Knapsack Problem Using Dynamic Programming
algorithm (AL402)
Additional remarks
Tutor
16 Title
Implementation of the 0/1 Knapsack Problem Using Dynamic Programming
Page 33
17 Neatly Drawn and labeled experimental setup
18.2 Program
#include <iostream>
#include <vector>
using namespace std;
// Floyd-Warshall algorithm
Page 34
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
int main() {
int graph[V][V] = {
{0, 3, INF, 7},
{8, 0, 1, INF},
{5, INF, 0, 2},
{2, INF, INF, 0}
};
floydWarshall(graph);
return 0;
}
19 Tabulation Sheet
20 Re
INPUT OUTPUT
{ su
{0, 3, INF, 7}, Shortest distances between every pair of lts
{8, 0, 1, INF}, vertices:
{5, INF, 0, 2}, 0346
3013
{2, INF, INF, 0}
5802
} 2560
Page 35
Floyd’s algorithm efficiently computes the shortest paths between all pairs of
vertices in a weighted graph by using dynamic programming. It allows for
intermediate vertices and updates paths iteratively for optimal results.
Additional remarks
Tutor
1 Title
From a given vertex in a weighted connected graph, find shortest paths to other vertices using
Dijkstra’s algorithm
2 Neatly Drawn and labeled experimental setup
Page 36
3 Theoretical solution of the instant problem
3.1 Algorithm
Initialize:
After loop, dist[] holds shortest distances from source to all other vertices.
3.2 Program
#include <iostream>
#include <limits.h>
using namespace std;
Page 37
int min = INT_MAX, min_index;
return min_index;
}
dist[src] = 0;
int main() {
int graph[MAX][MAX], n, src;
cout << "Enter source vertex (0 to " << n - 1 << "): ";
cin >> src;
Page 38
dijkstra(graph, n, src);
return 0;
}
4 Tabulation Sheet
5
INPUT OUTPUT
Enter number of vertices: 4
Enter adjacency matrix (0 if no edge): Vertex Distance from Source
1257 0 1
0215 1 3
1309 2 0
9023 3 8
Enter source vertex (0 to 3): 2
Results
Dijkstra’s algorithm efficiently computes the shortest paths from a single source vertex to all other
vertices in a weighted graph using a greedy approach.
Page 39
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implement any scheme to find the optimal solution
for the Traveling Sales Person problem and then solve the same
Lab: Analysis & design of
problem instance using any approximation algorithm and
algorithm (AL402) determine the error in the approximation.
Additional remarks
Tutor
6 Title
Implement any scheme to find the optimal solution for the Traveling Sales Person problem and then
solve the same problem instance using any approximation algorithm and determine the error in the
Page 40
approximation.
7 Neatly Drawn and labeled experimental setup
Generate all possible permutations of the cities (except the starting one).
Calculate the cost for each tour.
Error Calculation:
Program
#include <iostream>
#include <algorithm>
using namespace std;
Page 41
#define MAX 10
#define INF 9999
do {
int currentCost = 0, k = 0;
for (int i = 0; i < n - 1; i++) {
currentCost += graph[k][cities[i]];
k = cities[i];
}
currentCost += graph[k][0]; // return to start
return minCost;
}
Page 42
nextCity = j;
}
}
visited[nextCity] = true;
path[i] = nextCity;
cost += minDist;
current = nextCity;
}
int main() {
int n;
int graph[MAX][MAX];
int bestPath[MAX], approxPath[MAX];
Page 43
cout << "\n\nError in approximation = " << error << "%\n";
return 0;
}
Tabulation Sheet:
INPUT OUTPUT
Enter number of cities: 4
Enter distance matrix: Optimal TSP Path (Brute-force): 0 3 1 2 0
121 209 56 78 Cost: 327
201 115 79 102
111 451 120 98 Approx TSP Path (Nearest Neighbor): 0 2
98 59 24 181 310
Cost: 414
Results: This experiment shows that exact algorithms provide optimal TSP
solutions, while heuristic methods like Nearest Neighbor offer faster but
potentially less accurate results, allowing quantifiable error analysis.
Page 44
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Lab: Analysis & design of Group / Title: Implement N Queen's problem using Back
algorithm (AL402) Tracking.
EVALUATION RECORD Type/ Lab Session: Divide and Conquer
Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular
Additional remarks
Tutor
Page 45
3 Theoretical solution of the instant problem:
3.1 Algorithm:
1 Represent the board with one integer array pos[N] where pos[row] = col keeps the
column index of the queen placed in that row.
2 Recursive function solve(row)
3 Safety test (for all previous rows i < row) is true when:
4 All recursion paths systematically explore and back-track, guaranteeing enumeration of every
legal placement.
Page 46
Time complexity: O(N!) in the worst case (vastly pruned by safety checks).
Space complexity: O(N) for the position array and recursion stack.
4.1 Program:
#include <iostream>
using namespace std;
#define MAX 12
int N;
int pos[MAX];
long long solutionCount = 0;
int main() {
cout << "Enter value of N (4–" << MAX << "): ";
cin >> N;
Page 47
if (N < 4 || N > MAX) {
cout << "Invalid N.\n";
return 0;
}
solve(0);
cout << "\nTotal number of solutions for N = " << N << " : "
<< solutionCount << '\n';
return 0;
}
5 Tabulation Sheet:
INPUT OUTPUT
Enter value of N (4–12): 4
Solution 1:
.Q..
...Q
Q...
..Q.
Solution 2:
..Q.
Q...
...Q
.Q..
Page 48
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Lab: Analysis & design of Group / Title: Implement optimal merge pattern and Huffman
algorithm (AL402) coding
EVALUATION RECORD Type/ Lab Session: Divide and Conquer
Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular
Additional remarks
Tutor
Page 49
3 Theoretical solution of the instant problem:
3.1 Algorithm:
Given n files of different sizes, repeatedly merge the two smallest files.
Keep adding their combined size to the total cost.
Repeat until only one file remains.
This simulates the minimum cost of sequential file merges.
Huffman Coding:
3.2 Program:
#include <iostream>
using namespace std;
Page 50
void optimalMerge(int arr[], int n) {
int totalCost = 0;
for (int i = 0; i < n - 1; i++) {
int min1 = -1, min2 = -1;
for (int j = 0; j < n; j++) {
if (arr[j] == -1) continue;
if (min1 == -1 || arr[j] < arr[min1]) min1 = j;
}
for (int j = 0; j < n; j++) {
if (arr[j] == -1 || j == min1) continue;
if (min2 == -1 || arr[j] < arr[min2]) min2 = j;
}
int merged = arr[min1] + arr[min2];
totalCost += merged;
arr[min1] = merged;
arr[min2] = -1;
}
cout << "Total Merge Cost: " << totalCost << "\n";
}
struct Node {
char ch;
int freq;
Node *left, *right;
};
Page 51
}
int main() {
int n;
cout << "Enter number of files: ";
cin >> n;
int sizes[MAX];
cout << "Enter sizes of files:\n";
for (int i = 0; i < n; i++)
cin >> sizes[i];
Page 52
optimalMerge(sizes, n);
cout << "\nEnter number of characters for Huffman: ";
cin >> n;
char chars[MAX];
int freq[MAX];
cout << "Enter characters:\n";
for (int i = 0; i < n; i++)
cin >> chars[i];
cout << "Enter frequencies:\n";
for (int i = 0; i < n; i++)
cin >> freq[i];
cout << "\nHuffman Codes:\n";
buildHuffman(chars, freq, n);
return 0;
}
4 Tabulation Sheet:
5
INPUT OUTPUT
Enter number of files: 4
Enter sizes of files: Total Merge Cost: 185
21 13 41 21 Huffman Codes:
Enter number of characters for A: 000
Huffman: 4 B: 001
Enter characters: C: 01
ABCD D: 1
Enter frequencies:
1 9 12 34
Results:
This experiment demonstrates how greedy algorithms can minimize merge costs and efficiently
encode data using Huffman trees.
Page 53