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

SCP6

The document discusses three optimization problems: the knapsack problem, exam scheduling problem, and the eight queens puzzle. For each problem, it provides code to model and solve the problem using recursion or backtracking.

Uploaded by

alexserban150
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SCP6

The document discusses three optimization problems: the knapsack problem, exam scheduling problem, and the eight queens puzzle. For each problem, it provides code to model and solve the problem using recursion or backtracking.

Uploaded by

alexserban150
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Problem 1: The Knapsack Problem

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

unsigned int id;

double weight;

double profit;

double profit_density;

} item;

static int compare_items(const item *item1, const item *item2) {

return (item1->profit_density > item2->profit_density) ? -1 : (item1->profit_density < item2-


>profit_density) ? 1 : 0;

static double profit_bound(const item *items, size_t n, double capacity, double current_weight, double
current_profit, unsigned int level) {

double remaining_capacity = capacity - current_weight;

double bound = current_profit;

unsigned int lvl = level;

while (lvl < n && items[lvl].weight <= remaining_capacity) {

remaining_capacity -= items[lvl].weight;

bound += items[lvl].profit;

lvl++;

}
if (lvl < n) {

bound += items[lvl].profit_density * remaining_capacity;

return bound;

static void knapsack_recursive(const item *items, size_t n, double capacity, unsigned int
*current_knapsack, double *current_weight,

double *current_profit, unsigned int *max_knapsack, double *max_profit, unsigned


int level) {

if (level == n) {

*max_profit = *current_profit;

memcpy(max_knapsack, current_knapsack, n * sizeof(unsigned int));

return;

if (*current_weight + items[level].weight <= capacity) {

*current_weight += items[level].weight;

*current_profit += items[level].profit;

current_knapsack[items[level].id] = 1;

knapsack_recursive(items, n, capacity, current_knapsack,

current_weight, current_profit, max_knapsack, max_profit, level + 1);

*current_weight -= items[level].weight;

*current_profit -= items[level].profit;

current_knapsack[items[level].id] = 0;

}
if (profit_bound(items, n, capacity, *current_weight, *current_profit, level + 1) > *max_profit) {

knapsack_recursive(items, n, capacity, current_knapsack,

current_weight, current_profit, max_knapsack, max_profit, level + 1);

double knapsack(const double *weights, const double *profits, size_t n, double capacity, unsigned int
**max_knapsack) {

double current_weight = 0.0;

double current_profit = 0.0;

double max_profit = 0.0;

unsigned int i;

item *items = malloc(n * sizeof(item));

unsigned int *current_knapsack = calloc(n, sizeof(unsigned int));

*max_knapsack = malloc(n * sizeof(unsigned int));

if (!(items && current_knapsack && *max_knapsack)) {

free(items);

free(current_knapsack);

free(*max_knapsack);

*max_knapsack = NULL;

return 0;

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

items[i].id = i;

items[i].weight = weights[i];
items[i].profit = profits[i];

items[i].profit_density = profits[i] / weights[i];

qsort(items, n, sizeof(item), (int (*)(const void *, const void *))compare_items);

knapsack_recursive(items, n, capacity, current_knapsack,

&current_weight, &current_profit, *max_knapsack, &max_profit, 0);

free(items);

free(current_knapsack);

return max_profit;

int main(void) {

double weights[] = {3, 5, 2, 1};

double profits[] = {9, 10, 7, 4};

const size_t n = sizeof(profits) / sizeof(profits[0]);

const double capacity = 7;

unsigned int *max_knapsack;

double max_profit = knapsack(weights, profits, n, capacity, &max_knapsack);

unsigned int i;

printf("Profit: %.2f\n", max_profit);

printf("Knapsack contains:\n");

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

if (max_knapsack[i] == 1) {
printf("Item %u with weight %.2f and profit %.2f\n", i, weights[i], profits[i]);

free(max_knapsack);

return 0;

Problem 2: The Exam Test Problem


#include <stdio.h>

#define MAX_QUESTIONS 100

void RecursiveExTest(int k, int nc, int pc, int rest, int n, int a, int b, int p[], int q, int X[]) {

if (k <= n) {

if (nc + 1 <= b && pc + p[k - 1] <= q) {

X[k - 1] = 1;

RecursiveExTest(k + 1, nc + 1, pc + p[k - 1], rest - p[k - 1], n, a, b, p, q, X);

if (nc + (n - k) >= a && pc + rest >= p) {

X[k - 1] = 0;

RecursiveExTest(k + 1, nc, pc, rest - p[k - 1], n, a, b, p, q, X);

} else {

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

printf("%d ", X[i]);

printf("\n");
}

void ExamTest() {

int n, a, b, p[MAX_QUESTIONS], q;

int X[MAX_QUESTIONS];

do {

printf("Enter n, p(i), a, b, p, q: ");

scanf("%d", &n);

int sumP = 0;

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

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

sumP += p[i];

scanf("%d%d%d", &a, &b, &q);

} while (!(n >= b && b >= a && sumP >= q && q >= p));

RecursiveExTest(1, 0, 0, sumP, n, a, b, p, q, X);

int main() {

ExamTest();

return 0;

Problem 3: The 8 queens puzzle


#include <stdio.h>
typedef struct {

int row;

int col;

} Queen;

int isUnderAttack(Queen queen[], int currentQueen) {

for (int i = 0; i < currentQueen; i++) {

if (queen[i].row == queen[currentQueen].row ||

queen[i].col == queen[currentQueen].col ||

(queen[i].row - queen[currentQueen].row) == (queen[i].col - queen[currentQueen].col) ||

(queen[currentQueen].row - queen[i].row) == (queen[i].col - queen[currentQueen].col)) {

return 1;

return 0;

int solveQueens(int boardSize, Queen queen[]) {

int count = 0;

int i = 0;

while (i >= 0) {

queen[i].row++;

if (queen[i].row >= boardSize) {

queen[i].row = -1;

i--;

} else {
while (isUnderAttack(queen, i)) {

queen[i].row++;

if (queen[i].row >= boardSize) {

queen[i].row = -1;

i--;

break;

if (i == boardSize - 1) {

count++;

queen[i].row = -1;

i--;

} else {

i++;

return count;

int main() {

const int boardSize = 8;

Queen queens[boardSize];

for (int i = 0; i < boardSize; i++) {

queens[i].row = -1;
queens[i].col = i;

int solutions = solveQueens(boardSize, queens);

printf("Number of solutions for the 8 Queens problem: %d\n", solutions);

return 0;

You might also like