Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::sat::SortedSubsetSums Class Reference

Yet another variant of FirstFewValues or MaxBoundedSubsetSum. More...

#include <util.h>

Public Member Functions

absl::Span< const int64_t > Compute (absl::Span< const int64_t > elements, int64_t maximum_sum, bool abort_if_maximum_reached=false)
 
absl::Span< const int64_t > SortedSums () const
 Returns the possible subset sums sorted.
 

Detailed Description

Yet another variant of FirstFewValues or MaxBoundedSubsetSum.

Definition at line 506 of file util.h.

Member Function Documentation

◆ Compute()

absl::Span< const int64_t > operations_research::sat::SortedSubsetSums::Compute ( absl::Span< const int64_t > elements,
int64_t maximum_sum,
bool abort_if_maximum_reached = false )

Computes all the possible subset sums in [0, maximum_sum]. Returns them sorted. All elements must be non-negative.

If abort_if_maximum_reached is true, we might not return all possible subset sums as we stop the exploration as soon as a subset sum is equal to maximum_sum. When this happen, we guarantee that the last element returned will be maximum_sum though.

Worst case complexity is in O(2^num_elements) if maximum_sum is large or O(maximum_sum * num_elements) if that is lower.

Todo
(user): We could optimize even further the case of a small maximum_sum.

Optimization: If all the sums in [0, maximum_sum] are already reachable we can abort early since no new reachable sum wil be discovered.

Early abort when asked if we already reached maximum_sum.

We merge sort sums_ and sums_ + e into new_sums_.

We are sure of this since we will break only when to_push > maximum_sum and we are guarantee to have pushed all sums below "to_push" before, that includes all the initial sums in sums_.

Definition at line 906 of file util.cc.

◆ SortedSums()

absl::Span< const int64_t > operations_research::sat::SortedSubsetSums::SortedSums ( ) const
inline

Returns the possible subset sums sorted.

Definition at line 525 of file util.h.


The documentation for this class was generated from the following files: