Ada Lab Manual
Ada Lab Manual
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>
int main() {
int n, i, j, min, a, b, u, v, cost[20][20], visited[20];
int ne = 1, mincost = 0;
while(ne < n) {
min = 999;
// Find the minimum weight edge connecting a visited node to an unvisited node
for(i = 0; i < n; i++) {
if(visited[i]) {
for(j = 0; j < n; j++) {
if(!visited[j] && cost[i][j] < min && cost[i][j] != 0) {
min = cost[i][j];
a = i;
b = j;
}
}
}
}
// Add the edge to the MST
visited[b] = 1;
printf("Edge %d: (%d -> %d) = %d\n", ne++, a, b, min);
mincost += min;
return 0;
}
OUTPUT
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
#include<stdio.h>
int a, b, u, v, n, i, j, ne = 1;
int visited[10] = {0}, min, mincost = 0, cost[10][10];
void main()
{
printf("\n Enter the number of nodes: ");
scanf("%d", &n);
visited[1] = 1;
printf("\n");
OUTPUT
3. A. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
#include <stdio.h>
#define INF 999
void main() {
int a[10][10], n, i, j;
floyd(a, n);
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
b. Design and implement C/C++ Program to find the transitive closure using Warshal's algorithm.
#include <stdio.h>
int i, j, k;
void main() {
int a[10][10], n, i, j;
scanf("%d", &n);
scanf("%d", &a[i][j]);
warsh(a, n);
printf("\n");
OUTPUT:
0 1 0 0
0 0 0 1
0 0 0 0
1 0 1 0
1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
4. Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm
#include <stdio.h>
#define INF 999
int main() {
int c[10][10], d[10], i, j, s, n;
printf("\nEnter n value: ");
scanf("%d", &n);
printf("\nEnter the graph data:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &c[i][j]);
}
}
printf("\nEnter the source node: ");
scanf("%d", &s);
dijkstra(c, n, s, d);
printf("\nShortest distances from source node %d:\n", s);
for (i = 1; i <= n; i++) {
printf("Node %d: %d\n", i, d[i]);
}
OUTPUT
Enter n value: 6
0 15 10 999 45 999
Node 1: 35
Node 2: 0
Node 3: 15
Node 4: 35
Node 5: 20
Node 6: 999
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph
#include<stdio.h>
int temp[10], k = 0;
void main()
{
int a[10][10], id[10], n, i, j;
// clrscr();
printf("\nEnter the n value: ");
scanf("%d", &n);
if (k != n)
printf("\nTopological ordering not possible");
else
{
printf("\nTopological ordering is: ");
for (i = 1; i <= k; i++)
printf("%d ", temp[i]);
}
OUTPUT:
0 0 1 1 0 0
0 0 0 1 1 0
0 0 0 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1
0 0 0 0 0 0
#include <stdio.h>
void main() {
int m, i, max_profit;
}
OUTPUT-1
OUTPUT-2
#include <stdio.h>
#define MAX 50
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}
int currentWeight = 0;
maxprofit = 0.0;
void main()
{
printf("Enter the number of objects: ");
scanf("%d", &n);
greedyKnapsack(n, w, p, m);
OUTPUT:
#include <stdio.h>
void findSubset(int S[ ], int n, int d, int sum, int index, int subset[ ], int subIndex) {
int I;
if (sum == d) {
printf("Subset: ");
for (i = 0; i < subIndex; i++) {
printf("%d \t", subset[i]);}
printf("\n");
return;
}
subset[subIndex] = S[index];
findSubset(S, n, d, sum + S[index], index + 1, subset, subIndex + 1);
findSubset(S, n, d, sum, index + 1, subset, subIndex);
}
void main()
{
int n,i, d;
printf("Enter number of elements (n): ");
scanf("%d", &n);
#include <stdio.h>
#include <sys/time.h>
small = a[j];
pos = j;
small = a[i];
pos = i;
temp = a[j];
a[j] = a[pos];
a[pos] = temp;
int main() {
scanf("%d", &n);
if (n > 10000) {
scanf("%d", &a[i]);
selsort(a, n);
if (microseconds < 0) {
seconds--;
}
printf("\nTime taken is: %ld seconds and %ld microseconds\n", seconds, microseconds);
printf("\n");
return 0;
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
key = low;
i = low + 1;
j = high;
while (i <= j) {
while (a[i] <= a[key] && i <= high) i++;
while (a[j] > a[key] && j >= low) j--;
if (i < j)
Exch(&a[i], &a[j]);
}
Exch(&a[j], &a[key]);
QuickSort(a, low, j - 1);
QuickSort(a, j + 1, high);
}
int main() {
int n, k;
struct timeval start, end; // To hold the start and end times
double ts;
// Record the start time using clock_gettime (CLOCK_MONOTONIC is the preferred clock)
QuickSort(a, 0, n - 1);
if (microseconds < 0) {
seconds--;
printf("\nTime taken is: %ld seconds and %ld microseconds\n", seconds, microseconds);
return 0;
}
OUTPUT:
Enter How many Numbers: 50
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int i, j, k;
i = low;
j = mid + 1;
k = 0;
b[k++] = a[i++];
else
b[k++] = a[j++];
b[k++] = a[i++];
b[k++] = a[j++];
for (i = low, k = 0; i <= high; i++, k++)
void main() {
int n, a[2000], k;
double ts;
scanf("%d", &n);
printf("%d\t", a[k]);
}
gettimeofday(&start, NULL);
MergeSort(a, 0, n - 1);
gettimeofday(&end, NULL);
if (microseconds < 0) {
seconds--;
printf("%d\t", a[k]);
printf("\nTime taken is: %ld seconds and %ld microseconds\n", seconds, microseconds);
OUTPUT:
83 86 77 15 93 35 86 92 49 21 62 27 90
59 63
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void print_sol(int n) {
int i, j;
count++;
printf("\n\nSolution #%d:\n", count);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (a[i] == j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n) {
int k = 1;
a[k] = 0;
while (k != 0) {
a[k] = a[k] + 1;
while ((a[k] <= n) && !place(k))
a[k]++;
if (a[k] <= n) {
if (k == n)
print_sol(n);
else {
k++;
a[k] = 0;
}
} else {
k--;
}
}
}
void main() {
int n;
printf("Enter the number of Queens: ");
scanf("%d", &n);
queen(n);
printf("\nTotal solutions = %d", count);
OUTPUT:
Solution #1:
* Q * *
* * * Q
Q * * *
* * Q *
Solution #2:
* * Q *
Q * * *
* * * Q
* Q * *
Total solutions = 2