0% found this document useful (0 votes)
32 views

Edited Ada

Uploaded by

Disha KM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Edited Ada

Uploaded by

Disha KM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

KRUSKALS PROGRAM-1

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v); OUTPUT
if(uni(u,v))
{
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
PRIMS PROGRAM-2
#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;
clrscr();
printf("\n enter n value:");
scanf("%d",&n);
printf("\n enter the graph data:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&c[i][j]);
printf("\n enter the source node:");
scanf("%d",&s);
res=prim(c,n,s);
printf("\n cost=%d",res);
getch();
}
OUTPUT
FLOYDS PROGRAM-3A
#include<stdio.h>
#include<conio.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;;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
OUTPUT
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
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]);
}
getch();
}
WARSHALS PROGRAM-3B

# include <stdio.h>
# include <conio.h>
int n,a[10][10],p[10][10];
void path()
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
p[i][j]=a[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(p[i][k]==1&&p[k][j]==1) p[i][j]=1;
}
void main()
{ int i,j; OUTPUT
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
path();
printf("\nThe path matrix is showm below\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",p[i][j]);
printf("\n");
}
getch();
}
DIJIKSTRAS PROGRAM-4
#include<stdio.h>
#define INF 999
void dijkstra(int c[10][10],int n,int s,int d[10])
{
int v[10],min,u,i,j;
for(i=1; i<=n; i++)
{
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1; i<=n; i++)
{
min=INF;
for(j=1; j<=n; j++)
if(v[j]==0 && d[j]<min)
{
min=d[j];
u=j;
}
v[u]=1;
for(j=1; j<=n; j++)
if(v[j]==0 && (d[u]+c[u][j])<d[j])
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);
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);
dijkstra(c,n,s,d);
for(i=1; i<=n; i++)
printf("\nShortest distance from %d to %d is %d",s,i,d[i]);
return 0;
}

output
TOPOLOGICAL PROGRAM-5
#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();
}

OUTPUT
0/1 KNAPSACK PROGRAMM-6
#include<stdio.h>
int w[10],p[10],n;
int max(int a,int b)
{
return a>b?a:b;
} OUTPUT
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;
}
KNAPSACK GREEDY PROGRAM-7
#include <stdio.h>
#define MAX 50
int p[MAX], w[MAX], x[MAX];
double maxprofit;
int n, m, i,j;
void greedyKnapsack(int n, int w[], int p[], int m) {
double ratio[MAX];
int temp2,currentWeight ;
for (i = 0; i < n; i++) {
ratio[i] = (double)p[i] / w[i];
}
for (i = 0; i < n - 1; i++) {
for ( j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
double temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
temp2 = w[i];
w[i] = w[j];
w[j] = temp2;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}}}
currentWeight = 0;
maxprofit = 0.0;
for (i = 0; i < n; i++) {
if (currentWeight + w[i] <= m) {
x[i] = 1;
currentWeight += w[i];
maxprofit += p[i];
} else {
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
SUBSET PROGRAM-8
#include <stdio.h>
#define MAX_SIZE 10
int set[MAX_SIZE], subset[MAX_SIZE];
int n, i,targetSum, flag = 0;
void display(int count) {
printf("{");
for ( i = 0; i < count; i++) {
printf("%d", subset[i]);
if (i < count - 1) {
printf(", ");
}
}
printf("}\n");
}
void findSubset(int sum, int index, int count) {
if (sum == targetSum) {
flag = 1;
display(count);
return;
}
if (sum > targetSum || index >= n) {
return;
}
subset[count] = set[index];
findSubset(sum + set[index], index + 1, count + 1);
findSubset(sum, index + 1, count);
}
int main() {
printf("Enter the number of elements in the set: ");
scanf("%d", &n);
printf("Enter the elements of the set: ");
for (i = 0; i < n; i++) {
scanf("%d", &set[i]);
}
printf("Enter the target sum: ");
scanf("%d", &targetSum);
printf("Subsets with sum %d:\n", targetSum);
findSubset(0, 0, 0);
if (!flag) {
printf("There is no solution\n");
}
return 0;
}

OUTPUT
SELECTION SORT PROGRAM-9
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j; // Update min_idx if a smaller element is found
}
}

int temp = arr[min_idx];


arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void generateRandomNumbers(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}
void printArray(int arr[], int n) {
printf("[ ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("]\n");
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user
if (n <= 5000) {
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
generateRandomNumbers(arr, n);
printf("Generated elements: ");
printArray(arr, n);
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
printf("Sorted elements: ");
printArray(arr, n);
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
free(arr);
return 0;
}

GNU PLOT
#include <stdio.h>
#define MAX_VALUES 5
int main() {
int n_values[MAX_VALUES] = {6000, 7000, 8000, 9000, 10000};
double time_taken[MAX_VALUES] = {0.031, 0.034, 0.047, 0.052,
0.077};
FILE *gnuplot = popen("gnuplot -persist", "w");
if (gnuplot == NULL) {
perror("gnuplot");
return 1;
}
fprintf(gnuplot, "set title 'Time taken vs N values'\n");
fprintf(gnuplot, "set xlabel 'N values'\n");
fprintf(gnuplot, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplot, "plot '-' with linespoints title 'Data'\n");
for (int i = 0; i < MAX_VALUES; ++i) {
fprintf(gnuplot, "%d %lf\n", n_values[i], time_taken[i]);
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);
pclose(gnuplot);
return 0;
}
QUICK SORT PROGRAM-10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100000;
}
}
int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1;
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
generateRandomNumbers(arr, n);
clock_t start = clock();
quickSort(arr, 0, n - 1);
clock_t end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
free(arr);
return 0;
}
GNU PLOT
#include <stdio.h>
#define MAX_VALUES 5
int main() {
int n_values[MAX_VALUES] = {10000,20000,50000,80000,90000};
double time_taken[MAX_VALUES] = {0.001, 0.0034, 0.005, 0.0082,
0.009};
FILE *gnuplot = popen("gnuplot -persist", "w");
if (gnuplot == NULL) {
perror("gnuplot");
return 1;
}
fprintf(gnuplot, "set title 'Time taken vs N values'\n");
fprintf(gnuplot, "set xlabel 'N values'\n");
fprintf(gnuplot, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplot, "plot '-' with linespoints title 'Data'\n");
for (int i = 0; i < MAX_VALUES; ++i) {
33
fprintf(gnuplot, "%d %lf\n", n_values[i], time_taken[i]);
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);
pclose(gnuplot);
return 0;
}
MERGE SORT PROGRAM-11
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
free(L);
free(R);
}
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void generateRandomArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
arr[i] = rand() % 100000;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1;
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
generateRandomArray(arr, n);
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
mergeSort(arr, 0, n - 1);
}
clock_t end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC /
1000.0;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
free(arr);
return 0;
}
GNU PLOT
#include <stdio.h>
#define MAX_VALUES 5
int main() {
int n_values[MAX_VALUES] = {6000, 7000, 8000, 9000, 10000, 11000,
12000, 13000, 15000};
double time_taken[MAX_VALUES] = {0.000709, 0.000752, 0.000916,
0.001493, 0.001589, 0.002562, 0.001944, 0.002961, 0.003563};
FILE *gnuplot = popen("gnuplot -persist", "w");
if (gnuplot == NULL) {
perror("gnuplot");
return 1;
}
fprintf(gnuplot, "set title 'Time taken vs N values'\n");
fprintf(gnuplot, "set xlabel 'N values'\n");
fprintf(gnuplot, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplot, "plot '-' with linespoints title 'Data'\n");
for (int i = 0; i < MAX_VALUES; ++i) {
fprintf(gnuplot, "%d %lf\n", n_values[i], time_taken[i]);
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);
pclose(gnuplot);
return 0;
}
NQUEENS PROGRAM-12
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int totalSolutions = 0;
void printSolution(int board[], int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i] == j)
printf(" Q ");
else
printf(" - ");
}
printf("\n");
}
}
bool isSafe(int board[], int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i] == col || abs(board[i] - col) == abs(i - row)) {
return false;
}
}
return true;
}
void solveNQUtil(int board[], int row, int N) {
if (row >= N) {
printSolution(board, N);
printf("\n");
totalSolutions++;
return;
}
for (int col = 0; col < N; col++) {
if (isSafe(board, row, col)) {
board[row] = col;
solveNQUtil(board, row + 1, N);
board[row] = -1;
}
}
}
void solveNQ(int N) {
int board[N];
for (int i = 0; i < N; i++) {
board[i] = -1;
}
solveNQUtil(board, 0, N);
}
int main() {
int N;
printf("Enter the number of queens: ");
scanf("%d", &N);
solveNQ(N);
printf("Total solutions: %d\n", totalSolutions);
return 0;
}

You might also like