ADA Manual - Cprogramming
ADA Manual - Cprogramming
LAB MANUAL
Syllabus
Part-A-
List of problems for which student should develop program and execute in the
Laboratory
1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected
graph using Kruskal's algorithm.
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected
graph using Prim's algorithm.
3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's algorithm.
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.
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given digraph.
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic Programming method.
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack problems using
greedy approximation method.
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.
9. 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.
10. 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.
12. Design and implement C/C++ Program for N Queen's problem using Backtracking.
Algorithm:
1. Sort all the edges of the graph in non-decreasing order of their weights.
2. Initialize an empty set to store the minimum spanning tree (MST).
3. Initialize an array to track the parent of each vertex in the graph. Initially, each vertex is its own parent.
4. Iterate through all the sorted edges:
a. Pick the smallest edge.
b. Check if including this edge forms a cycle in the MST. If adding this edge creates a cycle, skip it.
c. If adding this edge does not create a cycle, add it to the MST and update the parent array to merge the subsets.
5. Repeat step 4 until there are V-1 edges in the MST, where V is the number of vertices in the graph.
6. The MST is now found. Output the edges of the MST.
#include <stdio.h>
#include <stdlib.h>
return parent[component]
= findParent(parent, parent[component]);
}
int parent[n];
int rank[n];
printf(
"Following are the edges in the constructed MST\n");
for (int i = 0; i < n; i++) {
int v1 = findParent(parent, edge[i][0]);
int v2 = findParent(parent, edge[i][1]);
int wt = edge[i][2];
// Driver code
int main()
{
int edge[5][3] = { { 0, 1, 10 },
{ 0, 2, 6 },
{ 0, 3, 5 },
{ 1, 3, 15 },
{ 2, 3, 4 } };
kruskalAlgo(5, edge);
return 0;
}
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Prim's algorithm.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
// Driver's code
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
return 0;
}
3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's algorithm.
3a.
// C Program for Floyd Warshall Algorithm
#include <stdio.h>
// driver's code
int main()
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5| |
| |1
Analysis & design of algorithms Lab(BCSL404)
9 ANUSHREE G
Analysis & design of algorithms Lab(BCSL404)
\|/ | Academic Year: 2023-24
(1)------->(2)
3 */
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
// Function call
floydWarshall(graph);
return 0;
}
3b.
// Driver Code
int main()
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5| |
| |1
\|/ |
(1)------->(2)
3 */
int graph[V][V] = { {1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 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.
// C program for Dijkstra's single source shortest path
// algorithm. The program is for adjacency matrix
// representation of the graph
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
// driver's code
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
// Function call
dijkstra(graph, 0);
if (wt[index] > W) {
// Driver Code
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
cout << knapSack(W, weight, profit, 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>
#define MAX 50
double maxprofit;
int n, m, i;
double ratio[MAX];
ratio[i] = ratio[j];
ratio[j] = temp;
w[i] = w[j];
w[j] = temp2;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
int currentWeight = 0;
maxprofit = 0.0;
currentWeight += w[i];
maxprofit += p[i];
else
break;
printf("%d\t", x[i]);
int main()
scanf("%d", &n);
scanf("%d", &w[i]);
Analysis & design of algorithms Lab(BCSL404)
21 ANUSHREE G
Analysis & design of algorithms Lab(BCSL404)
printf("Enter the objects' profits: "); Academic Year: 2023-24
scanf("%d", &p[i]);
scanf("%d", &m);
greedyKnapsack(n, w, p, m);
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.
/* Find a subset of a given set S={s1,s2...sn} of n positive integers whose sum is eqal to a
given positive integer d.A suitable message is to be displayed if the given problem instance
doesn't have a solution.*/
#include<stdio.h>
void subset(int,int,int);
int x[10],w[10],d,count=0;
void main()
{
int i,n,sum=0;
clrscr();
printf("Enter the no. of elements: ");
scanf("%d",&n);
printf("\nEnter the elements in ascending order:\n");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("\nEnter the sum: ");
scanf("%d",&d);
for(i=0;i<n;i++)
sum=sum+w[i];
if(sum<d)
{
printf("No solution\n");
getch();
return;
}
subset(0,0,sum);
if(count==0)
{
printf("No solution\n");
getch();
return;
}
getch();
}
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==d)
{
printf("\n\nSubset %d\n",++count);
for(i=0;i<=k;i++)
if(x[i]==1)
printf("%d\t",w[i]);
}
Analysis & design of algorithms Lab(BCSL404)
23 ANUSHREE G
Analysis & design of algorithms Lab(BCSL404)
else if(cs+w[k]+w[k+1]<=d) Academic Year: 2023-24
subset(cs+w[k],k+1,r-w[k]);
if(cs+r-w[k]>=d && cs+w[k]<=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
9. 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.
// C program for implementation of selection sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fptr;
fptr = fopen("selection_sort_results.csv", "w");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
Analysis & design of algorithms Lab(BCSL404)
24 ANUSHREE G
Analysis & design of algorithms Lab(BCSL404)
Academic Year: 2023-24
srand(time(0));
fprintf(fptr, "Number of Elements,Time Taken (seconds)\n");
start = clock();
selectionSort(arr, n);
end = clock();
free(arr);
}
fclose(fptr);
return 0;
}
10. 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 main() {
FILE *fptr;
fptr = fopen("results.csv", "w");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
srand(time(0));
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
free(arr);
}
fclose(fptr);
return 0;
}
11. 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 = 0, j = 0, k = left;
Analysis & design of algorithms Lab(BCSL404)
27 ANUSHREE G
Analysis & design of algorithms Lab(BCSL404)
while (i < n1 && j < n2) { Academic Year: 2023-24
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int main() {
int n;
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
free(arr);
return 0;
}
Part2
int main() {
FILE *fptr;
fptr = fopen("merge_sort_results.csv", "w");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
srand(time(0));
fprintf(fptr, "Number of Elements,Time Taken (seconds)\n");
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
free(arr);
}
fclose(fptr);
return 0;
}
12. Design and implement C/C++ Program for N Queen's problem using Backtracking.