Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views26 pages

Program 1

The document contains multiple C programs implementing various algorithms including Minimum Spanning Tree (Prim's and Kruskal's), Floyd-Warshall for shortest paths, Dijkstra's algorithm, Topological Sorting, Knapsack problem, and sorting algorithms (Selection, Quick, and Merge Sort). Each program prompts the user for input, processes data, and outputs results related to the respective algorithm. The programs demonstrate fundamental concepts in graph theory, optimization, and algorithm efficiency.

Uploaded by

gurup0710
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)
3 views26 pages

Program 1

The document contains multiple C programs implementing various algorithms including Minimum Spanning Tree (Prim's and Kruskal's), Floyd-Warshall for shortest paths, Dijkstra's algorithm, Topological Sorting, Knapsack problem, and sorting algorithms (Selection, Quick, and Merge Sort). Each program prompts the user for input, processes data, and outputs results related to the respective algorithm. The programs demonstrate fundamental concepts in graph theory, optimization, and algorithm efficiency.

Uploaded by

gurup0710
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/ 26

Program1

#include<stdio.h>
#include<conio.h>
int ne=1, min_cost=0;
void main()
{
int n,i,j,min,a,u,b,v,cost[20][20],parent[20];
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
for(i=1;i<=n;i++)
parent[i]=0;
printf("\nThe Edges of spanning tree are:\n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}} }
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("Edge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d\n",min_cost);
}
Program 2
#include<stdio.h>
int ne=1,min_cost=0;
void main()
{
int n,i,j,min,cost[20][20],a,u,b,v,source,visited[20];
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the cost matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the root node:");
scanf("%d",&source);
visited[source]=1;
printf("\nMinimum cost spanning tree is: \n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
} }}
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d\n ",min_cost);
getch();
}
Program 3a
#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][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 value of n: ");
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");
}}
Program 3b
#include<stdio.h>
void warshall(int[10][10],int);
void main()
{
int a[10][10],i,j,n;
printf("Enter the number of nodes:");
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]);
warshall(a,n);
}
void warshall(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(j=1;j<=n;j++)
{
for(i=1;i<=n;i++)
{
if((p[i][j]==0) && (p[i][k]==1)&& (p[k][j]==1))
{
p[i][j]=1;
} }}}
printf("\nThe path matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",p[i][j]);
}
printf("\n");
}}
Program 4
#include<stdio.h>
void dij(int,int [20][20],int [20],int [20],int);
void main()
{
int i,j,n,visited[20],source,cost[20][20],d[20];
printf("Enter no of vertices:");
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]);
} }
printf("\nEnter the source node:");
scanf("%d",&source);
dij(source,cost,visited,d,n);
for(i=1;i<=n;i++)
if(i!=source)
printf("\nShortest path from %d to %d is %d",source,i,d[i]);
}
void dij(int source, int cost[20][20],int visited[20],int d[20],int n)
{
int i,j,min,u,w;
for(i=1;i<=n;i++)
{
visited[i]=0;
d[i]=cost[source][i];
}
visited[source]=1;
d[source]=0;
for(j=2;j<=n;j++)
{
min=999;
for(i=1;i<=n;i++)
{
if(!visited[i])
{
if(d[i]<min)
{
min=d[i];
u=i;
}} }
visited[u]=1;
for(w=1;w<=n;w++)
{
if(cost[u][w]!=999 && visited[w]==0)
{
if(d[w]>cost[u][w]+d[u])
d[w]=cost[u][w]+d[u];
}}}}
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(); }
Program 6
#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("Enter 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;
}
Program 7
#include <stdio.h>
int main()
{
float weight[50], profit[50], ratio[50], totalvalue = 0, temp, capacity, amount;
int i, j, n;
printf("Enter the number of items: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter weight and profit for item[%d]: ", i);
scanf("%f %f", &weight[i], &profit[i]);
ratio[i] = profit[i] / weight[i];
}
printf("\nEnter the capacity of Knapsack: ");
scanf("%f", &capacity);
for(i = 0; i < n - 1; i++)
(Descending Order)
{
for(j = i + 1; j < n; j++)
{
if(ratio[i] < ratio[j]) {
temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
temp = weight[i];
weight[i] = weight[j];
weight[j] = temp;
temp = profit[i];
profit[i] = profit[j];
profit[j] = temp;
}}}
printf("\nKnapsack problems using Greedy Algorithm");
for(i = 0; i < n; i++)
{
if(weight[i] <= capacity)
{
capacity - = weight[i];
totalvalue + = profit[i];
} }}
else
{
totalvalue + = ratio[i] * capacity;
break;
printf("\nThe maximum value is: %f\n", totalvalue);
return 0;
}
Program 8
#include<stdio.h>
#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
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-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k]);
}}
int main()
{
int i,n,sum=0;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the set in increasing order:");
for(i=1; i<=n; i++)
scanf("%d",&s[i]);
printf("\nEnter the max subset value:");
scanf("%d",&d);
for(i=1; i<=n; i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
return 0;
}
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;
}}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void generateRandomNumbers(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
{
arr[i] = rand() % 10000;
}}
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();
selectionSort(arr, n);
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;
}
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);
int j;
for (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);
}
}
int i;
void generateRandomNumbers(int arr[], int n)
{
for (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;
}
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)
{
int i;
for (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();
int i;
for (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;
}
Program 12
#include <stdio.h>
#include <stdlib.h>
void nqueens(int);
int place(int [], int);
void printsolution(int, int []);
int main()
{
int n;
printf("Enter the number of queens: ");
scanf("%d", &n);
nqueens(n);
return 0;
}
void nqueens(int n)
{
int x[20], count = 0, k = 1;
x[k] = 0;
while (k != 0)
{
x[k] = x[k] + 1;
while (x[k] <= n && !place(x, k))
x[k] = x[k] + 1;
if (x[k] <= n)
{
if (k == n)
{
}
count++;
printf("\nSolution %d\n", count);
printsolution(n, x);
else
{
k++;
x[k] = 0;
}
}
else
{
k--;
}}}
int place(int x[], int k)
{
int i;
for (i = 1; i < k; i++)
{
if (x[i] == x[k] || abs(x[i] - x[k]) == abs(i - k))
return 0;
}
return 1;
}
void printsolution(int n, int x[])
{
int i, j;
char c[10][10];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
c[i][j] = 'X';
}
for (i = 1; i <= n; i++)
c[i - 1][x[i] - 1] = 'Q';
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%c\t", c[i][j]);
}
printf("\n");
}
}

You might also like