Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views53 pages

ADA Lab Manual Jan-June 2025

The document outlines the laboratory assignment for the Analysis & Design of Algorithms course at Acropolis Institute of Technology and Research. It details the objectives, syllabus, and evaluation criteria for students, along with specific experiments to be conducted, such as binary search and sorting algorithms. The document emphasizes the importance of algorithm design and analysis in computer science education.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views53 pages

ADA Lab Manual Jan-June 2025

The document outlines the laboratory assignment for the Analysis & Design of Algorithms course at Acropolis Institute of Technology and Research. It details the objectives, syllabus, and evaluation criteria for students, along with specific experiments to be conducted, such as binary search and sorting algorithms. The document emphasizes the importance of algorithm design and analysis in computer science education.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 53

2024-25

Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To: Prof. Sumit Jain (Analysis & Design of Algorithms)

Analysis & Design of Algorithms (AL402)

Submitted By:
Aditya anant patil
0827AL231013
ALS-1/2nd Yr/ 4th Sem

[LAB ASSIGNMENT ADA (AL-402)]


The Objective of this laboratory work is to enlighten the student with knowledge base in
Designing the Algorithm and predict the behavior of an algorithm without implementing it on a
specific computer. Also demonstrate a working understanding of the determination of the
amount of time and space resources required to execute algorithm.

ACROPOLIS INSTITUTE OF TECHNOLOGY & RESEARCH,


INDORE
Department of CSE (Artificial Intelligence & Machine Learning)

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./

Ms. …………………………………B.TECH II year IV semester in the

Analysis & Design of Algorithms Laboratory of this institute during the

academic year 2024- 2025.

Signature of the Faculty


About the Laboratory

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

 Without Prior permission do not enter into the Laboratory.

 While entering into the LAB students should wear their ID cards.

 The Students should come with proper uniform.

 Students should sign in the LOGIN REGISTER before entering into the
laboratory.

 Students should come with observation and record note book to the laboratory.

 Students should maintain silence inside the laboratory.

 After completing the laboratory exercise, make sure to shutdown the system
properly.

 DONT’S

 Students bringing the bags inside the laboratory.

 Students using the computers in an improper way.

 Students scribbling on the desk and mishandling the chairs.

 Students using mobile phones inside the laboratory.

 Students making noise inside the laboratory.


SYLLABUS
Course: AL402 (Analysis & Design of Algorithms)
Branch/Year/Sem: CSE (Artificial Intelligence & Machine Learning) / II / IV

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

HARDWARE AND SOFTWARE REQUIREMENTS:

S. Name of Item Specification


No.
1 Computer System Hard Disk min 5 GB
RAM: 2 GB or higher
Processor: Intel i3 or above

S. Name of Item Specification


No.
1 Operating system Window 8 or higher version
Editor DevC++, TurboC++, Vscode or any other
compatible
RATIONALE:
The purpose of this subject is to cover the concepts of Analyzing and designing algorithms in terms
of Theory and Implementation .The syllabus provides all the essential concepts of ADA.

PREREQUISITE:-

 Basic Programming knowledge of data structure using c.


 Student familiar with the concept of data structure and sufficient background in discrete
mathematics.

COURSE OBJECTIVES AND OUTCOMES

 Course Objectives

1. To use of the concept of Data Structure & ADA in programming language.


2. To learn how to analyze a problem & design the solution for the problem.
3. To Implement Sorting, Searching, Huffman coding, Tree Traversal and graph Technique.
4. To analyze Lower Bound Theory and various problems.
5. To evaluate Tree, Graph and NP Problems

 Course Outcomes

At the end of the course student will be able to:

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

1 Write a program for Iterative and Recursive Binary


Search.
2 Sort a given set of elements using the Quick sort
method and determine the time required to sort the
elements.
3 Implement Merge sort algorithm to sort a given set of
elements and determine the time required to sort the
elements.
4 Find Minimum Cost Spanning Tree of any given
undirected graph using Prim’s algorithm.
5 Find Minimum Cost Spanning Tree of any sample
undirected graph using Kruskal’s algorithm.
6 Write a program for Strassen’s Matrix Multiplication.

7 Implement 0/1 Knapsack problem using Dynamic


