ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FractionalKnapsack.h File Reference

The real (fractional) knapsack of section 14.1: a fraction \( x_i \in [ 0 , 1 ] \) of each object, of largest profit \( \sum_i p_i x_i \) and volume \( \sum_i v_i x_i \le C \). More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <numeric>
#include <vector>
#include "ADS/optimization/impl/FractionalKnapsack.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::vector< double > ADS::fractionalKnapsack (const std::vector< double > &p, const std::vector< double > &v, double C)
 the fractions \( x \) of the objects, by decreasing \( p_i / v_i \) (the zaino() of the book)

Detailed description

The real (fractional) knapsack of section 14.1: a fraction \( x_i \in [ 0 , 1 ] \) of each object, of largest profit \( \sum_i p_i x_i \) and volume \( \sum_i v_i x_i \le C \).

The greedy choice by decreasing specific profit \( p_i / v_i \) is optimal: the most profitable objects per unit of volume go in whole, the next one in part, the others not; \( O( n \log n ) \) time for the sort. By decreasing profit or by increasing volume it is not, and neither is any of them for the 0-1 knapsack (exercise 14.1).

The contracts [see Contract.h]: profits and volumes of the same objects, the volumes positive, the profits and the capacity not negative; the fractions are in \( [ 0 , 1 ] \) and fill at most the capacity. The operations are defined in FractionalKnapsack.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 14.1, https://algoritmicamente.it/materiali/libro/capitoli/cap14.pdf
G. B. Dantzig, Discrete-variable extremum problems, Operations Research 5(2), 266-288, 1957, doi:10.1287/opre.5.2.266
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa