Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
DP_Optimizations.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
53#ifndef DP_OPTIMIZATIONS_H
54#define DP_OPTIMIZATIONS_H
55
56#include <algorithm>
57#include <cstddef>
58#include <limits>
59#include <numeric>
60#include <type_traits>
61#include <utility>
62
63#include <ah-errors.H>
64#include <tpl_array.H>
65
66namespace Aleph {
67namespace dp_optimization_detail {
68// MSVC cl has no __int128_t; use long double to avoid signed-integer
69// overflow UB in intermediate computations (clamping happens at the cast site).
70#if defined(_MSC_VER) && !defined(__clang__)
71using _promoted_int_t = long double;
72#else
73using _promoted_int_t = __int128_t;
74#endif
75
76template <typename T>
78 std::conditional_t<std::is_floating_point_v<T>,
79 T,
80 std::conditional_t<(sizeof(T) < 8), long long, _promoted_int_t>>;
81
82template <typename T>
83[[nodiscard]] constexpr T default_inf() noexcept
84{
85 if constexpr (std::numeric_limits<T>::has_infinity)
86 return std::numeric_limits<T>::infinity();
87 else
88 return std::numeric_limits<T>::max() / 4;
89}
90
91template <typename Target, typename Source>
92[[nodiscard]] constexpr Target clamped_cast(Source val) noexcept
93{
94 if (val >= static_cast<Source>(std::numeric_limits<Target>::max()))
95 return std::numeric_limits<Target>::max();
96 if (val <= static_cast<Source>(std::numeric_limits<Target>::min()))
97 return std::numeric_limits<Target>::min();
98 return static_cast<Target>(val);
99}
100} // namespace dp_optimization_detail
101
104template <typename Cost>
111
136template <typename Cost, class Transition_Cost_Fn>
138 const size_t groups,
139 const size_t n,
140 Transition_Cost_Fn transition_cost,
141 const Cost inf = dp_optimization_detail::default_inf<Cost>())
142{
143 Array<Cost> prev = Array<Cost>::create(n + 1);
144 Array<Cost> curr = Array<Cost>::create(n + 1);
145
146 for (size_t i = 0; i <= n; ++i)
147 {
148 prev(i) = inf;
149 curr(i) = inf;
150 }
151 prev(0) = Cost{};
152
154 split.reserve(groups + 1);
155 for (size_t g = 0; g <= groups; ++g)
156 {
158 for (size_t i = 0; i <= n; ++i)
159 row(i) = 0;
160 split.append(std::move(row));
161 }
162
163 for (size_t g = 1; g <= groups; ++g)
164 {
165 for (size_t i = 0; i <= n; ++i)
166 curr(i) = inf;
167
168 if (g <= n)
169 {
170 auto solve = [&](auto &&self, const size_t left, const size_t right,
171 const size_t opt_left, const size_t opt_right) -> void
172 {
173 if (left > right)
174 return;
175
176 const size_t mid = std::midpoint(left, right);
177 const size_t k_end = std::min(mid - 1, opt_right);
178
179 Cost best = inf;
180 size_t best_k = opt_left;
181
182 for (size_t k = opt_left; k <= k_end; ++k)
183 {
184 if (prev[k] >= inf)
185 continue;
186
187 const Cost tc = transition_cost(k, mid);
188 Cost cand;
189 if (tc >= inf or prev[k] > inf - tc)
190 cand = inf;
191 else
192 cand = prev[k] + tc;
193
194 if (cand < best)
195 {
196 best = cand;
197 best_k = k;
198 }
199 }
200
201 curr(mid) = best;
202 split[g](mid) = best_k;
203
204 if (left < mid)
205 self(self, left, mid - 1, opt_left, best_k);
206 if (mid < right)
207 self(self, mid + 1, right, best_k, opt_right);
208 };
209
210 solve(solve, g, n, g - 1, n - 1);
211 }
212
213 prev.swap(curr);
214 }
215
216 return Divide_Conquer_DP_Result<Cost>{prev[n], std::move(prev), std::move(split)};
217}
218
221template <typename Cost>
228
251template <typename Cost, class Interval_Cost_Fn>
253 const size_t n,
254 Interval_Cost_Fn interval_cost,
255 const Cost inf = dp_optimization_detail::default_inf<Cost>())
256{
258 dp.reserve(n + 1);
259 for (size_t i = 0; i <= n; ++i)
260 {
262 for (size_t j = 0; j <= n; ++j)
263 row(j) = inf;
264 dp.append(std::move(row));
265 }
266
268 opt.reserve(n + 1);
269 for (size_t i = 0; i <= n; ++i)
270 {
272 for (size_t j = 0; j <= n; ++j)
273 row(j) = 0;
274 opt.append(std::move(row));
275 }
276
277 for (size_t i = 0; i <= n; ++i)
278 {
279 dp(i)(i) = Cost{};
280 opt(i)(i) = i;
281 if (i + 1 <= n)
282 {
283 dp(i)(i + 1) = Cost{};
284 opt(i)(i + 1) = i + 1;
285 }
286 }
287
288 for (size_t len = 2; len <= n; ++len)
289 for (size_t i = 0; i + len <= n; ++i)
290 {
291 const size_t j = i + len;
292
293 size_t left = opt[i][j - 1];
294 size_t right = opt[i + 1][j];
295 if (left < i + 1)
296 left = i + 1;
297 if (right > j - 1)
298 right = j - 1;
299
300 ah_runtime_error_if(left > right)
301 << "knuth_optimize_interval: invalid opt bounds at [" << i << ", " << j << ")";
302
303 const Cost w = interval_cost(i, j);
304
305 Cost best = inf;
306 size_t best_k = left;
307
308 for (size_t k = left; k <= right; ++k)
309 {
310 const Cost d1 = dp[i][k];
311 const Cost d2 = dp[k][j];
312 Cost cand;
313 if (d1 >= inf or d2 >= inf or w >= inf or d1 > inf - d2 or (d1 + d2) > inf - w)
314 cand = inf;
315 else
316 cand = d1 + d2 + w;
317
318 if (cand < best)
319 {
320 best = cand;
321 best_k = k;
322 }
323 }
324
325 dp(i)(j) = best;
326 opt(i)(j) = best_k;
327 }
328
329 return Knuth_Optimization_Result<Cost>{n == 0 ? Cost{} : dp[0][n], std::move(dp), std::move(opt)};
330}
331
343{
344 const size_t n = weights.size();
346 prefix(0) = 0;
347
348 for (size_t i = 0; i < n; ++i)
349 {
350 ah_runtime_error_if(prefix[i] > std::numeric_limits<size_t>::max() - weights[i])
351 << "optimal_merge_knuth: prefix sum overflow";
352 prefix(i + 1) = prefix[i] + weights[i];
353 }
354
355 return knuth_optimize_interval<size_t>(n, [&](const size_t i, const size_t j) -> size_t
356 {
357 return prefix[j] - prefix[i];
358 }, std::numeric_limits<size_t>::max() / 4);
359}
360
369template <typename T>
371{
372 static_assert(std::is_signed_v<T>, "Convex_Hull_Trick requires a signed type");
373
374public:
376 struct Line
377 {
378 T slope = T{};
380
381 [[nodiscard]] T value_at(const T x) const noexcept
382 {
384 return static_cast<T>(static_cast<PromotedT>(slope) * static_cast<PromotedT>(x) +
385 static_cast<PromotedT>(intercept));
386 }
387 };
388
389private:
391 Array<long double> starts_; // x where this line becomes optimal
392 size_t cursor_ = 0; // for monotone queries
393
394 [[nodiscard]] static long double intersection_x(const Line &a, const Line &b) noexcept
395 {
396 return static_cast<long double>(b.intercept - a.intercept) /
397 static_cast<long double>(a.slope - b.slope);
398 }
399
400public:
402 Convex_Hull_Trick() = default;
403
407 Convex_Hull_Trick(const Convex_Hull_Trick &other) = default;
408
418 : lines_(std::move(other.lines_)), starts_(std::move(other.starts_)),
419 cursor_(std::exchange(other.cursor_, 0))
420 {}
421
427
439 {
440 swap(other);
441 return *this;
442 }
443
448 void swap(Convex_Hull_Trick &other) noexcept
449 {
450 lines_.swap(other.lines_);
451 starts_.swap(other.starts_);
452 std::swap(cursor_, other.cursor_);
453 }
454
455 [[nodiscard]] size_t size() const noexcept
456 {
457 return lines_.size();
458 }
459
460 [[nodiscard]] bool is_empty() const noexcept
461 {
462 return lines_.size() == 0;
463 }
464
465 void clear()
466 {
467 Array<Line> tmp_lines;
468 lines_.swap(tmp_lines);
469
470 Array<long double> tmp_starts;
471 starts_.swap(tmp_starts);
472
473 cursor_ = 0;
474 }
475
476 void reset_query_cursor() noexcept
477 {
478 cursor_ = 0;
479 }
480
482 void add_line(const T slope, const T intercept)
483 {
484 Line ln{slope, intercept};
485
486 if (lines_.size() > 0)
487 {
488 const Line &last = lines_[lines_.size() - 1];
489 ah_domain_error_if(ln.slope > last.slope)
490 << "Convex_Hull_Trick::add_line: slopes must be non-increasing";
491
492 // Keep only the best intercept for equal slope.
493 if (ln.slope == last.slope)
494 {
495 if (ln.intercept >= last.intercept)
496 return;
497
498 (void) lines_.remove_last();
499 (void) starts_.remove_last();
500 if (cursor_ > lines_.size())
501 cursor_ = lines_.size();
502 }
503 }
504
505 while (lines_.size() > 0)
506 {
507 const long double x = intersection_x(lines_[lines_.size() - 1], ln);
508
509 if (lines_.size() == 1 or x > starts_[starts_.size() - 1])
510 {
511 starts_.append(x);
512 lines_.append(ln);
513 if (cursor_ >= lines_.size())
514 cursor_ = lines_.size() - 1;
515 return;
516 }
517
518 (void) lines_.remove_last();
519 (void) starts_.remove_last();
520 if (cursor_ > lines_.size())
521 cursor_ = lines_.size();
522 }
523
524 starts_.append(-std::numeric_limits<long double>::infinity());
525 lines_.append(ln);
526 if (cursor_ >= lines_.size())
527 cursor_ = lines_.size() - 1;
528 }
529
531 [[nodiscard]] T query(const T x) const
532 {
533 ah_runtime_error_if(lines_.size() == 0) << "Convex_Hull_Trick::query: no lines available";
534
535 size_t lo = 0;
536 size_t hi = lines_.size() - 1;
537 const auto xd = static_cast<long double>(x);
538
539 while (lo < hi)
540 if (const size_t mid = std::midpoint(lo, hi + 1); starts_[mid] <= xd)
541 lo = mid;
542 else
543 hi = mid - 1;
544
545 return lines_[lo].value_at(x);
546 }
547
549 [[nodiscard]] T query_monotone(const T x)
550 {
551 ah_runtime_error_if(lines_.size() == 0)
552 << "Convex_Hull_Trick::query_monotone: no lines available";
553
554 while (cursor_ + 1 < lines_.size() and starts_[cursor_ + 1] <= static_cast<long double>(x))
555 ++cursor_;
556
557 return lines_[cursor_].value_at(x);
558 }
559};
560
568template <typename T>
570{
571 static_assert(std::is_integral_v<T> and std::is_signed_v<T>,
572 "Li_Chao_Tree requires a signed integral coordinate/value type");
573
574public:
576 struct Line
577 {
578 T slope = T{};
580
581 [[nodiscard]] T value_at(const T x) const noexcept
582 {
584 return static_cast<T>(static_cast<PromotedT>(slope) * static_cast<PromotedT>(x) +
585 static_cast<PromotedT>(intercept));
586 }
587 };
588
589private:
590 struct Node
591 {
593 size_t left = std::numeric_limits<size_t>::max();
594 size_t right = std::numeric_limits<size_t>::max();
595 };
596
597 static constexpr size_t NIL = std::numeric_limits<size_t>::max();
598
602 size_t root_ = NIL;
603
604 [[nodiscard]] size_t new_node(const Line &line)
605 {
606 nodes_.append(Node{line, NIL, NIL});
607 return nodes_.size() - 1;
608 }
609
610 void add_line_impl(const size_t idx, const T l, const T r, Line line)
611 {
612 const T mid = std::midpoint(l, r);
613
614 if (line.value_at(mid) < nodes_[idx].line.value_at(mid))
615 std::swap(line, nodes_[idx].line);
616
617 if (l == r)
618 return;
619
620 if (line.value_at(l) < nodes_[idx].line.value_at(l))
621 {
622 if (nodes_[idx].left == NIL)
623 {
624 nodes_[idx].left = new_node(line);
625 return;
626 }
627 add_line_impl(nodes_[idx].left, l, mid, line);
628 return;
629 }
630
631 if (line.value_at(r) < nodes_[idx].line.value_at(r))
632 {
633 if (nodes_[idx].right == NIL)
634 {
635 nodes_[idx].right = new_node(line);
636 return;
637 }
638 add_line_impl(nodes_[idx].right, mid + 1, r, line);
639 }
640 }
641
642 [[nodiscard]] T query_impl(const size_t idx, const T l, const T r, const T x) const
643 {
645 auto ret = static_cast<PromotedT>(nodes_[idx].line.value_at(x));
646 if (l == r)
647 return static_cast<T>(ret);
648
649 const T mid = std::midpoint(l, r);
650 if (x <= mid and nodes_[idx].left != NIL)
651 ret = std::min(ret, static_cast<PromotedT>(query_impl(nodes_[idx].left, l, mid, x)));
652 else if (x > mid and nodes_[idx].right != NIL)
653 ret = std::min(ret, static_cast<PromotedT>(query_impl(nodes_[idx].right, mid + 1, r, x)));
654
655 return dp_optimization_detail::clamped_cast<T>(ret);
656 }
657
658public:
659 Li_Chao_Tree(const T x_left, const T x_right) : x_left_(x_left), x_right_(x_right)
660 {
662 << "Li_Chao_Tree: invalid domain [" << x_left_ << ", " << x_right_ << "]";
663 }
664
668 Li_Chao_Tree(const Li_Chao_Tree &other) = default;
669
679 Li_Chao_Tree(Li_Chao_Tree &&other) noexcept
680 : x_left_(other.x_left_), x_right_(other.x_right_), nodes_(std::move(other.nodes_)),
681 root_(std::exchange(other.root_, NIL))
682 {}
683
688 Li_Chao_Tree &operator=(const Li_Chao_Tree &other) = default;
689
696 {
697 swap(other);
698 return *this;
699 }
700
705 void swap(Li_Chao_Tree &other) noexcept
706 {
707 std::swap(x_left_, other.x_left_);
708 std::swap(x_right_, other.x_right_);
709 nodes_.swap(other.nodes_);
710 std::swap(root_, other.root_);
711 }
712
713 [[nodiscard]] bool is_empty() const noexcept
714 {
715 return root_ == NIL;
716 }
717 [[nodiscard]] size_t node_count() const noexcept
718 {
719 return nodes_.size();
720 }
721
722 void clear()
723 {
724 Array<Node> tmp;
725 nodes_.swap(tmp);
726 root_ = NIL;
727 }
728
729 void add_line(const T slope, const T intercept)
730 {
731 const Line line{slope, intercept};
732 if (root_ == NIL)
733 {
734 root_ = new_node(line);
735 return;
736 }
738 }
739
740 [[nodiscard]] T query(const T x) const
741 {
742 ah_runtime_error_if(root_ == NIL) << "Li_Chao_Tree::query: no lines available";
743 ah_out_of_range_error_if(x < x_left_ or x > x_right_)
744 << "Li_Chao_Tree::query: x=" << x << " outside domain [" << x_left_ << ", " << x_right_ << "]";
745
746 return query_impl(root_, x_left_, x_right_, x);
747 }
748};
749
752template <typename Cost>
758
778template <typename Cost>
780 const Array<Cost> &base_cost,
781 const size_t window,
782 const Cost inf = dp_optimization_detail::default_inf<Cost>())
783{
784 const size_t n = base_cost.size();
785 if (n == 0)
787
788 ah_domain_error_if(window == 0 and n > 1)
789 << "monotone_queue_min_dp: window must be > 0 when n > 1";
790
793
794 Array<size_t> deque_idx;
795 size_t head = 0;
796
797 dp(0) = base_cost[0];
798 parent(0) = 0;
799 deque_idx.append(0);
800
801 for (size_t i = 1; i < n; ++i)
802 {
803 const size_t min_valid = i > window ? i - window : 0;
804
805 while (head < deque_idx.size() and deque_idx[head] < min_valid)
806 ++head;
807
808 ah_runtime_error_if(head == deque_idx.size())
809 << "monotone_queue_min_dp: no valid predecessor for i=" << i;
810
811 const size_t best = deque_idx[head];
812 const Cost bc = base_cost[i];
813 if (dp[best] >= inf or bc >= inf or dp[best] > inf - bc)
814 dp(i) = inf;
815 else
816 dp(i) = dp[best] + bc;
817
818 parent(i) = best;
819
820 while (deque_idx.size() > head and dp[deque_idx[deque_idx.size() - 1]] >= dp[i])
821 (void) deque_idx.remove_last();
822
823 deque_idx.append(i);
824 }
825
826 return Monotone_Queue_DP_Result<Cost>{std::move(dp), std::move(parent)};
827}
828
849template <typename T>
851 const Array<T> &weights)
852{
853 static_assert(std::is_integral_v<T> and std::is_signed_v<T>,
854 "min_weighted_squared_distance_1d requires signed integral T");
855
856 ah_domain_error_if(xs.size() != weights.size())
857 << "min_weighted_squared_distance_1d: xs and weights size mismatch";
858
859 const size_t n = xs.size();
860 if (n == 0)
861 return Array<T>();
862
863 T min_x = xs[0];
864 T max_x = xs[0];
865 for (size_t i = 1; i < n; ++i)
866 {
867 if (xs[i] < min_x)
868 min_x = xs[i];
869 if (xs[i] > max_x)
870 max_x = xs[i];
871 }
872
873 Li_Chao_Tree<T> lc(min_x, max_x);
875 for (size_t j = 0; j < n; ++j)
876 {
877 const auto px = static_cast<PromotedT>(xs[j]);
878 const T m = static_cast<T>(static_cast<PromotedT>(-2) * px);
879 const T b = static_cast<T>(px * px + static_cast<PromotedT>(weights[j]));
880 lc.add_line(m, b);
881 }
882
883 Array<T> ret = Array<T>::create(n);
884 for (size_t i = 0; i < n; ++i)
885 {
886 const auto px = static_cast<PromotedT>(xs[i]);
887 ret(i) = static_cast<T>(px * px + static_cast<PromotedT>(lc.query(xs[i])));
888 }
889
890 return ret;
891}
892} // namespace Aleph
893
894#endif // DP_OPTIMIZATIONS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
long double w
Definition btreepic.C:153
size_t row
Definition ca-c-api.h:115
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
Convex Hull Trick for minimum queries.
Convex_Hull_Trick(Convex_Hull_Trick &&other) noexcept
Take the lines of another hull, which is left empty, as clear() leaves it.
static long double intersection_x(const Line &a, const Line &b) noexcept
bool is_empty() const noexcept
T query_monotone(const T x)
Query minimum with non-decreasing x (amortized O(1)).
Convex_Hull_Trick & operator=(const Convex_Hull_Trick &other)=default
Copy the lines and the query cursor of another hull.
void reset_query_cursor() noexcept
T query(const T x) const
Query minimum value at arbitrary x (O(log n)).
Convex_Hull_Trick & operator=(Convex_Hull_Trick &&other) noexcept
Exchange this hull with another, which is left with the previous lines and cursor of this one.
size_t size() const noexcept
void swap(Convex_Hull_Trick &other) noexcept
Exchange in O(1) the lines and the cursors of this hull and another.
void add_line(const T slope, const T intercept)
Insert a new line; slopes must be non-increasing.
Array< long double > starts_
Convex_Hull_Trick()=default
Construct an empty hull.
Convex_Hull_Trick(const Convex_Hull_Trick &other)=default
Copy the lines and the query cursor of another hull.
Li Chao tree for min line queries on an integral x-domain.
void add_line(const T slope, const T intercept)
static constexpr size_t NIL
T query(const T x) const
void swap(Li_Chao_Tree &other) noexcept
Exchange in O(1) the domains and the lines of this tree and another.
T query_impl(const size_t idx, const T l, const T r, const T x) const
bool is_empty() const noexcept
Li_Chao_Tree & operator=(Li_Chao_Tree &&other) noexcept
Exchange this tree with another, which is left with the previous domain and lines of this one.
Li_Chao_Tree & operator=(const Li_Chao_Tree &other)=default
Copy the domain and the lines of another tree.
Li_Chao_Tree(Li_Chao_Tree &&other) noexcept
Take the lines of another tree, which is left empty over the same domain.
Li_Chao_Tree(const Li_Chao_Tree &other)=default
Copy the domain and the lines of another tree.
void add_line_impl(const size_t idx, const T l, const T r, Line line)
size_t node_count() const noexcept
size_t new_node(const Line &line)
Li_Chao_Tree(const T x_left, const T x_right)
std::conditional_t< std::is_floating_point_v< T >, T, std::conditional_t<(sizeof(T)< 8), long long, _promoted_int_t > > promoted_t
constexpr T default_inf() noexcept
constexpr Target clamped_cast(Source val) noexcept
Main namespace for Aleph-w library functions.
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
Array< T > min_weighted_squared_distance_1d(const Array< T > &xs, const Array< T > &weights)
Weighted squared-distance lower envelope on a line.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Knuth_Optimization_Result< size_t > optimal_merge_knuth(const Array< size_t > &weights)
Optimal adjacent merge cost via Knuth optimization.
static void prefix(Node *root, DynList< Node * > &acc)
Monotone_Queue_DP_Result< Cost > monotone_queue_min_dp(const Array< Cost > &base_cost, const size_t window, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize windowed min-transition DP with a monotone queue.
and
Check uniqueness with explicit hash + equality functors.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Knuth_Optimization_Result< Cost > knuth_optimize_interval(const size_t n, Interval_Cost_Fn interval_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize interval DP with Knuth optimization.
Affine line y = m*x + b.
T value_at(const T x) const noexcept
Result of divide-and-conquer partition DP optimization.
Array< Cost > last_row
Last DP layer (size n+1).
Array< Array< size_t > > split
Best split index per layer/state.
Cost optimal_cost
Final optimum at state (groups, n).
Result of Knuth interval-DP optimization.
Array< Array< Cost > > dp
Interval DP table.
Cost optimal_cost
Final optimum at interval [0, n).
Array< Array< size_t > > opt
Argmin table used by Knuth bounds.
Affine line y = m*x + b.
T value_at(const T x) const noexcept
Result of monotone-queue windowed DP transition.
Array< size_t > parent
Chosen predecessor index for each i.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
DynList< int > l