Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lis_example.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 <iostream>
38# include <iomanip>
39
40# include <LIS.H>
41# include <print_rule.H>
42
43using namespace Aleph;
44
45namespace
46{
54 void print_array(const char * label, const Array<int> & arr)
55 {
56 std::cout << std::setw(14) << std::left << label << ": [";
57 for (size_t i = 0; i < arr.size(); ++i)
58 {
59 if (i > 0)
60 std::cout << ", ";
61 std::cout << arr[i];
62 }
63 std::cout << "]\n";
64 }
65
75 template <typename Fn>
76 void run_scenario(const char * label,
77 const Array<int> & seq,
79 const char * res_label,
80 const char * len_label)
81 {
82 std::cout << label << "\n";
83 print_rule();
84 print_array("Input", seq);
85 const auto r = compute_fn(seq);
86 print_array(res_label, r.subsequence);
87 std::cout << std::setw(14) << std::left << len_label << ": " << r.length << "\n";
88 }
89}
90
101int main()
102{
103 std::cout << "\n=== Longest Increasing Subsequence Toolkit ===\n\n";
104
105 // Example 1: classic LIS
106 {
107 Array<int> seq = {10, 9, 2, 5, 3, 7, 101, 18};
108 run_scenario("Scenario A: Classic LIS", seq,
109 [](const auto & s) { return longest_increasing_subsequence(s); },
110 "One LIS", "LIS length");
111 std::cout << std::setw(14) << std::left << "Length-only" << ": "
112 << lis_length(seq) << "\n";
113 print_rule();
114 std::cout << "\n";
115 }
116
117 // Example 2: already sorted
118 {
119 Array<int> seq = {1, 3, 5, 7, 9};
120 run_scenario("Scenario B: Already sorted", seq,
121 [](const auto & s) { return longest_increasing_subsequence(s); },
122 "One LIS", "LIS length");
123 print_rule();
124 std::cout << "\n";
125 }
126
127 // Example 3: reverse sorted
128 {
129 Array<int> seq = {9, 7, 5, 3, 1};
130 run_scenario("Scenario C: Reverse sorted", seq,
131 [](const auto & s) { return longest_increasing_subsequence(s); },
132 "One LIS", "LIS length");
133 print_rule();
134 std::cout << "\n";
135 }
136
137 // Example 4: non-decreasing subsequence
138 {
139 Array<int> seq = {3, 1, 2, 2, 3, 1};
140 run_scenario("Scenario D: Longest non-decreasing subsequence", seq,
141 [](const auto & s) { return longest_nondecreasing_subsequence(s); },
142 "One LNDS", "LNDS length");
143 print_rule();
144 std::cout << "\n";
145 }
146
147 // Example 5: decreasing sequence via custom comparator
148 {
149 std::cout << "Scenario E: Decreasing subsequence via comparator\n";
150 print_rule();
151 Array<int> seq = {5, 1, 4, 2, 8, 3};
152 print_array("Input", seq);
156 print_array("Strict dec", lds.subsequence);
157 print_array("Non-inc", lnds_desc.subsequence);
158 std::cout << std::setw(14) << std::left << "LDS length" << ": "
159 << lds.length << "\n";
160 std::cout << std::setw(14) << std::left << "Non-inc len" << ": "
161 << lnds_desc.length << "\n";
162 print_rule();
163 }
164
165 std::cout << "\nDone.\n";
166 return 0;
167}
Longest Increasing Subsequence (LIS) via patience sorting.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
int main()
Demonstrates usage of Aleph-w LIS/LNDS utilities by running several example scenarios and printing th...
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
size_t lis_length(const Array< T > &seq, Compare cmp=Compare())
Compute only the length of the LIS (no reconstruction).
Definition LIS.H:178
void print_array(const char *label, const Container &a)
gsl_rng * r