ADA
ADA
ADA
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>
// Function prototypes
void addEdge(struct Edge* edges, int src, int dest, int weight);
int find(struct Subset subsets[], int i);
void Union(struct Subset subsets[], int x, int y);
int compareEdges(const void* a, const void* b);
void KruskalMST(struct Edge* edges, int V);
int main() {
int V, E;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
KruskalMST(edges, V);
free(edges);
return 0;
}
// If including this edge does not cause cycle, include it in result and increment the index
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
free(subsets);
}
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>
#include <stdlib.h>
#include <limits.h>
return min_index;
}
int main() {
/* Let us create the following graph
23
(0)--(1)--(2)
|/\|
6| 8/ \5 |7
|/ \|
(3)-------(4)
9 */
return 0;
}
3a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's algorithm.
#include <stdio.h>
#include <limits.h>
if (dist[i][j] == INT_MAX)
printf("INF\t");
else
printf("%d\t", dist[i][j]);
printf("\n");
int dist[V][V];
// Update dist[][] with the shortest path using all vertices as intermediates
// If vertex k is on the shortest path from i to j, then update the value of dist[i][j]
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j]) {
printSolution(dist);
int main() {
10
(0)------->(3)
| /|\
5| |
| |1
\|/ |
(1)------->(2)
3 */
{INT_MAX, 0, 3, INT_MAX},
{INT_MAX, INT_MAX, 0, 1},
floydWarshall(graph);
return 0;
3b. Design and implement C Program to find the transitive closure using Warshal's algorithm.
#include <stdio.h>
#include <stdbool.h>
printf("%d\t", reach[i][j]);
printf("\n");
bool reach[V][V];
reach[i][j] = graph[i][j];
}
// Update reach[][] to include all vertices that are reachable through intermediate vertices
printTransitiveClosure(reach);
int main() {
1 4
(0)--->(3)
/|\ /|\
| |
2| |5
| |
\/ \/
(1)--->(2)
*/
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}};
// Find the transitive closure of the given graph
transitiveClosure(graph);
return 0;
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>
#include <stdbool.h>
#include <limits.h>
// Function to find the vertex with the minimum distance value, from the set of vertices not yet included in
the shortest path tree
min = dist[v];
min_index = v;
return min_index;
// Function to implement Dijkstra's algorithm for a given graph represented as an adjacency matrix graph[V]
[V]
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree or shortest distance
from src to i is finalized
dist[i] = INT_MAX;
sptSet[i] = false;
dist[src] = 0;
// Pick the minimum distance vertex from the set of vertices not yet processed.
sptSet[u] = true;
// Update dist[v] only if is not in sptSet, there is an edge from u to v, and total weight of path from src
to v through u is smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
printSolution(dist);
int main() {
4 8
(0)----->(1)----->(2)
| /|\ |
2| / | \ |7
| / |3 \ |
\/ / \/ \/ \/
(3)<------(4)----->(5)
| 6| /
9| |2 4|
| \/ \/
\------->(6)----->(7)----->(8)
2 1 5
*/
{0, 0, 8, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 7, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 9, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 6, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0}};
dijkstra(graph, src);
return 0;
Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given digraph
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct GraphNode {
int vertex;
};
struct Graph {
int numVertices;
bool* visited;
};
newNode->vertex = v;
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
graph->visited[i] = false;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
void DFS(struct Graph* graph, int vertex, bool visited[], int* stack, int* index) {
visited[vertex] = true;
if (!visited[adjVertex]) {
adjNode = adjNode->next;
stack[(*index)++] = vertex;
int stack[MAX_VERTICES];
int index = 0;
bool visited[MAX_VERTICES];
visited[i] = false;
if (!visited[i]) {
printf("\n");
}
int main() {
topologicalSort(graph);
return 0;
Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic Programming method.
#include <stdio.h>
return (a > b) ? a : b;
int i, w;
int K[n + 1][W + 1];
if (i == 0 || w == 0)
K[i][w] = 0;
else
return K[n][W];
int main() {
int W = 50;
printf("Maximum value that can be obtained: %d\n", knapsack(W, wt, val, n));
return 0;
7.Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack problems using
greedy approximation method.
#include <stdio.h>
int weight;
int value;
} Item;
int totalValue = 0;
// Swap items
items[j + 1] = temp;
totalValue += items[i].value;
remainingCapacity -= items[i].weight;
} else {
return totalValue;
// Swap items
items[j + 1] = temp;
totalValue += items[i].value;
capacity -= items[i].weight;
} else {
break;
return totalValue;
int main() {
// Example usage
return 0;
8 Design and implement C/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.
#include <stdio.h>
void findSubsets(int set[], int n, int sum, int subset[], int subsetSize, int index, int targetSum) {
if (sum == targetSum) {
printf("\n");
return;
return;
subset[subsetSize] = set[index];
// Exclude the current element from the subset and try next element
int subset[MAX_SIZE];
int main() {
int targetSum = 7;
subsetSum(set, n, targetSum);
return 0;
}
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 min_index = i;
min_index = j;
arr[i] = arr[min_index];
arr[min_index] = temp;
int main() {
double cpu_time_used;
// You can also generate random numbers instead of reading from a file
if (file == NULL) {
return 1;
int n;
if (arr == NULL) {
return 1;
fclose(file);
selectionSort(arr, n);
end = clock();
printf("Sorted array:\n");
printf("\n");
free(arr);
return 0;
Design and implement C/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.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
quickSort(arr, pi + 1, high);
int main() {
double cpu_time_used;
// File pointer to read elements from file
FILE *file;
// You can also generate random numbers instead of reading from a file
if (file == NULL) {
return 1;
int n;
if (arr == NULL) {
return 1;
fclose(file);
// Measure the time taken to sort using quick sort
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
printf("Sorted array:\n");
printf("\n");
free(arr);
return 0;
Design and implement C/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.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
i++;
k++;
arr[k] = R[j];
j++;
k++;
// l is for left index and r is right index of the sub-array of arr to be sorted
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
double cpu_time_used;
// File pointer to read elements from file
FILE *file;
// You can also generate random numbers instead of reading from a file
if (file == NULL) {
return 1;
int n;
if (arr == NULL) {
return 1;
fclose(file);
// Measure the time taken to sort using merge sort
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
printf("Sorted array:\n");
printf("\n");
free(arr);
return 0;
Design and implement C/C++ Program for N Queen's problem using Backtracking
#include <stdio.h>
#include <stdbool.h>
printf("\n");
int i, j;
if (board[row][i])
return false;
if (board[i][j])
return false;
if (board[i][j])
return false;
return true;
}
// Recursive function to solve N Queen problem
if (col >= N)
return true;
// Consider this column and try placing this queen in all rows one by one
if (isSafe(board, i, col)) {
board[i][col] = 1;
return true;
board[i][col] = 0;
// If the queen can't be placed in any row in this column, return false
return false;
void solveNQ() {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0} };
if (solveNQUtil(board, 0) == false) {
return;
printSolution(board);
int main() {
solveNQ();
return 0;