ADA Lab Programs
ADA Lab Programs
#include <stdio.h>
#include <stdlib.h>
int find(int parent[], int i) {
if (parent[i] != i) {
parent[i] = find(parent, parent[i]); }
return parent[i]; }
int uni(int parent[], int i, int j) {
if (i != j) {
parent[j] = i;
return 1; }
return 0; }
void kruskal(int n, int cost[9][9]) {
int parent[9];
int mincost = 0, ne = 1;
for (int i = 1; i <= n; i++) {
parent[i] = i; }
printf("The edges of Minimum Cost Spanning Tree are:\n");
while (ne < n) {
int min = 999, a = -1, b = -1, u, v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = i; b = j;
} } }
u = find(parent, a);
v = find(parent, b);
if (uni(parent, u, v)) {
printf("%d edge (%d,%d) = %d\n", ne++, a, b, min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999; }
printf("\nMinimum cost = %d\n", mincost); }
int main() {
int n, cost[9][9];
printf("\nImplementation of Kruskal's algorithm\n");
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the cost adjacency matrix:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
} }
kruskal(n, cost);
return 0; }
02 prims
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define INF 9999999
int main() {
int V;
printf("Enter the number of vertices: ");
scanf("%d", &V);
int G[V][V];
printf("Enter the adjacency matrix (enter 0 if no edge exists):\n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
scanf("%d", &G[i][j]);
}
}
int no_edge = 0;
int selected[V];
memset(selected, false, sizeof(selected));
selected[0] = true;
int startVertex, endVertex;
int minWeight;
printf("Edge : Weight\n");
while (no_edge < V - 1) {
minWeight = INF;
startVertex = -1;
endVertex = -1;
for (int i = 0; i < V; i++) {
if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j] > 0) {
if (minWeight > G[i][j]) {
minWeight = G[i][j];
startVertex = i;
endVertex = j;
} } } } }
printf("%d - %d : %d\n", startVertex, endVertex, G[startVertex][endVertex]);
selected[endVertex] = true;
no_edge++;
}
return 0;
}
3,a Floyd Algorithm
#include <stdio.h>
#include <limits.h>
#include <time.h>
#define INF 999
void floyd(int w[10][10], int n) {
int i, j, k;
for (k = 0; k < n; k++) { // warshalls
for (i = 0; i < n; i++) { #include <stdio.h>
for (j = 0; j < n; j++) { #include <stdlib.h>
if (w[i][k] != INF && w[k][j] != INF) { void warshalls(int a[20][20], int n) {
w[i][j] = (w[i][j] < w[i][k] + w[k][j]) ? int i, j, k;
w[i][j] : (w[i][k] + w[k][j]); for (k = 0; k < n; k++)
} } } } } for (i = 0; i < n; i++)
int main() { for (j = 0; j < n; j++)
int a[10][10], n, i, j; a[i][j] = a[i][j] || (a[i][k] && a[k][j]);
printf("\nEnter the number of vertices: "); printf("\nThe transitive closure is:\n");
scanf("%d", &n); for (i = 0; i < n; i++) {
printf("\nEnter the cost matrix (0 for self-loop, for (j = 0; j < n; j++)
999 for no edge):\n"); printf("%d\t", a[i][j]);
for (i = 0; i < n; i++) { printf("\n");
for (j = 0; j < n; j++) { }
scanf("%d", &a[i][j]); }
if (i != j && a[i][j] == 0) { int main() {
a[i][j] = INF; int a[20][20], n, i, j;
} printf(" Compute transitive closure using Warshall's
} Algorithm \n");
} printf("\nEnter the number of vertices of the graph
floyd(a, n); (1-20): ");
printf("\nShortest path matrix:\n"); scanf("%d", &n);
for (i = 0; i < n; i++) { printf("\nEnter the adjacency matrix (0 or 1):\n");
for (j = 0; j < n; j++) { for (i = 0; i < n; i++)
if (a[i][j] == INF) for (j = 0; j < n; j++)
printf("INF\t"); scanf("%d", &a[i][j]);
else warshalls(a, n);
printf("%d\t", a[i][j]); return 0;
} }
printf("\n");
}
return 0;
}
04 dijkstras Algorithm
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];
for (i = 0; i < n; i++) {
distance[i] = cost[start][i];
pred[i] = start; visited[i] = 0; }
distance[start] = 0;
visited[start] = 1;
count = 1;
while (count < n - 1) {
mindistance = INFINITY;
for (i = 0; i < n; i++)
if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i; }
visited[nextnode] = 1;
for (i = 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode; }
count++; }
for (i = 0; i < n; i++)
if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
} }
int main() {
int Graph[MAX][MAX], i, j, n, u;
printf("\n Enter number of vertices");
scanf("%d",&n);
printf("\n Enter values for matrix");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&Graph[i][j]);
printf("\n Enter starting vertex");
scanf("%d",&u);
Dijkstra(Graph, n, u);
return 0; }
05 Topological order
#include <stdio.h>
int adj[10][10], indegree[10], n;
void topologicalSort() {
int count = 0;
printf("Topological Order: ");
while (count < n) {
int found = 0;
for (int i = 0; i < n; i++) {
if (indegree[i] == 0) {
printf("%d\t ", i + 1);
indegree[i] = -1;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
printf("Knapsack problems using Greedy Algorithm:\n");
for (i = 0; i < n; i++)
{
if (weight[i] > capacity)
break;
else {
Totalvalue = Totalvalue + profit[i];
capacity = capacity - weight[i];
} }
if (i < n)
Totalvalue = Totalvalue + (ratio[i]*capacity);
printf("\nThe maximum value is :%f\n",Totalvalue);
return 0;
}
06 0/1 knapsack
#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int n, capacity;
int weights[100], values[100];
int dp[100][100];
int i, j;
printf("Enter number of items: ");
scanf("%d", &n);
printf("Find subset of the given set whose sum is equal to a given integer \n");
do {
printf("\nEnter the number of elements of the set (1 to 89): ");
scanf("%d", &n);
}while (n <= 0 || n >= 90);
printf("Enter the elements of the set:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
do {
printf("\nEnter the positive integer sum: ");
scanf("%d", &d);
} while (d <= 0);
for (i = 1; i < pow(2, n); i++) {
getSubset(i, n, x);
sum = 0;
for (j = 0; j < n; j++) {
if (x[j] == 1)
sum += a[j];
}
if (sum == d) {
printf("\nSubset = { ");
present = 1;
for (j = 0; j < n; j++) {
if (x[j] == 1)
printf("%d ", a[j]);
}
printf("} = %d", d);
}
}
if (!present)
printf("\nNo subset with the given sum exists.");
return 0;
}
09 SelectionSort
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void selectionsort(int a[], int n)
{
for (int I = 0; I < n; i++)
{
int min_idx = I;
for (int j = I + 1; j < n; j++)
{
if (a[j] < a[min_idx])
{
min_idx = j;
}
int temp = a[min_idx];
a[min_idx] = a[i];
a[i] = temp;
}
}
}
int main()
{
int n;
printf(“Enter the nmber of elements : \n”);
scanf(“%d”, &n);
int a[n];
srand(time(NULL));
for (int I = 0; I < n; i++)
{
a[i] = rand() % 10000 + 1;
}
clock_t start, end;
start = clock();
selectionsort(a, n);
end = clock();
double cpu_time = ((double)(end – start)) / CLOCKS_PER_SEC;
for (int I = 0; I < n; i++)
{
printf(“%d \t”, a[i]);
}
printf(“\n”);
printf(“Time taken to sort the array is %f seconds\n”, cpu_time);
return 0;
}
10 QuickSort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int partition(int a[], int low, int high)
{
int pivot = a[high];
int i = low - 1;
for (int j = low; j <= high; j++)
{
if (a[j] < pivot) {
i++;
int temp = a[i];
a[i] = a[j];
a[j] = temp; }
}
int temp = a[i + 1];
a[i + 1] = a[high];
a[high] = temp;
return i + 1;
}
void quicksort(int a[], int low, int high) {
if (low < high) {
int p = partition(a, low, high);
quicksort(a, low, p - 1);
quicksort(a, p + 1, high);
} }
int main() {
int n;
printf("Enter the number of elements : \n");
scanf("%d", &n);
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) {
a[i] = rand() % 5000 + 1;
}
clock_t start, end;
start = clock();
quicksort(a, 0, n - 1);
end = clock();
double cpu_time = ((double)(end - start)) / CLOCKS_PER_SEC;
for (int i = 0; i < n; i++)
{
printf("%d \t", a[i]);
}
printf("\n");
printf("Time taken to sort the array is %f seconds\n", cpu_time);
return 0; }
11 MergeSort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void merge(int a[], int low, int mid, int high)
{
int l1, l2, b[5000], i = 0;
for (l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++)
{
if (a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while (l1 <= mid)
b[i++] = a[l1++];
while (l2 <= high)
b[i++] = a[l2++];
for (i = low; i <= high; i++)
a[i] = b[i];
}
void mergesort(int a[], int low, int high) {
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergesort(a, low, mid);
mergesort(a, mid + 1, high);
merge(a, low, mid, high);
} }
int main() {
int n;
printf("Enter number of terms ");
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
a[i] = rand() % 5000 + 1;
srand(time(NULL));
clock_t start, end;
start = clock();
mergesort(a, 0, n);
end = clock();
double cpu_time = ((double)(end - start)) / CLOCKS_PER_SEC;
for (int i = 0; i < n; i++)
printf("\t%d", a[i]);
printf("\ntotal time %lf", cpu_time);
return 0;
}
12 N Queen
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int board[10], n;
int is_safe(int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i] == col || abs(board[i] - col) == abs(i - row))
return 0;
}
return 1;
}
void print_board() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i] == j)
printf("Q ");
else
printf(". ");
}
printf("\n");
}
printf("\n");
}
void solve(int row) {
if (row == n) {
print_board();
return;
}
for (int col = 0; col < n; col++) {
if (is_safe(row, col)) {
board[row] = col;
solve(row + 1);
}
}
}
int main() {
printf("Enter N (max 10): ");
scanf("%d", &n);
solve(0);
return 0;
}