newdaamanual
newdaamanual
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.
#include<stdio.h>
#define INF 999
int prim(int c[10][10],int n,int s)
{
int v[10],i,j,sum=0,ver[10],d[10],min,u;
for(i=1; i<=n; i++)
{
ver[i]=s;
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1; i<=n-1; i++)
{
min=INF;
for(j=1; j<=n; j++)
if(v[j]==0 && d[j]<min)
{
min=d[j];
u=j;
}
v[u]=1;
sum=sum+d[u];
printf("\n%d -> %d sum=%d",ver[u],u,sum);
for(j=1; j<=n; j++)
if(v[j]==0 && c[u][j]<d[j])
{
d[j]=c[u][j];
ver[j]=u;
}
}
return sum;
}
void main()
{
int c[10][10],i,j,res,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 souce node:");
scanf("%d",&s);
res=prim(c,n,s);
printf("\nCost=%d",res);
getch();
}
output
Enter n value:4
4 -> 1 sum=0
4 -> 2 sum=2
1 -> 3 sum=4
Cost=4
3. 3A. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd’s algorithm.
#include<stdio.h>
#include<conio.h>
#define INF 999
int min(int a,int b)
{
return(a<b)?a:b;
}
void floyd(int p[][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++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
void main()
{
int a[10][10],n,i,j;
printf("\nEnter the 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",&a[i][j]);
floyd(a,n);
printf("\nShortest path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}
output
3.B3B. Design and implement C/C++ Program to find the transitive closure using Warshal’s
algorithm.
#include<stdio.h>
void warsh(int p[][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++)
p[i][j]=p[i][j] || p[i][k] && p[k][j];
}
int main()
{
int a[10][10],n,i,j;
printf("\nEnter the 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",&a[i][j]);
warsh(a,n);
printf("\nResultant path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
output
#include<stdio.h>
int v[10],min,u,i,j;
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
min=INF;
min=d[j];
u=j;
v[u]=1;
d[j]=d[u]+c[u][j];
int main()
int c[10][10],d[10],i,j,s,sum,n;
printf("\nEnter n value:");
scanf("%d",&n);
scanf("%d",&c[i][j]);
scanf("%d",&s);
dijkstra(c,n,s,d);
return 0;
}
output
Enter n value:4
999 87 56 45
1 0 999 678
5. 5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.
#include<stdio.h>
#include<conio.h>
int temp[10],k=0;
void sort(int a[][10],int id[],int n)
{
int i,j;
for(i=1; i<=n; i++)
{
if(id[i]==0)
{
id[i]=-1;
temp[++k]=i;
for(j=1; j<=n; j++)
{
if(a[i][j]==1 && id[j]!=-1)
id[j]--;
}
i=0;
}
}
}
void main()
{
int a[10][10],id[10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
for(i=1; i<=n; i++)
id[i]=0;
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==1)
id[j]++;
}
sort(a,id,n);
if(k!=n)
printf("\nTopological ordering not possible");
else
{
printf("\nTopological ordering is:");
for(i=1; i<=k; i++)
printf("%d ",temp[i]);
}
getch();
}
****************************************OUTPU1*******************************
*********
****************************************OUTPU2*******************************
*********
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
#include<stdio.h>
int w[10],p[10],n;
int max(int a,int b)
{
return a>b?a:b;
}
int knap(int i,int m)
{
if(i==n) return w[i]>m?0:p[i];
if(w[i]>m) return knap(i+1,m);
return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
}
int main()
{
int m,i,max_profit;
printf("\nEnter the no. of objects:");
scanf("%d",&n);
printf("\nEnter the knapsack capacity:");
scanf("%d",&m);
printf("\nEnter profit followed by weight:\n");
for(i=1; i<=n; i++)
scanf("%d %d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("\nMax profit=%d",max_profit);
return 0;
}
output
Enter the no. of objects:4
Max profit=100
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
int p[MAX], w[MAX], x[MAX];
double maxprofit;
int n, m, i;
void greedyKnapsack(int n, int w[], int p[], int m)
{
double ratio[MAX];
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}
int currentWeight = 0;
maxprofit = 0.0;
// Fill the knapsack with items
for (i = 0; i < n; i++)
{
if (currentWeight + w[i] <= m)
{
x[i] = 1; // Item i is selected
currentWeight += w[i];
maxprofit += p[i];
}
else
{
// Fractional part of item i is selected
x[i] = (m - currentWeight) / (double)w[i];
maxprofit += x[i] * p[i];
break;
}
}
printf("Optimal solution for greedy method: %.1f\n", maxprofit);
printf("Solution vector for greedy method: ");
for (i = 0; i < n; i++)
printf("%d\t", x[i]);
}
int main()
{
printf("Enter the number of objects: ");
scanf("%d", &n);
printf("Enter the objects' weights: ");
for (i = 0; i < n; i++)
scanf("%d", &w[i]);
printf("Enter the objects' profits: ");
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
printf("Enter the maximum capacity: ");
scanf("%d", &m);
greedyKnapsack(n, w, p, m);
return 0;
}
output
#include<stdio.h>
#define MAX 10
int s[MAX],x[MAX],d;
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1; i<=k; i++)
if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
else if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r
-s[k]);
if((p+r
x[k]=0;
sumofsub(p,k+1,r
-s[k]);
int main()
int i,n,sum=0;
scanf("%d",&n);
scanf("%d",&s[i]);
scanf("%d",&d);
sum=sum+s[i];
if(sum<d || s[1]>d)
else
sumofsub(0,1,sum);
return 0;
}
output
126
135
18
234
27
36
45
9
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.
Step 1: Implement the Selection Sort Algorithm
The Selection Sort algorithm works by repeatedly finding the minimum element from the
unsorted part and putting it at the beginning.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i, j, min_idx;
arr[min_idx] = arr[i];
arr[i] = temp;
int main()
{
int n;
if (n <= 5000)
if (arr == NULL)
generateRandomNumbers(arr, n);
selectionSort(arr, n);
free(arr);
return 0;
}
Step 2: Measure Time Taken
The above program generates n random numbers, sorts them using the Selection Sort algorithm,
and measures the time taken for the sorting process.
Step 3: Run the Program for Various Values of n
To collect data, run the program with different values of n greater than 5000, such as 6000,
7000, 8000, etc., and record the time taken for each.
Step 4: Plot the Results
You can use a graphing tool like Python with matplotlib to plot the results.
# data collected
time_taken = [0.031000, 0.034000, 0.047000, 0.052000, 0.077000] # replace with actual times
recorded
plt.plot(n_values, time_taken, marker='o')
plt.grid(True)
plt.show()
output
********************************************************************
********************************************************************
********************************************************************
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.
Step 1: Implement the Quick Sort Algorithm
Quick Sort is a divide-and-conquer algorithm that works by selecting a ‘pivot’ element and
partitioning the array into elements less than and greater than the pivot.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int t = *a;
*a = *b;
*b = t;
return (i + 1);
quickSort(arr, pi + 1, high);
int main()
int n;
if (n <= 5000)
if (arr == NULL)
{
generateRandomNumbers(arr, n);
quickSort(arr, 0, n - 1);
free(arr);
return 0;
}
Step 2: Measure Time Taken
This program generates n random numbers, sorts them using the Quick Sort algorithm, and
measures the time taken for the sorting process.
Step 3: Run the Program for Various Values of n
To collect data, run the program with different values of n greater than 5000, such as 6000,
7000, 8000, etc., and record the time taken for each if you didn’t get time then increase the value
of n for example 20000, 40000, 60000 etc….
Step 4: Plot the Results
You can use a graphing tool like Python with matplotlib to plot the results.
time_taken = [0.0000, 0.015000, 0.011000, 0.003000, 0.015000] # replace with actual times
recorded
plt.grid(True)
plt.show()
output
********************************************************************
Enter number of elements: 20000
********************************************************************
********************************************************************
********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = L[i];
i++;
else
arr[k] = R[j];
j++;
}
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
free(L);
free(R);
}
// Function to implement Merge Sort
int main()
{
int n;
scanf("%d", &n);
if (n <= 5000)
if (arr == NULL)
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
mergeSort(arr, 0, n - 1);
free(arr);
return 0;
}
Step 2: Measure Time Taken
This program generates n random numbers, sorts them using the Merge Sort algorithm, and
measures the time taken for the sorting process.
Step 3: Run the Program for Various Values of n
To collect data, run the program with different values of n greater than 5000, such as 6000,
7000, 8000, etc., and record the time taken for each.
Step 4: Plot the Results
You can use a graphing tool like Python with matplotlib to plot the results.
n_values = [6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 15000]
time_taken = [0.000709, 0.000752, 0.000916, 0.001493, 0.001589, 0.002562, 0.001944,
0.002961, 0.003563] # Replace with actual times recorded
plt.grid(True)
plt.show()
output
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
Enter number of elements: 13000
********************************************************************
12. 12. Design and implement C/C++ Program for N Queen’s problem using Backtracking.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
printf("\n");
int i, j;
if (board[row][i])
return false;
}
// Check upper diagonal on left side
if (board[i][j])
return false;
if (board[i][j])
return false;
return true;
}
// A recursive utility 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, N, i, col))
board[i][col] = 1;
return true;
}
// If placing queen in board[i][col] doesn't lead to a solution,
board[i][col] = 0; // BACKTRACK
// If the queen cannot be placed in any row in this column col, then return false
return false;
// It returns false if queens cannot be placed, otherwise, return true and prints the placement of
queens
bool solveNQ(int N)
board[i][j] = 0;
if (!solveNQUtil(board, N, 0))
free(board[i]);
free(board);
return false;
printSolution(board, N);
free(board[i]);
}
free(board);
return true;
int main()
int N;
scanf("%d", &N);
solveNQ(N);
return 0;
}
o/p
************************OUTPUT-1************************
##Q#
Q###
###Q
#Q##
************************OUTPUT-2************************
Enter the number of queens: 3
Solution does not exist