Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lis_example.cc
Author
Leandro Rabindranath Leon
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
# include <iostream>
# include <iomanip>
# include <LIS.H>
# include <print_rule.H>
using namespace Aleph;
namespace
{
void print_array(const char * label, const Array<int> & arr)
{
std::cout << std::setw(14) << std::left << label << ": [";
for (size_t i = 0; i < arr.size(); ++i)
{
if (i > 0)
std::cout << ", ";
std::cout << arr[i];
}
std::cout << "]\n";
}
template <typename Fn>
void run_scenario(const char * label,
const Array<int> & seq,
Fn compute_fn,
const char * res_label,
const char * len_label)
{
std::cout << label << "\n";
print_rule();
print_array("Input", seq);
const auto r = compute_fn(seq);
print_array(res_label, r.subsequence);
std::cout << std::setw(14) << std::left << len_label << ": " << r.length << "\n";
}
}
int main()
{
std::cout << "\n=== Longest Increasing Subsequence Toolkit ===\n\n";
// Example 1: classic LIS
{
Array<int> seq = {10, 9, 2, 5, 3, 7, 101, 18};
run_scenario("Scenario A: Classic LIS", seq,
[](const auto & s) { return longest_increasing_subsequence(s); },
"One LIS", "LIS length");
std::cout << std::setw(14) << std::left << "Length-only" << ": "
<< lis_length(seq) << "\n";
std::cout << "\n";
}
// Example 2: already sorted
{
Array<int> seq = {1, 3, 5, 7, 9};
run_scenario("Scenario B: Already sorted", seq,
[](const auto & s) { return longest_increasing_subsequence(s); },
"One LIS", "LIS length");
std::cout << "\n";
}
// Example 3: reverse sorted
{
Array<int> seq = {9, 7, 5, 3, 1};
run_scenario("Scenario C: Reverse sorted", seq,
[](const auto & s) { return longest_increasing_subsequence(s); },
"One LIS", "LIS length");
std::cout << "\n";
}
// Example 4: non-decreasing subsequence
{
Array<int> seq = {3, 1, 2, 2, 3, 1};
run_scenario("Scenario D: Longest non-decreasing subsequence", seq,
[](const auto & s) { return longest_nondecreasing_subsequence(s); },
"One LNDS", "LNDS length");
std::cout << "\n";
}
// Example 5: decreasing sequence via custom comparator
{
std::cout << "Scenario E: Decreasing subsequence via comparator\n";
Array<int> seq = {5, 1, 4, 2, 8, 3};
print_array("Input", seq);
const auto lnds_desc = longest_nondecreasing_subsequence(seq,
print_array("Strict dec", lds.subsequence);
print_array("Non-inc", lnds_desc.subsequence);
std::cout << std::setw(14) << std::left << "LDS length" << ": "
<< lds.length << "\n";
std::cout << std::setw(14) << std::left << "Non-inc len" << ": "
<< lnds_desc.length << "\n";
}
std::cout << "\nDone.\n";
return 0;
}
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
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
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