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

The pseudopolynomial algorithms of section 19.1 and of the exercises 19.1 to 19.5. More...

#include "ADS/Contract.h"
#include "ADS/complexity/Problems.h"
#include <algorithm>
#include <map>
#include <numeric>
#include <optional>
#include <utility>
#include <vector>
#include "ADS/intractable/impl/Pseudopolynomial.tpp"

Go to the source code of this file.

Classes

struct  ADS::Piece
 an interval \( [ \mathit{start} , \mathit{end} ) \) of the program on a processor More...

Namespaces

namespace  ADS

Functions

template<class Bits = std::vector< bool >>
bool ADS::subsetSumDP (const std::vector< long > &A, long k, std::vector< std::vector< bool > > *rows=nullptr)
 whether some elements of \( A \) sum to \( k \) (false if \( k < 0 \)), by the vector \( M \) with \( M[ j ] \) true if some of \( A[ 0 .. i ] \) sum to \( j \); the vector \( M \) after each element goes to *rows, if given, the first one before any; \( O( n k ) \) time; \( M \) is a vector of Bits, std::vector< bool > by default, or boost::dynamic_bitset, or BitVector of ADS [see BitVector.h], with the same cells written
std::optional< Nodes > ADS::subsetSumSet (const std::vector< long > &A, long k)
 the places of a subset of \( A \) of sum \( k \), by the table of all the rows, \( O( n k ) \) time and space, or nothing
std::optional< Nodes > ADS::partitionDP (const std::vector< long > &A)
 PARTITION by dynamic programming: the places of a half of \( A \) of half the sum, or nothing (exercise 19.1).
std::optional< std::vector< int > > ADS::schedulingDP (const std::vector< long > &t, int m, long d)
 MULTIPROCESSOR SCHEDULING by dynamic programming on the vectors of the loads of the \( m \) processors, each at most \( d \): the processor of each program, or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0); \( O( n ( d + 1 )^m ) \) time (exercise 19.2).
std::pair< double, std::vector< std::vector< Piece > > > ADS::mcNaughton (const std::vector< double > &t, int m)
 the rule of McNaughton: the length \( d \) of the schedule and the pieces of each processor; the programs fill a processor up to \( d \) and go on at time 0 of the next one, and the two pieces of a program do not overlap because \( t[ i ] \le d \); \( O( n + m ) \) time (exercise 19.3)
std::optional< std::vector< std::vector< Piece > > > ADS::realTimeScheduling (const std::vector< double > &C, const std::vector< double > &T, int m)
 REAL-TIME SCHEDULING without priority rule: the tasks of time \( C[ i ] \) every \( T[ i ] \) run on \( m \) processors if and only if \( C[ i ] \le T[ i ] \) and \( \sum_i C_i / T_i \le m \); the schedule of a period of length 1, task \( i \) for the fraction \( C[ i ] / T[ i ] \) of it, or nothing (exercise 19.4).
std::optional< std::vector< int > > ADS::bottino (const std::vector< long > &P)
 the thief of each jewel of price \( P[ i ] \), so that the three thieves take \( m / 3 \) each, or nothing; \( S[ i ][ j ][ h ] \) tells whether the first \( i \) jewels give \( j \) to the first and \( h \) to the second thief, and the prices are not negative, so that \( j \) and \( h \) range in \( [ 0 , m / 3 ] \); \( O( n m^2 ) \) time (exercise 19.5)

Detailed description

The pseudopolynomial algorithms of section 19.1 and of the exercises 19.1 to 19.5.

An algorithm is pseudopolynomial if its time is a polynomial in the size \( d \) of the input and in the largest integer \( \# \) in it: subsetSumDP() takes \( O( n k ) \), polynomial if \( k \) is bounded by a polynomial in \( n \) and exponential in \( d = O( n \log k ) \) otherwise. A problem is strongly NP-complete if it stays NP-complete when \( \# \) is bounded by a polynomial in \( d \) (CLIQUE, TRAVELING SALESMAN, 3-PARTITION); none of them has a pseudopolynomial algorithm unless P = NP (Theorem 19.1), while SUBSET SUM, PARTITION and KNAPSACK are weakly NP-complete and have one.

The scheduling of \( n \) programs of times \( t_i \) on \( m \) processors within \( d \) is pseudopolynomial by dynamic programming on the loads of the processors, \( O( n ( d + 1 )^m ) \); if a program may be split in intervals, the rule of McNaughton fills the processors one after the other up to \( d = \max\{ \max_i t_i , \sum_i t_i / m \} \) in \( O( n ) \) time, and the periodic tasks of REAL-TIME SCHEDULING, with \( t_i = C_i / T_i \) and \( d = 1 \), are scheduled the same way when no priority rule is required.

The contracts [see Contract.h]: the numbers are not negative and the processors at least one; each solution returned is checked as a postcondition, the preemptive schedule of McNaughton included. The operations are defined in Pseudopolynomial.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 19.1 and exercises 19.1-19.5, https://algoritmicamente.it/materiali/libro/capitoli/cap19.pdf
R. McNaughton, Scheduling with deadlines and loss functions, Management Science 6(1), 1-12, 1959, doi:10.1287/mnsc.6.1.1
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa