Progs1 8
Progs1 8
#include<stdio.h>
void main() {
int n, i, j, a, b, u, v, min, mincost=0, count=1;
int parent[10] = {0}, cost[10][10];
while(count<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;
}
}
}
while(parent[u]) u=parent[u];
while(parent[v]) v=parent[v];
if(u!=v) {
printf("\nEdges(%d,%d) = %d\n", a, b, min);
count++;
mincost += min;
parent[v] = u;
}
cost[a][b] = cost[b][a]=999;
}
printf("minimum cost = %d",mincost);
}
2) Design and implement C program to find Minimum Cost Spanning Tree of a given
connected undirected graph using prims algorithm.
#include <stdio.h>
void main() {
int n, i, j, a, b, u, v;
int s[10], cost[10][10], min, mincost=0, ne=0;
while(ne <= n) {
for(i=1, min=999; i<=n; i++) {
for(j=1; j<=n; j++) {
if(cost[i][j]<min)
if(s[i] == 0) continue;
else {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
if(s[u] == 0 || s[v] == 0) {
printf("edge (%d,%d)=%d\n",a, b, min);
mincost += min;
s[b]=1;
}
ne++;
cost[a][b] = cost[b][a] = 999;
printf("minimum cost : %d\n",mincost);
}
}
3.a) Design and implement C program to solve All-Pairs shortest problem using
Floyd’s algorithm
#include <stdio.h>
void main() {
int n, a[10][10], i, j, k;
#include<stdio.h>
void main() {
int i, j, k, n, a[10][10];
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
scanf("%d",&a[i][j]);
}
}
#include<stdio.h>
visited[source] = 1;
d[source] = 0;
visited[u] = 1;
void main() {
int i, j, n, source, cost[20][20];
#include<stdio.h>
void find_indegree() {
int j,i;
for(j=1;j<=n;j++) {
indegree[j] = 0;
for(i=1;i<=n;i++)
indegree[j] += a[i][j];
}
}
void topology() {
int i,u,v,t[10],s[10],top=-1,k=1;
find_indegree();
for(i=1;i<=n;i++) {
if(indegree[i] == 0)
s[++top] = i;
}
while(top!=-1) {
u = s[top--];
t[k++] = u;
for(v=1;v<=n;v++)
if(a[u][v] == 1 && --indegree[v] == 0)
s[++top] = v;
}
void main() {
int i,j;
topology();
}
6) Design and implement C program to solve 0/1 knapsack problem using Dynamic
programming method
#include<stdio.h>
#include<ctype.h>
int v[10][10],w[10],p[10];
void main() {
int i,j,n,c,opt,x[10] = {0};
opt=knap(n,c);
printf("\nThe optional solution is %d\n",opt);
i=n;
j=c;
while(i!=0&&j!=0) {
if(v[i][j] != v[i-1][j]) {
x[i] = 1;
j = j-w[i];
}
i = i-1;
}
}
7) Design and implement C program to solve discrete knapsack and continuous
Knapsack problems using greedy approximation method
#include <stdio.h>
temp = weight[j];
weight[j] = weight[j + 1];
weight[j + 1] = temp;
temp = profit[j];
profit[j] = profit[j + 1];
profit[j + 1] = temp;
}
}
}
}
if (i < n)
x[i] = (float)capacity / weight[i];
int main() {
float weight[10], profit[10], ratio[10];
int i, n, capacity;
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>
if(curSum+set[curIdx] == targetSum) {
printf("Subset : ");
for(i=1; i<=curIdx; i++)
if(included[i] == 1)
printf("\t%d", set[i]);
printf("\n");
}
else {
if(curSum+set[curIdx] + set[curIdx+1] <= targetSum)
findSubset(curSum+set[curIdx], curIdx+1);
if(curSum+sum-set[curIdx] >= targetSum && curSum+set[curIdx+1] <=
targetSum) {
included[curIdx] = 0;
findSubset(curSum, curIdx+1);
}
}
}
void main() {
int n, i;
#include <stdio.h>
#include <stdbool.h>
int board[10];
int n;
void printSolution() {
int i, j;
printf("Solution:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (board[i] == j) {
printf("Q\t");
} else {
printf(".\t");
}
}
printf("\n");
}
printf("\n");
}
int main() {
printf("Enter the number of queens: ");
scanf("%d", &n);
solveNQueens(0);
return 0;
}