Programming.
8 Implement All-Pairs Shortest Paths Problem using
Floyd's algorithm.
9 From a given vertex in a weighted connected graph,
find shortest paths to other vertices using Dijkstra’s
algorithm.
10 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
approximation.
11 Implement N Queen's problem using Back Tracking.

12 Implement optimal merge pattern and Huffman


coding

Program Outcome (PO)


The engineering graduate of this institute will demonstrate:
a) Apply knowledge of mathematics, science, computing and engineering fundamentals to computer
science engineering problems.
b) Able to identify, formulate, and demonstrate with excellent programming, and problem solving skills.
c) Design solutions for engineering problems including design of experiment and processes to meet
desired needs within reasonable constraints of manufacturability, sustainability, ecological,
intellectual and health and safety considerations.
d) Propose and develop effective investigational solution of complex problems using research
methodology; including design of experiment, analysis and interpretation of data, and combination of
information to provide suitable conclusion. synthesis
e) Ability to create, select and use the modern techniques and various tools to solve engineering
problems and to evaluate solutions with an understanding of the limitations.
f) Ability to acquire knowledge of contemporary issues to assess societal, health and safety, legal and
cultural issues.
g) Ability to evaluate the impact of engineering solutions on individual as well as organization in a
societal and environmental context, and recognize sustainable development, and will be aware of
emerging technologies and current professional issues.
h) Capability to possess leadership and managerial skills, and understand and commit to professional
ethics and responsibilities.
i) Ability to demonstrate the team work and function effectively as an individual, with an ability to
design, develop, test and debug the project, and will be able to work with a multi-disciplinary team.
j) Ability to communicate effectively on engineering problems with the community, such as being able
to write effective reports and design documentation.
k) Flexibility to feel the recognition of the need for, and have the ability to engage in independent and
life- long learning by professional development and quality enhancement programs in context of
technological change.
l) A practice of engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and entrepreneurship.
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Lab: Analysis & design of Group / Title: Iterative & Recursive binary search
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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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;

// Iterative Binary Search


int iterativeBinarySearch(int arr[], int size, int target) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // Target found, return index
} else if (arr[mid] > target) {
high = mid - 1; // Search left half
} else {
low = mid + 1; // Search right half
}
}
return -1; // Target not found
}

// Recursive Binary Search


