0% found this document useful (0 votes)
35 views12 pages

Progs1 8

Program from the manual to the day of the day Bhai ki
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)
35 views12 pages

Progs1 8

Program from the manual to the day of the day Bhai ki
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/ 12

1) Design and implement C program to find Minimum cost spanning tree of a given

connected undirected graph using Kruskal’s algorithm.

#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];

printf("Enter the no. of verticies:\t");


scanf("%d",&n);

printf("Enter 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;
}

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;

printf("Enter the number of vertices :\t");


scanf("%d",&n);

printf("Enter 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;
}

for(i=2; i<=n; i++) s[i]=0;


s[1]=1;

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;

printf("Enter the number of nodes: ");


scanf("%d", &n);

printf("\nEnter the cost adjacency matrix:\n");


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}

for (k = 0; k < n; k++) {


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i][k] + a[k][j] < a[i][j]) {
a[i][j] = a[i][k] + a[k][j];
}
}
}
}

printf("\nThe distance matrix is:\n");


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
3.b) Design and implement C program to find the transitive closure using
Warshal’s algorithm

#include<stdio.h>

void main() {
int i, j, k, n, a[10][10];

printf("Enter the number of vertices\t:");


scanf("%d",&n);

printf("Enter the adjacency matrix\n");

for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
scanf("%d",&a[i][j]);
}
}

for(k=1; k<=n; k++) {


for(i=1; i<=n; i++) {
for(j=1; j<=n; j++) {
if((a[i][k] == 1) && (a[k][j] == 1))
a[i][j] = 1;
}
}
}

printf("transitive closure is\n");


for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
printf("%d\t", a[i][j]);
printf("\n");
}
}
4) Design and implement C program to find shortest paths from a given matrix in
a weighted connected graph to other vertices using Dijkstra’s algorithm.

#include<stdio.h>

void dij(int source, int cost[20][20], int n) {


int i, j, min, u, w, visited[20], d[20];

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] && 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 && d[w] > cost[u][w] + d[u]) {
d[w] = cost[u][w] + d[u];
}
}

for(i=1; i<=n; i++)


if(i != source) {
printf("\nShortest path from %d to %d is %d", source, i, d[i]);
}
}

void main() {
int i, j, n, source, cost[20][20];

printf("Enter the number of vertices:");


scanf("%d", &n);

printf("Enter 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 vertex:");


scanf("%d", &source);

dij(source, cost, n);


}
5) Design and implement C program to obtain the Topological ordering of vertices
in a given digraph.

#include<stdio.h>

int a[10][10], n, indegree[10];

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;
}

printf("The topogolical sequence is :\n");


for(i=1;i<=n;i++)
printf("%d\t", t[i]);
}

void main() {
int i,j;

printf("Enter the number of vertices : ");


scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");


for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
scanf("%d",&a[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];

int max (int a,int b) {


return((a>b)?a:b);
}

int knap(int n,int c) {


int i,j;
for(i=0;i<=n;i++) {
for(j=0;j<=c;j++) {
if(i==0||j==0)
v[i][j]=0;
else if(j<w[i])
v[i][j] = v[i-1][j];
else
v[i][j] = max((v[i-1][j]),(p[i]+v[i-1][j-w[i]]));
}
}

printf("\n matrix is:\n");


for(i=0;i<=n;i++) {
printf("\n");
for(j=0;j<=c;j++) {
printf("%d\t",v[i][j]);
}
}
return v[n][c];
}

void main() {
int i,j,n,c,opt,x[10] = {0};

printf("\nEnter the number of items\n");


scanf("%d",&n);

printf("\nEnter the value of weights\n");


for(i=1;i<=n;i++) {
scanf("%d",&w[i]);
}

printf("\nEnter the value for profit\n");


for(i=1;i<=n;i++) {
scanf("%d",&p[i]);
}

printf("\nEnter the value for capacity\n");


scanf("%d",&c);

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>

void displayinfo(int n, float weight[], float profit[], float ratio[]) {


printf("ITEM\tWEIGHT\tPROFIT\tRATIO\n");
for (int i = 0; i < n; i++)
printf("%d\t%.2f\t%.2f\t%.2f\n", i+1, weight[i], profit[i], ratio[i]);
printf("\n");
}

void sortArray(int n, float weight[], float profit[], float ratio[]) {


int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (ratio[j] < ratio[j + 1]) {
float temp = ratio[j];
ratio[j] = ratio[j + 1];
ratio[j + 1] = temp;

temp = weight[j];
weight[j] = weight[j + 1];
weight[j + 1] = temp;

temp = profit[j];
profit[j] = profit[j + 1];
profit[j + 1] = temp;
}
}
}
}

void knapsack(int n, float weight[], float profit[], int capacity) {


float x[10] = {0}, total_profit = 0;
int i;

for (i = 0; i < n; i++) {


if (weight[i] > capacity)
break;
else {
x[i] = 1;
total_profit += profit[i];
capacity -= weight[i];
}
}

if (i < n)
x[i] = (float)capacity / weight[i];

total_profit += x[i] * profit[i];


printf("\nThe result is = ");

for (i = 0; i < n; i++)


printf("%.2f\t", x[i]);
printf("\nMaximum profit is %.2f\n", total_profit);
}

int main() {
float weight[10], profit[10], ratio[10];
int i, n, capacity;

printf("Enter the total number of items:\n");


scanf("%d", &n);
printf("Enter the weight of each item:\n");
for (i = 0; i < n; i++)
scanf("%f", &weight[i]);

printf("Enter the profit of each item:\n");


for (i = 0; i < n; i++)
scanf("%f", &profit[i]);

printf("Enter the knapsack capacity:\n");


scanf("%d", &capacity);

for (i = 0; i < n; i++)


ratio[i] = profit[i] / weight[i];

printf("Information about knapsack are:\n");


displayinfo(n, weight, profit, ratio);

printf("Capacity = %d\n", capacity);


sortArray(n, weight, profit, ratio);

printf("\nDetails after sorting items:\n");


displayinfo(n, weight, profit, ratio);

knapsack(n, weight, profit, capacity);


printf("\n");

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>

int set[10], included[10], sum=0, targetSum;

void findSubset(int curSum, int curIdx) {


int i = 1;
included[curIdx] = 1;

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;

printf("\n Enter the size of the set : ");


scanf("%d", &n);

printf("\nEnter the set in increasing order : \n");


for(i=1; i<=n; i++) {
scanf("%d", &set[i]);
}

printf("\nEnter the value of Target Sum : \n");


scanf("%d", &targetSum);

for(i=1; i<=n; i++) {


sum = sum + set[i];
}

if(sum < targetSum || set[1] > targetSum) {


printf("\nNo subset possible");
}
else {
findSubset(0, 1);
}
}
12) Design and implement C program for N Queen problem using Backtracking

#include <stdio.h>
#include <stdbool.h>

int board[10];
int n;

bool isSafe(int row, int col) {


for (int i = 0; i < row; i++) {
if (board[i] == col || board[i] - i == col - row || board[i] + i == col
+ row) {
return false;
}
}
return true;
}

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");
}

void solveNQueens(int row) {


int col;
if (row == n) {
printSolution();
return;
}

for (col = 0; col < n; col++) {


if (isSafe(row, col)) {
board[row] = col;
solveNQueens(row + 1);
}
}
}

int main() {
printf("Enter the number of queens: ");
scanf("%d", &n);
solveNQueens(0);
return 0;
}

You might also like