0% found this document useful (0 votes)
5 views7 pages

9-12 Ada

The document contains multiple C/C++ programs that solve various algorithmic problems, including the 0/1 Knapsack problem using dynamic programming, discrete and continuous Knapsack problems using a greedy approach, subset sum problem, and the N Queen's problem using backtracking. Each program includes user input for parameters and outputs the results based on the implemented algorithms. The code snippets demonstrate the application of different programming techniques to solve these classic computational problems.

Uploaded by

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

9-12 Ada

The document contains multiple C/C++ programs that solve various algorithmic problems, including the 0/1 Knapsack problem using dynamic programming, discrete and continuous Knapsack problems using a greedy approach, subset sum problem, and the N Queen's problem using backtracking. Each program includes user input for parameters and outputs the results based on the implemented algorithms. The code snippets demonstrate the application of different programming techniques to solve these classic computational problems.

Uploaded by

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

9.

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 of the items:\n");

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

scanf("%d",&p[i]);

printf("\nEnter Weight of the items:\n");

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

scanf("%d",&w[i]);

max_profit=knap(1,m);

printf("\nMax profit=%d",max_profit);

return 0;

}
10 Design and implement C/C++ Program to solve discrete Knapsack
and continuous Knapsack problems using greedy approximation
method.
#include <stdio.h>

#include <stdlib.h>

// Structure to represent items

struct Item {

int value;

int weight;

double ratio; // Value-to-weight ratio for sorting

};

// Comparison function for sorting items based on ratio in descending order

int compare(const void *a, const void *b) {

struct Item *item1 = (struct Item *)a;

struct Item *item2 = (struct Item *)b;

double ratio1 = item1->ratio;

double ratio2 = item2->ratio;

if (ratio1 > ratio2) return -1;

if (ratio1 < ratio2) return 1;

return 0;

} //Function to solve discrete Knapsack problem

void discreteKnapsack(struct Item items[], int n, int capacity) {

int i, j;

int dp[n + 1][capacity + 1];

// Initialize the DP table

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

for (j = 0; j <= capacity; j++) {

if (i == 0 || j == 0)

dp[i][j] = 0;
else if (items[i - 1].weight <= j)

dp[i][j] = (items[i - 1].value + dp[i - 1][j - items[i - 1].weight] > dp[i - 1][j]) ?

(items[i - 1].value + dp[i - 1][j - items[i - 1].weight]) : dp[i - 1][j];

Else

dp[i][j] = dp[i - 1][j];

}p

rintf("Total value obtained for discrete knapsack: %d\n", dp[n][capacity]);

} //

Function to solve continuous Knapsack problem

void continuousKnapsack(struct Item items[], int n, int capacity) {

int i;

double totalValue = 0.0;

int remainingCapacity = capacity;

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

if (remainingCapacity >= items[i].weight)

totalValue += items[i].value;

remainingCapacity -= items[i].weight;

}e

lse

totalValue += (double)remainingCapacity / items[i].weight * items[i].value;

break;

}p

rintf("Total value obtained for continuous knapsack: %.2lf\n", totalValue);

} in

t main()

{
int n, capacity, i;

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

scanf("%d", &n);

struct Item items[n];

printf("Enter the capacity of the knapsack: ");

scanf("%d", &capacity);

printf("Enter the value and weight of each item:\n");

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

scanf("%d %d", &items[i].value, &items[i].weight);

items[i].ratio = (double)items[i].value / items[i].weight;

} //

Sort items based on value-to-weight ratio

qsort(items, n, sizeof(struct Item), compare);

discreteKnapsack(items, n, capacity);

continuousKnapsack(items, n, capacity);

return 0;

11 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>

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

}
12 Design and implement C/C++ Program for N Queen's problem
using Backtracking.
#include<stdio.h>

#include<math.h>

#include<stdlib.h>

int a[30], count = 0;

int place(int pos) {

int i;

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

if((a[i] == a[pos]) || (abs(a[i] - a[pos]) == abs(i - pos)))

return 0;

return 1;

void print_sol(int n) {

int i, j;

count++;

printf("\nSolution #%d:\n", count);

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

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

if(a[i] == j)

printf("Q\t");

else

printf("*\t");

printf("\n");

void queen(int n) {

int k = 1;
a[k] = 0;

while(k != 0) {

a[k] = a[k] + 1;

while((a[k] <= n) && !place(k))

a[k]++;

if(a[k] <= n) {

if(k == n)

print_sol(n);

else {

k++;

a[k] = 0;

} else {

k--;

int main() {

int n;

printf("Enter the number of Queens\n");

scanf("%d", &n);

queen(n);

printf("\nTotal solutions = %d\n", count);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy