DAA Lab Programs
DAA Lab Programs
Version : 1
Prepared by : [Link]
1
Table of Contents
2
Rubrics for 22ISC41 – Design and Analysis of Algorithms
Completion on time (10) 15 Completed on time Completed with Very late submission
short delay
Viva - 15 marks
Viva (15) 15 Answered all the viva Answered few Subject knowledge is
questions and good questions and subject poor
subject knowledge knowledge to be
improved
3
Ex:No:1
Exploration of UCI Repository Dataset Using WEKA Tool
Date:
Aim
● To explore and analyze a dataset from the UCI Machine Learning Repository using the
WEKA tool and study its attributes and instances.
Procedure
The exploration of datasets from the UCI Machine Learning Repository is carried out using the
WEKA tool. This process helps to understand the structure of the dataset, including attributes,
instances, and class labels. It is implemented using the following steps.
● Download a dataset from the UCI Machine Learning Repository in .arff or .csv format.
● Open the WEKA tool and select the Explorer interface from the WEKA GUI Chooser.
● Load the dataset by clicking the Open File option in the Preprocess tab.
● Observe the dataset information such as number of attributes, number of instances, and
attribute types displayed in the WEKA Explorer window.
● Use the Visualize option to analyze the distribution of data and relationships between
attributes.
SAMPLE PROGRAM
//Program – Chi-Square Feature Selection using WEKA
# start JVM
[Link]()
# load dataset
loader = Loader(classname="[Link]")
data = loader.load_file("[Link]")
data.class_is_last()
# chi-square evaluator
evaluator = ASEvaluation(classname="[Link]")
4
# ranker search method
search = ASSearch(classname="[Link]")
# print result
print(attsel.results_string)
# stop JVM
[Link]()
OUTPUT
Ranked attributes:
0.91 MemoryLoss
0.88 Confusion
0.80 LanguageProblem
0.45 Age
EXERCISE PROBLEMS
1. Load a dataset in WEKA and view its summary statistics (attributes, instances, missing
values).
2. Load a dataset in WEKA and apply the RemoveDuplicates to identify and eliminate
duplicate records from the dataset. Compare the dataset before and after removing
duplicate instances
VIVA QUESTIONS
1. What is WEKA, and what are its main features?
2. What types of data formats can WEKA work with?
3. How do you load a dataset in WEKA and view its summary information?
4. What are filters in WEKA? Give examples and explain their purpose.
5
Ex:No:2 Data manipulation using NumPy, pandas and visualization using
Date: matplotlib.
Aim
● To perform data manipulation using NumPy and Pandas and visualize the data using
Matplotlib.
Procedure
1. Import the required Python libraries such as NumPy for numerical operations, Pandas for
data manipulation, and Matplotlib for data visualization.
2. Upload the given CSV dataset (oasis_cross-[Link]) into Google Colab and load it
using the pandas.read_csv() function.
3. Display the first few rows of the dataset using head() and view dataset information using
info() to understand the structure, column names, and data types.
4. Obtain the statistical summary of the dataset using describe() and check for missing or null
values using isnull().sum().
5. Handle missing values by replacing them with the mean of the corresponding numeric
columns using fillna().
6. Convert the Age column into a NumPy array and perform numerical operations such as
mean, maximum, minimum, and standard deviation.
7. Perform data manipulation operations such as selecting specific columns, filtering records
based on age, sorting the dataset, and creating a new column (Age_Group).
8. Visualize the data using Matplotlib by plotting a histogram for age distribution, a scatter
plot for Age vs MMSE, and a bar chart for age group distribution.
SAMPLE PROGRAM
import numpy as np
import pandas as pd
import [Link] as plt
df = pd.read_csv("/content/oasis_cross-[Link]")
print("Dataset Loaded Successfully\n")
6
print([Link]())
print("\nDataset Information:")
print([Link]())
print("\nStatistical Summary:")
print([Link]())
print("\nChecking Missing Values:")
print([Link]().sum())
[Link]([Link](numeric_only=True), inplace=True)
print("\nMissing values handled successfully")
age_array = [Link](df['Age'])
print("\nNumPy Operations on Age Column")
print("Mean Age:", [Link](age_array))
print("Maximum Age:", [Link](age_array))
print("Minimum Age:", [Link](age_array))
print("Standard Deviation:", [Link](age_array))
print("\nSelecting Columns (Age, MMSE):")
print(df[['Age', 'MMSE']].head())
print("\nFiltering Patients Age > 70:")
print(df[df['Age'] > 70].head())
print("\nSorting by Age (Descending):")
print(df.sort_values(by='Age', ascending=False).head())
df['Age_Group'] = [Link](df['Age'] >= 60, 'Senior', 'Adult')
print("\nNew Column Added (Age_Group):")
print(df[['Age', 'Age_Group']].head())
[Link]()
[Link](df['Age'], bins=10)
[Link]('Age')
7
[Link]('Frequency')
[Link]('Age Distribution')
[Link]()
[Link]()
[Link](df['Age'], df['MMSE'])
[Link]('Age')
[Link]('MMSE Score')
[Link]('Age vs MMSE')
[Link]()
[Link]()
df['Age_Group'].value_counts().plot(kind='bar')
[Link]('Age Group')
[Link]('Count')
[Link]('Age Group Distribution')
[Link]()
OUTPUT
Dataset Information:
<class '[Link]'>
RangeIndex: 436 entries, 0 to 435
Data columns (total 12 columns):
# Column Non-Null Count Dtype
0 ID 436 non-null object
1 M/F 436 non-null object
2 Hand 436 non-null object
3 Age 436 non-null int64
4 Educ 235 non-null float64
5 SES 216 non-null float64
8
6 MMSE 235 non-null float64
7 CDR 235 non-null float64
8 eTIV 436 non-null int64
9 nWBV 436 non-null float64
10 ASF 436 non-null float64
11 Delay 20 non-null float64
dtypes: float64(7), int64(2), object(3)
memory usage: 41.0+ KB
None
Missing values handled successfully
NumPy Operations on Age Column
Mean Age: 51.357798165137616
Maximum Age: 96
Minimum Age: 18
Standard Deviation: 25.240866432656176
New Column Added (Age_Group):
Age Age_Group
0 74 Senior
1 55 Adult
2 73 Senior
3 28 Adult
4 18 Adult
9
EXERCISE PROBLEMS
1. Write a Python program to create a NumPy array of 10 numbers and find the mean, median,
and standard deviation.
2. Write a Python program using Pandas to create a DataFrame containing student names, marks
in three subjects, and calculate the total and average marks.
3. Write a Python program to read a CSV dataset using Pandas and display summary statistics
10
(mean, max, min, count) of the data.
4. Write a Python program using Matplotlib to plot a line graph showing sales data for 6 months.
5. Write a Python program to create a dataset using Pandas and visualize it using both a bar chart
and a pie chart with Matplotlib.
VIVA QUESTIONS
1. What is the difference between a NumPy array and a Pandas DataFrame?
6. What is the difference between line plot, bar plot, and scatter plot in Matplotlib?
9. How can you customize a plot in Matplotlib (labels, title, legend, etc.)?
11
Ex:No:3 Implement linear models to approximate the given data.
Date:
Aim
To implement a simple linear regression model to approximate the given data.
Procedure
Initialize the environment in Google Colab by importing required Python libraries for
numerical computation, data handling, visualization, and linear regression modeling.
Load the dataset using pandas.read_csv() and handle missing numerical values by
replacing them with the mean of the respective columns.
Select the variables for regression, where Age is chosen as the independent variable (X)
and MMSE as the dependent variable (y).
Split the dataset into training and testing sets using the train-test split method with 80%
data for training and 20% for testing.
Create and train a Simple Linear Regression model using the training data to learn the
linear relationship between Age and MMSE.
Predict MMSE values for the test dataset and compute model parameters such as slope
and intercept.
Evaluate the model performance using Mean Squared Error (MSE) and R² score to
measure prediction accuracy.
Visualize the regression results using a scatter plot and regression line, and predict the
MMSE value for a new input age.
SAMPLE PROGRAM
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
df = pd.read_csv('/content/oasis_cross-[Link]')
[Link]([Link](numeric_only=True), inplace=True)
X = df[['Age']]
y = df['MMSE']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
12
print("Slope:", model.coef_[0])
print("Intercept:", model.intercept_)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R2 Score:", r2_score(y_test, y_pred))
[Link](X_test, y_test)
[Link](X_test, y_pred)
[Link]('Age')
[Link]('MMSE')
[Link]('Simple Linear Regression')
age_value = [Link]({'Age': [70]})
print("Predicted MMSE for Age 70:", [Link](age_value)[0])
[Link]()
Slope: -0.011149810153846043
Intercept: 27.620509691535073
Mean Squared Error: 6.2823540963309625
R2 Score: -0.005917905915383326
Predicted MMSE for Age 70: 26.84002298076585
13
EXERCISE PROBLEMS
1. Basic Linear Regression
Write a Python program to implement Simple Linear Regression using the dataset:
Task:
Fit a linear model y=mx+cy = mx + cy=mx+c
Find the slope (m) and intercept (c)
Predict the value of Y when X = 6
2. Linear Model using NumPy
Write a Python program to implement linear regression using NumPy for the following
data:
Task:
Train the linear model
Plot the best fit line
Predict marks for 6 hours of study
14
3. Implement a Linear Regression algorithm and evaluate the model performance using
error metrics such as Mean Squared Error.
4. Write a Python program to implement Multiple Linear Regression for predicting output
using more than one input feature.
5. Write a Python program to fit a linear model for given training data and visualize the best
fit line using a graph
VIVA QUESTIONS
1. What is a Linear Model in Machine Learning?
5. What is the difference between Simple Linear Regression and Multiple Linear
Regression?
10. Which libraries are commonly used in Python to implement linear regression?
11. What is the difference between R² score and Mean Squared Error in evaluating a
regression model?
12. Why is it important that independent variables should not be highly correlated in
Multiple Linear Regression?
13. How does gradient descent help in finding optimal parameters in linear models?
14. What happens if the relationship between variables is not linear but we still apply linear
regression?
15. Why do we split data into training and testing sets while building a regression model?
15
Ex:No:4
Information Gain and Gain Ratio to find the best attribute.
Date:
Aim
● To find the attribute with maximum information gain and gain ratio for the given dataset.
Procedure
A spanning tree is one in which all the vertices must be connected. The two disjoint subsets of
vertices must be connected to make a Spanning Tree. They must be connected with the
minimum weight edge to make it a Minimum Spanning Tree.
Algorithm:
Step-01:
a) Randomly choose any vertex.
b) The vertex connecting to the edge having least weight is usually selected.
Step-02:
a) Find all the edges that connect the tree to new vertices.
b) Find the least weight edge among those edges and include it in the existing tree.
c) If including that edge creates a cycle, then reject that edge and look for the next least weight
edge.
Step-03:
a) Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree (MST)
is obtained.
EXERCISE PROBLEMS
1. Write a program to construct the minimum spanning tree (MST) for the given graph
using Prim’s Algorithm.
ALGORITHM:
Start with a connected, weighted graph.
Select any vertex as the starting point.
Initialize the key values of all vertices to infinity (∞), except the starting vertex (set to 0).
Mark all vertices as unvisited initially.
Repeat for all vertices:
o Pick the unvisited vertex with the smallest key value.
o Mark it as visited.
o Update its neighboring vertices' key values if a smaller weight edge is found.
Store the edges forming the MST using a parent array.
Print the edges and total weight of the Minimum Spanning Tree (MST).
End.
PROGRAM:
#include <stdio.h>
16
#include <limits.h>
#include <conio.h>
#define MAX 10
printf("Edge \tWeight\n");
for (i = 1; i < vertices; i++) {
printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);
totalCost += graph[i][parent[i]];
}
printf("Total Cost: %d\n", totalCost);
}
17
void main() {
int graph[MAX][MAX], vertices,i,j;
clrscr();
printf("Enter number of vertices: ");
scanf("%d", &vertices);
primsAlgorithm(graph, vertices);
getch();
}
OUTPUT:
1. Write a program to construct the minimum spanning tree (MST) for the given graph using
Kruskal’s Algorithm.
ALGORITHM:
Start with a connected, weighted graph.
Sort all edges in ascending order of weight.
Initialize an empty Minimum Spanning Tree (MST).
Pick the smallest edge and check if it forms a cycle with the MST using the Disjoint Set
(Union-Find).
If no cycle, add the edge to the MST.
Repeat until we have (V - 1) edges in the MST (where V = number of vertices).
18
Print the edges and total cost of the MST.
End.
PROGRAM:
#include <stdio.h>
//#include <conio.h>
#define MAX 10
int parent[MAX];
int find(int i) {
while(parent[i] != i)
i = parent[i];
return i;
}
void unionSets(int i, int j) {
int a = find(i);
int b = find(j);
parent[a] = b;
}
void kruskalMST(int cost[MAX][MAX], int n) {
int minCost = 0, i, j, u, v, a, b, min, edgeCount = 0;
for(i = 0; i < n; i++)
parent[i] = i;
printf("\nEdges in Minimum Spanning Tree:\n");
while(edgeCount < n - 1) {
min = 9999;
a = -1;
b = -1;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(cost[i][j] < min && find(i) != find(j)) {
min = cost[i][j];
a = i;
b = j;
}
}
}
if(a != -1 && b != -1) {
unionSets(a, b);
printf("Edge %d: (%d - %d) Cost: %d\n", edgeCount + 1, a + 1, b + 1, min);
minCost += min;
edgeCount++;
19
cost[a][b] = cost[b][a] = 9999;
}
}
printf("\nTotal cost of Minimum Spanning Tree: %d\n", minCost);
}
void main() {
int cost[MAX][MAX], n, i, j;
//clrscr();
printf("Kruskal's Algorithm - Minimum Spanning Tree\n");
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix (use 9999 for no edge):\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &cost[i][j]);
}
}
kruskalMST(cost, n);
//getch();
}
OUTPUT:
ALGORITHM:
Start with the given adjacency matrix.
Use each node as an intermediate node → for (k = 0; k < n; k++)
20
Check all starting nodes → for (i = 0; i < n; i++)
Check all ending nodes → for (j = 0; j < n; j++)
Update the shortest path if a better path is found → D[i][j] = min(D[i][j], D[i][k] +
D[k][j]);
Repeat until all shortest paths are found.
Print the final matrix with shortest paths.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define MAX 10
void warshall(int graph[MAX][MAX], int n) {
int k,i,j;
for ( k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
}
}
}
}
void printMatrix(int graph[MAX][MAX], int n) {
int i,j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", graph[i][j]);
}
printf("\n");
}
}
void main() {
int n, graph[10][10],i,j;
clrscr();
printf("Warshall Algorithm\n");
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix:\n");
for (i = 0; i < n; i++) {
for ( j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
warshall(graph, n);
printf("Transitive Closure :\n");
printMatrix(graph, n);
getch();
21
}
OUTPUT:
ALGORITHM:
Initialize the distance matrix dist[i][j] = graph[i][j] for all edges, and dist[i][j] = INF for
no edge.
Iterate over all intermediate nodes: for (k = 0; k < n; k++)
Check all pairs of nodes: for (i = 0; i < n; i++)
For each pair of nodes (i, j), check if going through node k gives a shorter path:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
Repeat the process for all intermediate nodes.
The final matrix dist[i][j] contains the shortest path from node i to node j.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
#define INF 999
void printMatrix(int n, int dist[MAX][MAX]) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%d ", dist[i][j]);
}
printf("\n");
}
}
void floydWarshall(int n, int graph[MAX][MAX]) {
22
int dist[MAX][MAX];
int i, j, k;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
printf("\nShortest distances between every pair of vertices:\n");
printMatrix(n, dist);
}
void main() {
int n, i, j;
int graph[10][10];
clrscr();
printf("Floyd Warshall\n");
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix(9999 for infinity):\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
floydWarshall(n, graph);
getch();
}
OUTPUT:
23
2 9999 9999 0
Floyd Warshall
Shortest distances between every pair of vertices:
0357
5024
7 10 0 5
2570
5. Write a program using of knapsack algorithm.
ALGORITHM:
Create a table dp[i][w], where i is the number of items and w is the weight capacity of
the knapsack.
Initialize
Set dp[0][w] = 0 for all w (0 items → 0 value), and dp[i][0] = 0 for all i (0 capacity → 0
value).
Loop through each item: For each item i from 1 to n, where n is the total number of
items.
Loop through each weight capacity: For each weight capacity w from 0 to W (the
knapsack’s maximum capacity).
Check if the current item can fit in the knapsack (if weights[i-1] <= w).
If it fits:
o Choose the maximum value between not including the item (dp[i-1][w]) or
including it (dp[i-1][w - weights[i-1]] + values[i-1]).
If it doesn’t fit:
o Set dp[i][w] = dp[i-1][w] (carry over the value from the previous item).
Repeat steps 3-7 for all items and capacities.
Final result: The maximum value will be in dp[n][W] (the value for n items and
knapsack capacity W).
PROGRAM:
#include <stdio.h>
#include <string.h>
#include<conio.h>
#define MAX 100
int knapsack(int W, int wt[], int val[], char items[][MAX], int n)
{
int dp[MAX][MAX]; // DP table
int i, w;
int totalProfit;
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (wt[i-1] <= w)
24
dp[i][w] = (val[i-1] + dp[i-1][w-wt[i-1]] > dp[i-1][w]) ? (val[i-1] + dp[i-1][w-wt[i-1]]) : dp[i-1]
[w];
else
dp[i][w] = dp[i-1][w];
}
}
totalProfit = dp[n][W];
printf("\nItems included in the knapsack:\n");
w = W;
for (i = n; i > 0 && totalProfit > 0; i--)
{
if (totalProfit != dp[i-1][w]) {
printf("Item: %s, Weight: %d, Profit: %d\n", items[i-1], wt[i-1], val[i-1]);
totalProfit -= val[i-1];
w -= wt[i-1];
}
}
return dp[n][W];
}
void main()
{
int n, W, i;
char items[10][MAX];
int val[20], wt[20];
int maxProfit;
clrscr();
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the capacity of the knapsack: ");
scanf("%d", &W);
for (i = 0; i < n; i++)
{
printf("Enter name of item %d: ", i+1);
scanf("%s", items[i]);
printf("Enter weight of item %d: ", i+1);
scanf("%d", &wt[i]);
printf("Enter profit (value) of item %d: ", i+1);
scanf("%d", &val[i]);
}
maxProfit = knapsack(W, wt, val, items, n);
printf("\nTotal profit of the included items: %d\n", maxProfit);
getch();
}
OUTPUT:
Enter the number of items: 4
25
Enter the capacity of the knapsack: 50
Enter name of item 1: Laptop
Enter weight of item 1: 10
Enter profit (value) of item 1: 60
Enter name of item 2: Phone
Enter weight of item 2: 20
Enter profit (value) of item 2: 100
Enter name of item 3: Headphones
Enter weight of item 3: 30
Enter profit (value) of item 3: 120
Enter name of item 4: Book
Enter weight of item 4: 15
Enter profit (value) of item 4: 50
Items included in the knapsack:
Item: Phone, Weight: 20, Profit: 100
Item: Laptop, Weight: 10, Profit: 60
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define MAX 100
int memo[MAX];
void initializeMemo() {
int i;
for (i = 0; i < MAX; i++) {
memo[i] = -1;
}
}
int fibonacci(int n) {
if (n <= 1) {
return n;
26
}
if (memo[n] != -1) {
return memo[n];
}
memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
return memo[n];
}
void main() {
int n;
clrscr();
initializeMemo();
printf("Enter the value of n: ");
scanf("%d", &n);
if (n < 0 || n >= MAX) {
printf("Please enter a number between 0 and %d.\n", MAX - 1);
return 1;
}
printf("Fibonacci of %d = %d\n", n, fibonacci(n-1));
getch();
}
OUTPUT:
Enter the value of n: 10
Fibonacci of 10 = 55
VIVA QUESTIONS
1. Let G be an undirected connected graph with distinct edge weight. Let ‘emax’ be the edge
with maximum weight and ‘emin’ the edge with minimum weight. Justify that if ‘emax’ is in a
minimum spanning tree, then its removal must disconnect G
2. Spanning trees are always acyclic in the case of a spanning tree of a graph G. Justify.
3. Consider the graph M with 3 vertices. Its adjacency matrix is shown below. Justify that Graph
M has 3 distinct minimum spanning trees, each of cost 2.
4. Given an undirected unweighted connected graph consisting of n vertices and m edges. The
task is to find any spanning tree of this graph using Kruskal’s algorithm such that the maximum
degree over all vertices is maximum possible. The order in which you print the output edges does
not matter and an edge can be printed in reverse also i.e. (u, v) can also be printed as (v, u).
Input:
1
/\
2 5
\/
3
|
27
4
5. Consider a complete graph G with 4 vertices. How many spanning trees does the graph G has?
6. How do you solve the travelling salesman problem using a minimum spanning tree? Explain.
7. Consider a undirected graph G with vertices {A, B, C, D, E}. In graph G, every edge has
distinct weight. Edge CD is edge with minimum weight and edge AB is edge with maximum
weight. Then, Justify that no minimum spanning tree contains AB is false.
8. Justify the application of minimum spanning tree problem in telephone network. What are the
advantages?.
9. An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,….vn. Two nodes vi ,
vj are connected if and only if 0 < |i – j| <= 2. Each edge (vi, vj ) is assigned a weight i + j. A
sample graph with n = 4 is shown below. What will be the cost of the minimum spanning tree
(MST) of such a graph with n nodes?
10. Consider a weighted complete graph G on the vertex set {v1,v2 ,v} such that the weight of
the edge (v,,v) is 2|i-j|. The weight of a minimum spanning tree of G is_____________.
Ex:No:5
DYNAMIC PROGRAMMING
Date:
Aim
Construct the huffman code for the given data. Also perform encoding and decoding (use Greedy
technique).
Procedure
Steps to build Huffman Tree
Input is an array of unique characters along with their frequency of occurrences and output is
Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min
Heap is used as a priority queue. The value of frequency field is used to compare two
28
nodes in min heap. Initially, the least frequent character is at root)
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with a frequency equal to the sum of the two nodes
frequencies. Make the first extracted node as its left child and the other extracted node as
its right child. Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the
root node and the tree is complete
ALGORITHM
Algorithm Huffman (c)
{
n= |c|
Q=c
for i<-1 to n-1
do
{
struct MinHeapNode {
char data;
29
float freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
30
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]-
>freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
31
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = buildMinHeap(data, freq, size);
while (minHeap->size > 1) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void main() {
int size,i;
char *data = (char*)malloc(size * sizeof(char));
32
float *freq = (float*)malloc(size * sizeof(float));
clrscr();
printf("Enter number of characters: ");
scanf("%d", &size);
free(data);
free(freq);
getch();
}
OUTPUT:
Enter number of characters: 5
Enter character 1: A
Enter frequency of 'A': 0.4
Enter character 2: B
Enter frequency of 'B': 0.3
Enter character 3: C
Enter frequency of 'C': 0.2
Enter character 4: D
Enter frequency of 'D': 0.1
Enter character 5: E
Enter frequency of 'E': 0.05
Huffman Codes:
A: 0
B: 10
C: 110
D: 1110
E: 1111
[Link] a program to implement the Dijkstra Algorithm.
33
ALGORITHM:
1. Start with the source node and set its distance to 0, while all others are infinity (∞).
2. Pick the unvisited node with the smallest distance.
3. Update distances of its neighboring nodes if a shorter path is found.
4. Mark the current node as visited (processed).
5. Repeat steps 2-4 until all nodes are visited or shortest paths are found.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <limits.h>
#define V 10
34
dist[v] = dist[u] + graph[u][v];
}
}
}
void main() {
int n, graph[V][V], i, j, source;
clrscr();
dijkstra(graph, n, source);
getch();
}
OUTPUT:
Enter number of vertices: 5
Enter adjacency matrix:
0 10 0 30 100
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
35
100 0 10 60 0
Enter source vertex: 0
Vertex Distance from Source
0 0
1 10
2 50
3 30
4 60
VIVA QUESTIONS
1. Calculate the time complexity of Huffman code algorithm.
2. Consider the following message BCCABBDDAECCBBAEDDCC find the no of bits
requiered for huffman encoding of above message.
3. The following message is: GATE2018GAATTTEEEE22000011188What is the average
length of bits required for encoding each letter using Huffman coding___?
4. Is there any difference in between draw tree for huffman coding and optimal merge pattern if
yes please give detailed explanation.
5. What would be huffman coding for following : CharacterFrequencya10l15i12o3u4s13t1.
1. Which of the following data structure cannot be used for efficient implementation of Huffman
encoding? a. Binary min heap b. Binary max heap [Link] tree (height balanced tree)
7. What is the time complexity of Huffman algorithms when the input is already sorted
8. Compare dynamic programming with divide and conquer.
9. Compare dynamic programming with greedy method.
10. A file contain charecters a,e,i,o,u,s,t with frequencies 10,15,12,3,4,13,&1 respectively , if we
use huffman codeing for data compression then avg code length will be ______.
36
Ex:No:6 IMPLEMENT SUM OF SUBSETS PROBLEM USING
Date: BACKTRACKING
Aim
● To solve sum of subset problem using backtracking.
Procedure
Sum of Subsets:
In this problem, there is a given set with some integer elements. And another some value is
also provided, we have to find a subset of the given set whose sum is the same as the given sum
value.
Here backtracking approach is used for trying to select a valid subset when an item is not
valid, we will backtrack to get the previous subset and add another element to get the solution.
ALGORITHM
Algorithm SumOfSub(s, k, r)
{ k-1 n
// s = ∑ w[j] * x[j]; r = ∑ w[j];
j=1 j=k
// w[j]’s are in non-decreasing order
EXERCISE PROBLEMS
1. Write a program to implement sum of subsets problem using backtracking.
37
Step 1: Input the number of elements n, the elements of the array arr[n], and the target
sum.
Step 2: Initialize an empty array subset[n] to store the current subset.
Step 3: Call the recursive function find_subsets with the following parameters:
arr[]: The input array.
n: Number of elements in the array.
target: Desired sum.
index: Current index in the array (starting from 0).
subset[]: Current subset being formed.
subset_size: Current size of the subset (starting from 0).
current_sum: Sum of elements in the current subset (starting from 0).
Step 4: In the recursive function find_subsets:
a. Base Case 1: If current_sum == target, print the current subset.
b. Base Case 2: If index == n or current_sum > target, return (no further subset
possible).
c. Recursive Case:
o Include arr[index] in the subset and recursively call the function with
index + 1, updating subset_size and current_sum.
o Exclude arr[index] and recursively call the function with index + 1
without changing subset_size or current_sum.
Step 5: The recursion explores all possible combinations of the array elements to find
subsets that sum up to the target.
CODE
#include <stdio.h>
void find_subsets(int arr[], int n, int target, int index, int subset[], int subset_size, int
current_sum) {
if (current_sum == target) {
printf("Subset found: { ");
for (int i = 0; i < subset_size; i++) {
printf("%d ", subset[i]);
}
printf("}\n");
return;
}
38
return;
}
subset[subset_size] = arr[index];
find_subsets(arr, n, target, index + 1, subset, subset_size + 1, current_sum +
arr[index]);
int main() {
int n, target;
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int subset[n];
printf("Subsets with sum %d:\n", target);
find_subsets(arr, n, target, 0, subset, 0, 0);
return 0;
}
OUTPUT
Enter number of elements: 5
Enter elements: 3 3 4 2 5
Enter target sum: 5
Subsets with sum 5:
Subset found: { 3 2 }
Subset found: { 3 2 }
39
Subset found: { 5 }
Step 1: Input the number of vertices v and the adjacency matrix graph[v][v].
Step 2: Initialize an array path[v] to keep track of the current path. Set all elements to -1
(indicating unvisited vertices).
Step 3: Set the starting vertex as path[0] = 0.
Step 4: Call the recursive function hamiltonianCycleUtil to find the Hamiltonian cycle.
Step 5: In the recursive function:
a. If pos == v, check if the last vertex is connected to the first vertex
(graph[path[pos - 1]][path[0]] == 1).
o If true, print the Hamiltonian cycle.
o If false, backtrack.
b. For each vertex v from 1 to v-1:
o Check if it's safe to add vertex v to the current path:
It must be connected to the previous vertex.
It must not already be in the current path.
o If safe:
Add vertex v to path[pos].
Recursively call hamiltonianCycleUtil with pos + 1.
If the recursion fails, backtrack (remove v from path[pos]).
Step 6: If a Hamiltonian cycle is found, print the cycle. If no cycle exists, print "No
Hamiltonian Cycle found."
CODE
#include <stdio.h>
#include <conio.h>
#define MAX 10
40
int isSafe(int v, int graph[MAX][MAX], int path[], int pos) {
int i;
if (graph[path[pos - 1]][v] == 0)
return 0;
return 1;
}
path[pos] = -1;
printf("Backtracking from vertex: %d (Step %d)\n", vertex, pos + 1);
}
}
return 0;
}
41
path[i] = -1;
path[0] = 0;
printf("Starting at vertex 0\n");
if (hamiltonianCycleUtil(graph, path, 1, v) == 0) {
printf("No Hamiltonian Cycle found\n");
return;
}
printCycle(path, v);
}
int main() {
int v,graph[MAX][MAX],i,j;
clrscr();
printf("hameltonion cycle\n");
printf("Enter the number of vertices: ");
scanf("%d", &v);
printf("Enter the adjacency matrix (%dx%d):\n", v, v);
for (i = 0; i < v; i++)
for ( j = 0; j < v; j++)
scanf("%d", &graph[i][j]);
hamiltonianCycle(graph, v);
return 0;
}
OUTPUT
hameltonion cycle
Enter the number of vertices: 5
Enter the adjacency matrix (5x5):
01010
10100
01011
10101
00110
Starting at vertex 0
Moving to vertex: 1 (Step 2)
42
Moving to vertex: 2 (Step 3)
Moving to vertex: 3 (Step 4)
Moving to vertex: 4 (Step 5)
Backtracking from vertex: 4 (Step 5)
Backtracking from vertex: 3 (Step 4)
Moving to vertex: 4 (Step 4)
Moving to vertex: 3 (Step 5)
Final step: 3 -> 0 (Back to start)
Hamiltonian Cycle: 0 1 2 4 3 0
VIVA QUESTIONS
1. “Dynamic problem solution of sum of subset problem is faster than recursive solution in terms
of time complexity”. How?
2. Under what condition any set A will be a subset of B”.
3. Can you apply backtracking to solve sudoku puzzle? Explain.
4. A n x n board is made in a way so that 1 or 0 can be placed in each box where 1 is for valid
path for moving towards exit and 0 is the closed path. Find the path to exit using backtracking
algorithm.
5. How many unique colors will be required for proper vertex coloring of an empty graph having
n vertices?
6. How many unique colors will be required for proper vertex coloring of a line graph having n
vertices?
7. “In graphs, in which all vertices have an odd degree, the number of Hamiltonian cycles
through any fixed edge is always even.” Justify.
8. How many Hamiltonian paths does the following graph have?
9. You are studying for an exam and you have to study N questions. The questions take {t1, t2,
43
t3,…., tn} time(in hours) and carry {m1, m2, m3,…., mn} marks. You can study for a maximum
of T hours. You can either study a question or leave it. Choose the questions in such a way that
your score is maximized
10. You are given a knapsack that can carry a maximum weight of 60. There are 4 items with
weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of the items
you can carry using the knapsack? How do you apply backtracking to this problem?
Procedure
Branch and Bound
Branch and bound is an algorithm design paradigm which is generally used for solving
combinatorial optimization problems. These problems are typically exponential in terms of time
complexity and may require exploring all possible permutations in worst case. The Branch and
Bound Algorithm technique solves these problems relatively quickly.
Travelling salesman problem
You are given-
A set of some cities
Distance between every pair of cities
ALGORITHM
1. Draw and initialize the root node (see below for details).
2. Repeat the following step until a solution (i.e., a complete circuit, represented by a
terminal node) has been found and no unexplored non-terminal node has a smaller bound
than the length of the best solution found: – Choose an unexplored non-terminal node
with the smallest bound, and process it (see page 2 for details about this step).
44
3. When a solution has been found and no unexplored non-terminal node has a smaller
bound than the length of the best solution found, then the best solution found is optimal.
EXERCISE PROBLEMS
[Link] a program to solve travelling salesman problem by using branch and bound method.
CODE
#include <stdio.h>
#include <limits.h>
#include <conio.h>
#define MAX 10
int N;
int costMatrix[MAX][MAX];
int minCost = INT_MAX;
void tsp(int start, int currentCity, int visited[], int count, int currentCost) {
int city;
if (count == N && costMatrix[currentCity][start]) {
if (currentCost + costMatrix[currentCity][start] < minCost) {
minCost = currentCost + costMatrix[currentCity][start];
}
return;
45
}
void main() {
int visited[MAX] = {0}, i,j;
clrscr();
printf("Enter the number of cities: ");
scanf("%d", &N);
visited[0] = 1;
tsp(0, 0, visited, 1, 0);
OUTPUT
Enter the number of cities: 5
Enter the cost matrix (5 x 5):
03158
30679
16042
57403
89230
Minimum Travelling Cost: 16
46
[Link] Job Assignment Problem
CODE
#include <stdio.h>
#include <limits.h>
#define MAX 10
int minCost = INT_MAX;
int costMatrix[MAX][MAX];
int N;
47
void assignJobs(int worker, int assignedJobs[], int currentCost) {
int job;
if (worker == N) {
if (currentCost < minCost) {
minCost = currentCost;
}
return;
}
void main() {
int assignedJobs[MAX] = {0},i,j;
}
OUTPUT
48
Enter the size of the cost matrix: 3
Enter the cost matrix (3 x 3):
934
784
10 5 2
Minimum Assignment Cost: 12
VIVA QUESTIONS
1. Which data structure is most suitable for implementing best first branch and bound strategy?
49
50