ADA - Lab Programs-Updated
ADA - Lab Programs-Updated
AIM: Design and implement C Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm
Source code:
#include <stdio.h>
#include <stdlib.h>
1
subsets[yroot].parent = xroot;
} else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
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);
2
printf("Enter edges and their weights:\n");
for (int i = 0; i < E; ++i) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph-
>edges[i].weight);
}
kruskalMST(graph);
return 0;
}
OUTPUT:
PROGRAM 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;
3
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: ");
4
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
}
return 0;
}
OUTPUT:
5
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;
6
printf("%d \t",p[i][j]);
printf("\n");
}
printf("\n The shortest paths are:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
OUTPUT:
Transitive closure:
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
7
The shortest paths are:
<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);
8
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]);
9
printf("\n");
OUTPUT:
Transitive closure:
1 1 0 1 0
10
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;
}
11
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:
12
5.Design and implement C Program to obtain the Topological ordering of vertices in a
given digraph.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
13
}
// 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;
}
14
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];
15
}
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>
#include <stdlib.h>
16
struct Item *item1 = (struct Item *)a;
struct Item *item2 = (struct Item *)b;
double ratio1 = item1->ratio;
double ratio2 = item2->ratio;
if (ratio1 > ratio2) return -1;
else if (ratio1 < ratio2) return 1;
else return 0;
}
int main() {
int n, capacity, i;
printf("Enter the number of items: ");
scanf("%d", &n);
17
printf("Enter the value and weight of each item:\n");
for (i = 0; i < n; i++) {
scanf("%d %d", &items[i].value, &items[i].weight);
items[i].ratio = (double)items[i].value / items[i].weight;
}
discreteKnapsack(items, n, capacity);
continuousKnapsack(items, n, capacity);
return 0;
}
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:
19
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, i;
clock_t start, end;
double cpu_time_used;
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
OUTPUT:
21
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>
22
}
int main() {
int n, i;
clock_t start, end;
double cpu_time_used;
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
23
end = clock();
OUTPUT:
24
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>
25
// Copy the remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
int main() {
int n, i;
clock_t start, end;
double cpu_time_used;
if (n < 5000) {
printf("Please enter a value of n greater than 5000.\n");
return 1;
}
26
// Generate n random numbers
srand(time(NULL));
// printf("Randomly generated array: ");
for (i = 0; i < n; i++) {
arr[i] = rand() % 10000; // Generating random numbers between 0 to 9999
// printf("%d ", arr[i]);
}
// printf("\n");
27
12.Design and implement C Program for N Queen's problem using Backtracking
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
}
}
28
}
29
Sample Viva Questions:
1. What is a data structure?
A data structure is a way of organizing and storing data to perform operations efficiently.
2. Differentiate between an array and a linked list.
An array is a fixed-size, contiguous memory structure, while a linked list is a dynamic data
structure where elements are linked using pointers.
3. Explain the concept of time complexity.
Time complexity represents the amount of time an algorithm takes to complete as a function of
the input size.
4. What is the difference between a stack and a queue?
A stack follows the Last In, First Out (LIFO) principle, while a queue follows the First In, First
Out (FIFO) principle.
5. How does a binary search work?
30
Binary search is a divide-and-conquer algorithm that efficiently finds the position of a target
value within a sorted array.
6. What is recursion, and how is it used in data structures?
Recursion is a programming technique where a function calls itself. It is often used in data
structures like trees and graphs.
7. Explain the concept of a hash table.
A hash table is a data structure that maps keys to values using a hash function, allowing for
efficient retrieval and storage of data.
8. What is the difference between a linked list and an array?
Arrays have a fixed size, and their elements are stored in contiguous memory locations, while
linked lists can dynamically change in size and store elements with non-contiguous memory.
9. Define a tree in terms of data structures.
A tree is a hierarchical data structure composed of nodes, each having a value and zero or more
child nodes.
10. Explain the process of tree traversal.
Tree traversal involves visiting all the nodes of a tree in a specific order. Common methods are
in-order, pre-order, and post-order traversals.
11. What is a priority queue?
A priority queue is a data structure that stores elements with associated priorities and allows for
efficient retrieval of the element with the highest priority.
12. Describe the concept of a graph.
A graph is a collection of nodes (vertices) and edges that connect pairs of nodes. It can be
directed or undirected.
Additional Programs
Program 1:
AIM: Program to demonstrate dynamic memory allocation using malloc() or calloc(),
realloc(), free() function.
Source Code:
#include<stdio.h>
#include<stdlib.h>
void main(){
int *p;
p = (int *)malloc(2*sizeof(int));
if(p==NULL){
printf("failed\n");
exit(1);
}
31
p[0]=1;
p[1]=2;
p = (int *)realloc(p,3*sizeof(int));
if(p==NULL){
printf("failed\n");
exit(1);
}
p[2]=10;
for(int i=0;i<3;i++){
printf("%d\n",p[i]);
}
free(p);
}
Sample Output:
32