int recursiveBinarySearch(int arr[], int low, int high, int target) {
if (low > high) {
return -1; // Base case: Target not found

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

cout << "Enter the target element to search: ";


cin >> target;

// Perform Iterative Binary Search


int iterResult = iterativeBinarySearch(arr, size, target);
cout << "\nIterative Binary Search Result: ";
if (iterResult != -1) {
cout << "Element found at index " << iterResult << endl;
} else {
cout << "Element not found" << endl;
}

// Perform Recursive Binary Search


int recResult = recursiveBinarySearch(arr, 0, size - 1, target);
cout << "Recursive Binary Search Result: ";
if (recResult != -1) {
cout << "Element found at index " << recResult << endl;
} else {
cout << "Element not found" << endl;
}

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

Sorted array: [2, 4, 6, 8, 10], Target: 7 Iterative: Element not found


Recursive: Element not found

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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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 partition(int arr[], int p, int r)


{
int x = arr[r]; // Pivot
int i = p - 1;
for (int j = p; j <= r - 1; j++)
{
if (arr[j] <= x)
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[r]);
return i + 1;
}

void quicksort(int arr[], int p, int r)


{
if (p < r)
{
int q = partition(arr, p, r);
quicksort(arr, p, q - 1);
quicksort(arr, q + 1, r);
}
}

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

EVALUATION RECORD Type/ Lab Session: Divide and Conquer


Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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

2. If the array has more than one element:

o Divide the array into two halves

o Recursively sort both halves

o Merge the two sorted halves

3. If the array has one or no element, it's already sorted

4. End

Time Complexity:

 Best case: O(n log n)


 Average case: O(n log n)

 Worst case: O(n log n)

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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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:

 Start from any vertex (say 0).


 Use a key array to track minimum edge weights.
 Use a parent array to store MST.
 Use mstSet[] to track vertices already included in MST.

 Iteration:

 Select the vertex not in MST with the smallest key value.
 Add it to MST.
 Update keys and parent for adjacent vertices.

 Repeat until all vertices are included in MST

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) .

EVALUATION RECORD Type/ Lab Session: Divide and Conquer


Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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 Theoretical solution of the instant problem


8.1 Algorithm
1. Sort all edges in the graph in non-decreasing order of their weights.
2. Initialize an empty MST.
3. Process each edge in sorted order:
• If adding the edge to the MST does not create a cycle, include it.
• Use a Union-Find data structure to detect cycles.
4. Repeat until the MST has V-1 edges, where V is the number of vertices.
5. Return the MST and the total cost.

8.2 Program
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Edge {
int src, dest, weight;
};

bool compare(Edge a, Edge b) {


return a.weight < b.weight;
}

int find(int parent[], int i) {


if (parent[i] == -1)
return i;
return find(parent, parent[i]);

Page 25
}

void unionSet(int parent[], int x, int y) {


int xset = find(parent, x);
int yset = find(parent, y);
parent[xset] = yset;
}

void kruskalMST(vector<Edge>& edges, int V) {


sort(edges.begin(), edges.end(), compare);

int* parent = new int[V];


fill_n(parent, V, -1);

cout << "Edges in the Minimum Cost Spanning Tree:\n";


int minCost = 0;

for (Edge e : edges) {


int x = find(parent, e.src);
int y = find(parent, e.dest);

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.

Acropolis Institute of Technology and Research, Indore


Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implementation of Strassen’s Matrix
Lab: Analysis & design of Multiplication for 2x2 Matrices
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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

Page 27
11 Title
Implementation of Strassen’s Matrix Multiplication for 2x2 Matrices
12 Neatly Drawn and labeled experimental setup

13 Theoretical solution of the instant problem


13.1 Algorithm
Input: Two 2x2 matrices A and B.
2. Divide: Break down the matrices into four 1x1 submatrices (scalars in this case):
• A = [[a, b], [c, d]]
• B = [[e, f], [g, h]]
3. Compute Seven Products (M1 to M7):
• M1 = (a + d) * (e + h)
• M2 = (c + d) * e
• M3 = a * (f - h)
• M4 = d * (g - e)
• M5 = (a + b) * h
• M6 = (c - a) * (e + f)
• M7 = (b - d) * (g + h)
4. Compute Result Submatrices:
• C[0][0] = M1 + M4 - M5 + M7
• C[0][1] = M3 + M5
• C[1][0] = M2 + M4
• C[1][1] = M1 - M2 + M3 + M6
5. Output: The resulting 2x2 matrix C.

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

cout << "Resultant Matrix C is:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}

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.

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)

EVALUATION RECORD Type/ Lab Session: Divide and Conquer


Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

Title
Page 30
Implementation of the 0/1 Knapsack Problem Using Dynamic Programming
.
Neatly Drawn and labeled experimental setup

Theoretical solution of the instant problem


Algorithm

 Initialization:

1.Create a 2D DP table of size (n+1) x (W+1)


2.Initialize the first row and column with 0 (base case)
3.For each item i from 1 to n and capacity w from 1 to W:
•If weight[i-1] <= w:
dp[i][w] = max(dp[i-1][w], value[i-1] + dp[i-1][w - weight[i-1]])
•Else:
dp[i][w] = dp[i-1][w]
4.Final answer is dp[n][W] — maximum value that can be carried

Program
#include <iostream>
#include <vector>
using namespace std;

int knapsack(int W, vector<int>& wt, vector<int>& val, int n) {


vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

for (int i = 1; i <= n; i++) {


for (int w = 1; w <= W; w++) {
if (wt[i - 1] <= w) {

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();

int maxProfit = knapsack(capacity, weights, values, n);


cout << "Maximum value in Knapsack = " << maxProfit << endl;

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)

EVALUATION RECORD Type/ Lab Session: Divide and Conquer


Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

16 Title
Implementation of the 0/1 Knapsack Problem Using Dynamic Programming

Page 33
17 Neatly Drawn and labeled experimental setup

18 Theoretical solution of the instant problem


18.1 Algorithm
Steps:
1.Initialize the distance matrix with given edge weights.
2.For each vertex k (intermediate node):
•For each pair of vertices (i, j):
•Update dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
3.After all iterations, dist[i][j] contains the shortest distance from vertex i to
vertex j.

18.2 Program
#include <iostream>
#include <vector>
using namespace std;

#define INF 9999


#define V 4

void floydWarshall(int graph[V][V]) {


int dist[V][V];

// Initialize distance matrix


for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

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

// Print final distance matrix


cout << "Shortest distances between every pair of vertices:\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

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.

Acropolis Institute of Technology and Research, Indore


Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title From a given vertex in a weighted connected
graph, find shortest paths to other vertices using Dijkstra’s
Lab: Analysis & design of
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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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:

 dist[] to hold shortest distances (set to ∞ except source)


 visited[] to track processed vertices (set to false)

Set distance of source to 0.

Repeat for all vertices:

 Pick unvisited vertex with the smallest distance (u)


 Mark u as visited
 Update distance of adjacent vertices v if:
o There’s an edge from u to v
o v is not visited
o New path is shorter than current dist[v]

 After loop, dist[] holds shortest distances from source to all other vertices.

3.2 Program
#include <iostream>
#include <limits.h>
using namespace std;

#define MAX 100

int minDistance(int dist[], bool visited[], int n) {

Page 37
int min = INT_MAX, min_index;

for (int v = 0; v < n; v++)


if (!visited[v] && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

void dijkstra(int graph[MAX][MAX], int n, int src) {


int dist[MAX];
bool visited[MAX];

for (int i = 0; i < n; i++)


dist[i] = INT_MAX, visited[i] = false;

dist[src] = 0;

for (int count = 0; count < n - 1; count++) {


int u = minDistance(dist, visited, n);
visited[u] = true;

for (int v = 0; v < n; v++)


if (!visited[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

cout << "\nVertex\tDistance from Source\n";


for (int i = 0; i < n; i++)
cout << i << "\t" << dist[i] << "\n";
}

int main() {
int graph[MAX][MAX], n, src;

cout << "Enter number of vertices: ";


cin >> n;

cout << "Enter adjacency matrix (0 if no edge):\n";


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> graph[i][j];

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.

EVALUATION RECORD Type/ Lab Session: Divide and Conquer


Name Aditya Anant Patil Enrollment No. 0827AL231013
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

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

8 Theoretical solution of the instant problem


8.1 Algorithm

Exact Algorithm (Brute-force using Backtracking / Permutations)

 Generate all possible permutations of the cities (except the starting one).
 Calculate the cost for each tour.

 Return the tour with the minimum total cost.

Approximation Algorithm (Nearest Neighbor Heuristic)

 Start at a chosen city.


 Repeatedly visit the nearest unvisited city.

 Return to the starting city once all cities are visited.

 Cost is not guaranteed to be optimal.

Error Calculation:

Program

#include <iostream>
#include <algorithm>
using namespace std;
Page 41
#define MAX 10
#define INF 9999

int tspBruteForce(int graph[MAX][MAX], int n, int bestPath[MAX]) {


int cities[MAX];
for (int i = 1; i < n; i++)
cities[i - 1] = i;

int minCost = INF;

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

if (currentCost < minCost) {


minCost = currentCost;
bestPath[0] = 0;
for (int i = 0; i < n - 1; i++)
bestPath[i + 1] = cities[i];
bestPath[n] = 0;
}
} while (next_permutation(cities, cities + n - 1));

return minCost;
}

int tspNearestNeighbor(int graph[MAX][MAX], int n, int path[MAX]) {


bool visited[MAX] = {false};
int cost = 0, current = 0;
visited[0] = true;
path[0] = 0;

for (int i = 1; i < n; i++) {


int nextCity = -1, minDist = INF;
for (int j = 0; j < n; j++) {
if (!visited[j] && graph[current][j] < minDist) {
minDist = graph[current][j];

Page 42
nextCity = j;
}
}
visited[nextCity] = true;
path[i] = nextCity;
cost += minDist;
current = nextCity;
}

path[n] = 0; // return to start


cost += graph[current][0];
return cost;
}

int main() {
int n;
int graph[MAX][MAX];
int bestPath[MAX], approxPath[MAX];

cout << "Enter number of cities: ";


cin >> n;

cout << "Enter distance matrix:\n";


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> graph[i][j];

int optimalCost = tspBruteForce(graph, n, bestPath);


int approxCost = tspNearestNeighbor(graph, n, approxPath);

float error = ((float)(approxCost - optimalCost) / optimalCost) * 100.0;

cout << "\nOptimal TSP Path (Brute-force): ";


for (int i = 0; i <= n; i++)
cout << bestPath[i] << " ";
cout << "\nCost: " << optimalCost;

cout << "\n\nApprox TSP Path (Nearest Neighbor): ";


for (int i = 0; i <= n; i++)
cout << approxPath[i] << " ";
cout << "\nCost: " << approxCost;

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

Error in approximation = 26.6055%

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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title: Implement N Queen's problem using Back Tracking.


2 Neatly Drawn and labeled experimental setup:

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)

If row == N → a complete solution is found; print / count it.

Else iterate col = 0 … N-1

If placing a queen at (row, col) is safe (no other queen in same


column or diagonals),

Assign pos[row] = col, call solve(row + 1).

3 Safety test (for all previous rows i < row) is true when:

pos[i] != col (no column clash)

abs(pos[i] – col) != row – i (main or anti-diagonal clash).

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;

bool isSafe(int row, int col) {


for (int i = 0; i < row; ++i) {
if (pos[i] == col) return false;
if (pos[i] - i == col - row) return false;
if (pos[i] + i == col + row) return false;
}
return true;
}

void solve(int row) {


if (row == N) {
++solutionCount;
cout << "\nSolution " << solutionCount << ":\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j)
cout << (pos[i] == j ? " Q" : " .");
cout << '\n';
}
return;
}
for (int col = 0; col < N; ++col) {
if (isSafe(row, col)) {
pos[row] = col;
solve(row + 1);
}
}
}

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..

Total number of solutions for N = 4 : 2


Results: Back-tracking systematically explores placements, pruning unsafe options, and thereby
enumerates all valid N-Queen solutions with manageable memory usage.

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

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title: Implement optimal merge pattern and Huffman coding


2 Neatly Drawn and labeled experimental setup:

Page 49
3 Theoretical solution of the instant problem:
3.1 Algorithm:

Optimal Merge Pattern:

 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:

 Each character is treated as a leaf node with its frequency.


 Build a binary tree using a greedy approach:
o Repeatedly extract two nodes with the lowest frequencies.
o Combine them into a new node with cumulative frequency.
o Repeat until one root node remains.
 Traverse the tree to assign codes (0 for left, 1 for right).

3.2 Program:
#include <iostream>
using namespace std;

#define MAX 100

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

Node* createNode(char ch, int freq) {


Node* node = new Node;
node->ch = ch;
node->freq = freq;
node->left = node->right = NULL;
return node;
}

void insertMinHeap(Node* heap[], int &size, Node* node) {


heap[size++] = node;
for (int i = size - 1; i > 0 && heap[i]->freq < heap[i - 1]->freq; i--) {
Node* temp = heap[i]; heap[i] = heap[i - 1]; heap[i - 1] = temp;
}
}

Node* extractMin(Node* heap[], int &size) {


Node* min = heap[0];
for (int i = 1; i < size; i++) heap[i - 1] = heap[i];
size--;
return min;

Page 51
}

void printCodes(Node* root, char code[], int depth) {


if (root->left == NULL && root->right == NULL) {
code[depth] = '\0';
cout << root->ch << ": ";
for (int i = 0; code[i] != '\0'; i++) cout << code[i];
cout << '\n';
return;
}
if (root->left) {
code[depth] = '0';
printCodes(root->left, code, depth + 1);
}
if (root->right) {
code[depth] = '1';
printCodes(root->right, code, depth + 1);
}
}

void buildHuffman(char chars[], int freq[], int n) {


Node* heap[MAX];
int size = 0;
for (int i = 0; i < n; i++)
insertMinHeap(heap, size, createNode(chars[i], freq[i]));
while (size > 1) {
Node* left = extractMin(heap, size);
Node* right = extractMin(heap, size);
Node* merged = createNode('-', left->freq + right->freq);
merged->left = left;
merged->right = right;
insertMinHeap(heap, size, merged);
}
Node* root = heap[0];
char code[MAX];
printCodes(root, code, 0);
}

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

You might also like