Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lis_test.cc
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
31
37# include <gtest/gtest.h>
38# include <algorithm>
39# include <cstdint>
40# include <random>
41
42# include <LIS.H>
43
44using namespace Aleph;
45
46namespace
47{
48 bool is_subsequence(const Array<int> & seq, const Array<int> & sub)
49 {
50 size_t pos = 0;
51 for (size_t i = 0; i < sub.size(); ++i)
52 {
53 while (pos < seq.size() and seq[pos] != sub[i])
54 ++pos;
55 if (pos == seq.size())
56 return false;
57 ++pos;
58 }
59 return true;
60 }
61
62 size_t lis_quadratic_length(const Array<int> & seq)
63 {
64 if (seq.is_empty())
65 return 0;
66
68 size_t best = 1;
69 for (size_t i = 0; i < seq.size(); ++i)
70 {
71 dp(i) = 1;
72 for (size_t j = 0; j < i; ++j)
73 if (seq[j] < seq[i] and dp[j] + 1 > dp[i])
74 dp(i) = dp[j] + 1;
75 best = std::max(best, dp[i]);
76 }
77 return best;
78 }
79
80 size_t lnds_quadratic_length(const Array<int> & seq)
81 {
82 if (seq.is_empty())
83 return 0;
84
86 size_t best = 1;
87 for (size_t i = 0; i < seq.size(); ++i)
88 {
89 dp(i) = 1;
90 for (size_t j = 0; j < i; ++j)
91 if (seq[j] <= seq[i] and dp[j] + 1 > dp[i])
92 dp(i) = dp[j] + 1;
93 best = std::max(best, dp[i]);
94 }
95 return best;
96 }
97} // namespace
98
99
101{
102 Array<int> empty;
103 auto r = longest_increasing_subsequence(empty);
104 EXPECT_EQ(r.length, 0u);
105 EXPECT_EQ(r.subsequence.size(), 0u);
106 EXPECT_EQ(lis_length(empty), 0u);
107}
108
110{
111 Array<int> single = {42};
113 EXPECT_EQ(r.length, 1u);
114 EXPECT_EQ(r.subsequence.size(), 1u);
115 EXPECT_EQ(r.subsequence[0], 42);
116}
117
119{
120 // LeetCode 300 classic: {10,9,2,5,3,7,101,18} -> LIS length 4
121 Array<int> seq = {10, 9, 2, 5, 3, 7, 101, 18};
123 EXPECT_EQ(r.length, 4u);
124 EXPECT_EQ(r.subsequence.size(), 4u);
125
126 // Verify the subsequence is strictly increasing
127 for (size_t i = 1; i < r.subsequence.size(); ++i)
128 EXPECT_LT(r.subsequence[i - 1], r.subsequence[i]);
129
130 // Verify subsequence preserves original order
131 EXPECT_TRUE(is_subsequence(seq, r.subsequence));
132}
133
135{
136 Array<int> all_same = {5, 5, 5, 5, 5};
138 EXPECT_EQ(r.length, 1u);
139}
140
142{
143 Array<int> sorted = {1, 2, 3, 4, 5};
145 EXPECT_EQ(r.length, 5u);
146}
147
149{
150 Array<int> rev = {5, 4, 3, 2, 1};
152 EXPECT_EQ(r.length, 1u);
153}
154
156{
157 Array<int> seq = {10, 9, 2, 5, 3, 7, 101, 18};
158 EXPECT_EQ(lis_length(seq), 4u);
159}
160
162{
163 Array<int> seq = {3, 1, 2, 2, 3};
165 // LNDS: {1, 2, 2, 3} = length 4
166 EXPECT_EQ(r.length, 4u);
167
168 // Verify non-decreasing
169 for (size_t i = 1; i < r.subsequence.size(); ++i)
170 EXPECT_LE(r.subsequence[i - 1], r.subsequence[i]);
171
172 EXPECT_TRUE(is_subsequence(seq, r.subsequence));
173}
174
176{
177 Array<int> seq = {7, 7, 7, 7, 7};
179 EXPECT_EQ(r.length, seq.size());
180 EXPECT_EQ(r.subsequence.size(), seq.size());
181 EXPECT_TRUE(is_subsequence(seq, r.subsequence));
182}
183
185{
186 // Longest decreasing subsequence via reversed comparator
187 Array<int> seq = {5, 1, 4, 2, 3};
189 EXPECT_EQ(r.length, 3u); // e.g. {5, 4, 2} or {5, 4, 3}
190
191 // Verify strictly decreasing
192 for (size_t i = 1; i < r.subsequence.size(); ++i)
193 EXPECT_GT(r.subsequence[i - 1], r.subsequence[i]);
194
195 EXPECT_TRUE(is_subsequence(seq, r.subsequence));
196}
197
199{
200 Array<int> seq = {5, 5, 4, 4, 3, 2, 2, 1};
202 EXPECT_EQ(r.length, seq.size());
203 for (size_t i = 1; i < r.subsequence.size(); ++i)
204 EXPECT_GE(r.subsequence[i - 1], r.subsequence[i]);
205 EXPECT_TRUE(is_subsequence(seq, r.subsequence));
206}
207
209{
210 // Random arrays: compare O(n log n) against O(n^2) reference DP.
211 std::mt19937 rng(12345);
212 for (int trial = 0; trial < 120; ++trial)
213 {
214 const size_t n = 20 + rng() % 60; // 20-79 elements
215 Array<int> arr;
216 for (size_t i = 0; i < n; ++i)
217 arr.append(static_cast<int>(rng() % 200) - 100);
218
219 const auto lis = longest_increasing_subsequence(arr);
220 const size_t fast_len = lis.length;
221 const size_t fast_len_only = lis_length(arr);
222 const size_t ref_len = lis_quadratic_length(arr);
223
225 << "Mismatch at trial " << trial;
227 << "Mismatch at trial " << trial;
228
229 EXPECT_TRUE(is_subsequence(arr, lis.subsequence));
230 for (size_t i = 1; i < lis.subsequence.size(); ++i)
231 EXPECT_LT(lis.subsequence[i - 1], lis.subsequence[i]);
232 }
233}
234
236{
237 std::mt19937 rng(98765);
238 for (int trial = 0; trial < 120; ++trial)
239 {
240 const size_t n = 20 + rng() % 60;
241 Array<int> arr;
242 for (size_t i = 0; i < n; ++i)
243 arr.append(static_cast<int>(rng() % 40)); // many duplicates
244
245 const auto lnds = longest_nondecreasing_subsequence(arr);
246 const size_t ref_len = lnds_quadratic_length(arr);
247
248 EXPECT_EQ(lnds.length, ref_len)
249 << "Mismatch at trial " << trial;
250 EXPECT_TRUE(is_subsequence(arr, lnds.subsequence));
251 for (size_t i = 1; i < lnds.subsequence.size(); ++i)
252 EXPECT_LE(lnds.subsequence[i - 1], lnds.subsequence[i]);
253 }
254}
Longest Increasing Subsequence (LIS) via patience sorting.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:194
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:348
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
#define TEST(name)
static mt19937 rng
static bool is_subsequence(const Array< Point > &original, const Array< Point > &sub)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
LIS_Result< T > longest_nondecreasing_subsequence(const Array< T > &seq, Compare cmp=Compare())
Compute the Longest Non-Decreasing Subsequence.
Definition LIS.H:216
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.
LIS_Result< T > longest_increasing_subsequence(const Array< T > &seq, Compare cmp=Compare())
Compute the Longest Increasing Subsequence (patience sorting).
Definition LIS.H:115
size_t lis_length(const Array< T > &seq, Compare cmp=Compare())
Compute only the length of the LIS (no reconstruction).
Definition LIS.H:178
double sub(double a, double b)
gsl_rng * r