Algorithm Lab Manual
Algorithm Lab Manual
LABORATORY MANUAL
Prepared by
Dr.Vikas Reddy. S
Associate Professor & Head
Department of Artificial Intelligence & Machine Learning
SJC Institute of Technology
2025
Template for Practical Course and if AEC is a practical Course Annexure-V
@# 16032024
Template for Practical Course and if AEC is a practical Course Annexure-V
@# 16032024
Template for Practical Course and if AEC is a practical Course Annexure-V
● SEE shall be conducted jointly by the two examiners of the same institute, examiners are
appointed by the Head of the Institute.
● The examination schedule and names of examiners are informed to the university before the
conduction of the examination. These practical examinations are to be conducted between the
schedule mentioned in the academic calendar of the University.
● All laboratory experiments are to be included for practical examination.
● (Rubrics) Breakup of marks and the instructions printed on the cover page of the answer script
to be strictly adhered to by the examiners. OR based on the course requirement evaluation
rubrics shall be decided jointly by examiners.
● Students can pick one question (experiment) from the questions lot prepared by the examiners
jointly.
● Evaluation of test write-up/ conduction procedure and result/viva will be conducted jointly by
examiners.
● General rubrics suggested for SEE are mentioned here, writeup-20%, Conduction procedure and
result in -60%, Viva-voce 20% of maximum marks. SEE for practical shall be evaluated for 100 marks
and scored marks shall be scaled down to 50 marks (however, based on course type, rubrics shall be
decided by the examiners)
● Change of experiment is allowed only once and 15% of Marks allotted to the procedure part are to be
made zero.
The minimum duration of SEE is 02 hours
@# 16032024
1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree
of a given connected undirected graph using Kruskal's algorithm.
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Graph graph;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &graph.V, &graph.E);
kruskalMST(&graph);
return 0;
}
Output
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int V;
printf("Enter the number of vertices: ");
scanf("%d", &V);
int graph[MAX][MAX];
printf("Enter the adjacency matrix (use 0 for no edge):
\n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
scanf("%d", &graph[i][j]);
}
}
primMST(graph, V);
return 0;
}
Output
#include <stdio.h>
#define INF 99999 // A large value to represent infinity
#define V 4 // Number of vertices in the graph
int main() {
int graph[V][V] = {
{0, 3, INF, 5},
floydWarshall(graph);
return 0;
}
Output
#include <stdio.h>
#define V 4 // Number of vertices in the graph
int main() {
int graph[V][V] = {
{0, 1, 0, 0},
{0, 0, 0, 1},
{0, 0, 0, 0},
{1, 0, 1, 0}
};
Output
#include <stdio.h>
#include <limits.h>
#define V 5 // Number of vertices in the graph
dist[src] = 0;
int main() {
int graph[V][V] = {
{0, 10, 3, 0, 0},
{10, 0, 1, 2, 4},
{3, 1, 0, 0, 8},
{0, 2, 0, 0, 5},
{0, 4, 8, 5, 0}
};
return 0;
}
Output
Vertex Distance from Source
0 0
1 4
2 3
3 6
4 8
Program ended with exit code: 0
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Enter number of vertices: ");
scanf("%d", &n);
topologicalSort();
return 0;
}
Output
Enter number of vertices: 5
0 0 1 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
Topological Order: 1 0 2 3 4
#include <stdio.h>
int main() {
int n, capacity;
printf("Enter number of items: ");
scanf("%d", &n);
int weights[n], values[n];
return 0;
}
#include <stdio.h>
struct Item {
int weight;
int profit;
int index; // Original item number
};
// Fractional Knapsack
float fractionalKnapsack(struct Item items[], int n, int
capacity) {
float totalProfit = 0.0;
return totalProfit;
}
return totalProfit;
}
int main() {
int n, capacity;
// Input
printf("Enter number of items: ");
scanf("%d", &n);
// Output
printf("\n--- Final Results ---\n");
printf("Total Profit (Fractional Knapsack): %.2f\n",
fracProfit);
printf("Total Profit (0/1 Knapsack - Greedy Approx):
%d\n", discProfit);
return 0;
}
Output
Enter number of items: 3
Enter weight and profit of item 1: 20 100
Enter weight and profit of item 2: 10 60
Enter weight and profit of item 3: 30 120
Enter knapsack capacity: 50
#include <stdio.h>
int found = 0;
// Include set[index]
subset[index] = 1;
subsetSum(set, n, d, index + 1, currentSum + set[index],
subset);
// Exclude set[index]
subset[index] = 0;
subsetSum(set, n, d, index + 1, currentSum, subset);
}
int main() {
int set[100], n, d;
if (!found)
printf("No subset found with the given sum.\n");
return 0;
}
Output
Sample 1:
Sample 2:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter number of elements (n > 5000 recommended):
");
scanf("%d", &n);
// Measure time
clock_t start = clock();
selectionSort(arr, n);
free(arr);
return 0;
}
Output
Sample 1:
Sample 2:
Sample 3:
Enter number of elements (n > 5000 recommended): 25000
Time taken to sort 25000 elements using Selection Sort:
0.39842 seconds
Program ended with exit code: 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Partition function
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Pivot element
int i = low - 1;
int main() {
int n;
printf("Enter number of elements (n > 5000 recommended):
");
scanf("%d", &n);
free(arr);
return 0;
}
Output
Sample 1:
Sample 2:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter number of elements (n > 5000 recommended):
");
scanf("%d", &n);
free(arr);
return 0;
}
Sample 1:
Enter number of elements (n > 5000 recommended): 5800
Time taken to sort 5800 elements using Merge Sort: 0.00210
seconds
Program ended with exit code: 0
Sample 2:
Enter number of elements (n > 5000 recommended): 14000
Time taken to sort 14000 elements using Merge Sort: 0.00547
seconds
Program ended with exit code: 0
Sample 3:
Enter number of elements (n > 5000 recommended): 21000
Time taken to sort 21000 elements using Merge Sort: 0.00805
seconds
Program ended with exit code: 0
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 20
int board[MAX];
int solutionCount = 0;
int main() {
int n;
printf("Enter the number of queens (N <= 15 recommended):
");
scanf("%d", &n);
solveNQueens(0, n);
printf("Total solutions for N = %d: %d\n", n,
solutionCount);
return 0;
}
Output
Sample 1:
Enter the number of queens (N <= 15 recommended): 4
Solution 1:
. Q . .
. . . Q
Q . . .
. . Q .
Solution 2:
. . Q .
Q . . .
. . . Q
. Q . .
Sample 2:
Enter the number of queens (N <= 15 recommended): 25
Invalid value of N. Try N between 1 and 20.
Program ended with exit code: 1
***********************