Ada Lab Manaul
Ada Lab Manaul
Ada Lab Manaul
Design and implement C Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
if (x != y) {
mst[e++] = next_edge;
Union(subsets, x, y);
}
}
int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);
kruskalMST(graph);
return 0;
}
OUTPUT:
2.Design
and
implement C Program to find Minimum Cost Spanning Tree of a given connected undirected graph
using Prim's algorithm
PROGRAM:
#include <stdio.h>
#include <limits.h>
// Function to find the vertex with the minimum key value, from the set of vertices not yet included
in the MST
int minKey(int key[], int mstSet[], int V) {
int min = INT_MAX, min_index;
return min_index;
}
// Function to construct and print MST for a graph represented using adjacency matrix
representation
void primMST(int graph[][V_MAX], int V) {
int parent[V_MAX]; // Array to store constructed MST
int key[V_MAX]; // Key values used to pick minimum weight edge in cut
int mstSet[V_MAX]; // To represent set of vertices not yet included in MST
// Always include first 1st vertex in MST. Make key 0 so that this vertex is picked as the first
vertex
key[0] = 0;
parent[0] = -1; // First node is always the root of MST
// Update key value and parent index of the adjacent vertices of the picked vertex
// Consider only those vertices which are not yet included in the MST
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
int main() {
int V, E;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
// Prompt the user to enter the source vertex, destination vertex, and weight for each edge
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;
graph[dest][source] = weight; // Since the graph is undirected
}
// Print the MST using Prim's algorithm
primMST(graph, V);
return 0;
}
OUTPUT:
3.a. Design and implement C Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
PROGRAM:
#include<stdio.h>
int min(int,int);
void floyds(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i==j)
p[i][j]=0; else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b) {
if(a<b)
return(a); else
return(b);
}
void main() {
int p[10][10],w,n,e,u,v,i,j;
OUTPUT:
Transitive closure:
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
<1,2>=10
<1,3>=3
<1,4>=4
<2,1>=2
<2,3>=5
<2,4>=6
<3,1>=7
<3,2>=7
<3,4>=1
<4,1>=6
<4,2>=16
3b.Design and implement C Program to find the transitive closure using Warshal's algorithm.
PROGRAM:
#include<stdio.h>
#include<math.h>
int i, j, k;
if (a > b)
return (a);
else
return (b);
void main() {
int p[10][10] = { 0 }, n, e, u, v, i, j;
scanf("%d", &n);
scanf("%d", &e);
p[u][v] = 1;
printf("%d\t", p[i][j]);
printf("\n");
warshal(p, n);
printf("%d\t", p[i][j]);
printf("\n");
OUTPUT:
Transitive closure:
1 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 1
4.Design and implement C Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
// A function to find the vertex with the minimum distance value, from the set of vertices not yet
included in the shortest path tree
int minDistance(int dist[], bool sptSet[], int V) {
int min = INF, min_index;
return min_index;
}
dist[src] = 0;
printSolution(dist, V);
}
// Driver code
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
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;
graph[dest][source] = weight; // Assuming undirected graph
}
dijkstra(graph, 0, V);
return 0;
}
OUTPUT:
5.Design and implement C Program to obtain the Topological ordering of vertices in a given
digraph.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
Graph* graph = createGraph(V);
printf("Enter the number of edges: ");
scanf("%d", &E);
printf("Enter the edges (source vertex, destination vertex):\n");
for (int i = 0, src, dest; i < E; i++) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
topologicalSort(graph);
return 0;
}
OUTPUT:
6.Design and implement C Program to solve 0/1 Knapsack problem using Dynamic Programming
method.
PROGRAM:
#include <stdio.h>
// K[n][W] contains the maximum value that can be put in a knapsack of capacity W
return K[n][W];
}
int main() {
int val[100], wt[100]; // Arrays to store values and weights
int W, n; // Knapsack capacity and number of items
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Maximum value that can be obtained: %d\n", knapsack(W, wt, val, n));
return 0;
}
OUTPUT:
7.Design and implement C Program to solve discrete Knapsack and continuous Knapsack problems
using greedy approximation method.
PROGRAM:
#include<stdio.h>
int main()
{
float weight[50],profit[50],ratio[50],Totalvalue,temp,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);
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];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
OUTPUT:
8.Design and implement C Program to find a subset of a given set S = {sl , s2,.....,sn} of n positive
integers whose sum is equal to a given positive integer d.
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
int main() {
int set[MAX_SIZE];
int subset[MAX_SIZE];
int n, sum;
return 0;
}
OUTPUT:
9.Design and implement 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.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
srand(time(0)); // Seed for random number generation
start = clock();
selectionSort(arr, n);
end = clock();
OUTPUT:
10.Design and implement C Program to sort a given set of n integer elements using Quick 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.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fp;
fp = fopen("numbers.txt", "w");
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
srand(time(NULL));
for (int i = 0; i < n; i++) {
int num = rand() % 10000;
fprintf(fp, "%d ", num);
}
fclose(fp);
int arr[n];
fp = fopen("numbers.txt", "r");
for (int i = 0; i < n; i++) {
fscanf(fp, "%d", &arr[i]);
}
fclose(fp);
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
return 0;
}
OUTPUT:
11.Design and implement C Program to sort a given set of n integer elements using Merge 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.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fp;
fp = fopen("numbers.txt", "w");
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
srand(time(NULL));
for (int i = 0; i < n; i++) {
int num = rand() % 10000;
fprintf(fp, "%d ", num);
}
fclose(fp);
int arr[n];
fp = fopen("numbers.txt", "r");
for (int i = 0; i < n; i++) {
fscanf(fp, "%d", &arr[i]);
}
fclose(fp);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
return 0;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}