Algorithm Lab Programs
Algorithm Lab Programs
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>
int i, j, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9] = {0};
int find(int i) {
while (parent[i])
i = parent[i];
return i;
}
void main() {
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the cost adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
printf("The edges of Minimum Cost Spanning Tree are:\n");
while (ne < n) {
for (i = 1, min = 999; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
Output:
void main() {
int num, i, j;
printf("\nEnter the number of nodes: ");
scanf("%d", &num);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= num; i++) {
for (j = 1; j <= num; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0) {
cost[i][j] = 999;
}
}
}
prims(num);
}
void main() {
int a[10][10], n, i, j;
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix :\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
floyd(a, n);
printf("\nShortest Path Matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
Output:
void main() {
int a[10][10], n, i, j;
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix :\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
warsh(a, n);
printf("\nResultant Path Matrix :\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
Output:
void main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
int graph[MAX_VERTICES][MAX_VERTICES] = {{0}};
printf("Enter the source vertex, destination vertex, and weight for each edge:\n");
for (int i = 0; i < E; i++) {
int source, dest, weight;
scanf("%d%d%d", &source, &dest, &weight);
graph[source][dest] = weight;
}
dijkstra(graph, 0, V);
}
Output:
void calcolsum() {
for (int j = 0; j < n; j++) {
colsum[j] = 0;
for (int i = 0; i < n; i++)
colsum[j] += adj[i][j];
}
}
void source_removal() {
int i, j, k, select[10] = {0};
printf("Topological ordering is: ");
for (i = 0; i < n; i++) {
calcolsum();
for (j = 0; j < n; j++) {
if (select[j] == 0 && colsum[j] == 0)
break;
}
printf("%d ", j + 1);
select[j] = 1;
for (k = 0; k < n; k++)
adj[j][k] = 0;
}
}
void main() {
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &adj[i][j]);
source_removal();
}
void main() {
int val[100], wt[100];
int W, n;
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the values and weights of %d items:\n", n);
for (int i = 0; i < n; i++) {
printf("Item %d - Weight and Value: ", i + 1);
scanf("%d %d", &wt[i], &val[i]);
}
printf("Enter the knapsack capacity: ");
scanf("%d", &W);
printf("Maximum value that can be obtained: %d\n", knapsack(W, wt, val, n));
}
Output:
Enter the number of items: 3
Enter the values and weights of 3 items:
Item 1 - Weight and Value : 1 15
Item 2 - Weight and Value : 2 40
Item 3 - Weight and Value : 3 15
Enter the knapsack capacity: 4
Maximum value that can be obtained: 55
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation methods.
#include <stdio.h>
void main() {
float weight[50], profit[50], ratio[50], Totalvalue = 0, temp;
float capacity, amount;
int n, i, j;
printf("Enter the number of items: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter Weight and Profit for item[%d]:\n", i + 1);
scanf("%f %f", &weight[i], &profit[i]);
}
printf("Enter the capacity of knapsack:\n");
scanf("%f", &capacity);
for (i = 0; i < n; i++)
ratio[i] = profit[i] / weight[i];
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
9. Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
if (n <= 5000) {
printf("Please enter a value greater than 5000\n");
return 0;
}
int *arr = (int *)malloc(n * sizeof(int));
generateRandomNumbers(arr, n);
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
int main() {
int n;
void main() {
int n;
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens: ");
scanf("%d", &n);
queen(1, n);
if (count == 0)
printf("\n\nNo solution is possible for %d queens.\n", n);
}
Output:
- N Queens Problem Using Backtracking -
Enter number of Queens:4
Solution 1:
1 2 3 4
1 - Q - -
2 - - - Q
3 Q - - -
4 - - Q -
Solution 2:
1 2 3 4
1 - - Q -
2 Q - - -
3 - - - Q
4 - Q - -
--------------------------------------------------
Enter number of Queens:3