libsemigroups  v3.3.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
paths.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2023-2025 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19// This file contains declarations related to iterating through paths in an
20// WordGraph.
21
22// TODO(2):
23// * check code coverage
24// * the function number_of_paths_algorithm and number_of_paths don't match
25// each other in terms of when they throw exceptions.
26
27#ifndef LIBSEMIGROUPS_PATHS_HPP_
28#define LIBSEMIGROUPS_PATHS_HPP_
29
30#include <algorithm> // for any_of, for_each, max_element
31#include <cstddef> // for size_t, ptrdiff_t
32#include <cstdint> // for uint64_t
33#include <iterator> // for distance, forward_iterator_tag
34#include <numeric> // for accumulate
35#include <string> // for std::string
36#include <type_traits> // for true_type
37#include <variant> // for visit, variant
38#include <vector> // for vector, allocator
39
40#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_ENA...
41#include "constants.hpp" // for Max, UNDEFINED, Positive...
42#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
43#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
44#include "order.hpp" // for order
45#include "paths-count.hpp" // for algorithm
46#include "ranges.hpp" // for is_input_range
47#include "types.hpp" // for word_type
48#include "word-graph-helpers.hpp" // for word_graph
49#include "word-graph.hpp" // for WordGraph
50#include "word-range.hpp" // for number_of_words
51
52#include "detail/containers.hpp" // for DynamicArray2
53#include "detail/path-iterators.hpp" // for default_postfix_increment
54
55namespace libsemigroups {
61 namespace paths {
63 using algorithm [[deprecated]] = v4::paths::algorithm;
64 } // namespace paths
65
110 // not noexcept because constructors of const_pilo_iterator aren't
111 template <typename Node1, typename Node2>
112 [[nodiscard]] auto cbegin_pilo(WordGraph<Node1> const& wg,
113 Node2 source,
114 size_t min = 0,
115 size_t max = POSITIVE_INFINITY) {
116 word_graph::throw_if_node_out_of_bounds(wg, static_cast<Node1>(source));
117 return detail::const_pilo_iterator<Node1>(&wg, source, min, max);
118 }
119
136 // not noexcept because constructors of const_pilo_iterator aren't
137 template <typename Node>
138 [[nodiscard]] auto cend_pilo(WordGraph<Node> const& wg) {
139 (void) wg;
140 return detail::const_pilo_iterator<Node>();
141 }
142
187 // TODO(2) example and what is the complexity?
188 // not noexcept because detail::const_pislo_iterator constructors aren't
189 template <typename Node1, typename Node2>
190 [[nodiscard]] auto cbegin_pislo(WordGraph<Node1> const& wg,
191 Node2 source,
192 size_t min = 0,
193 size_t max = POSITIVE_INFINITY) {
194 word_graph::throw_if_node_out_of_bounds(wg, static_cast<Node1>(source));
195 return detail::const_pislo_iterator<Node1>(&wg, source, min, max);
196 }
197
215 // not noexcept because detail::const_pislo_iterator constructors aren't
216 template <typename Node>
217 [[nodiscard]] auto cend_pislo(WordGraph<Node> const& wg) {
218 (void) wg;
219 return detail::const_pislo_iterator<Node>();
220 }
221
262 // not noexcept because detail::const_pstilo_iterator constructors aren't
263 template <typename Node1, typename Node2>
264 [[nodiscard]] auto cbegin_pstilo(WordGraph<Node1> const& wg,
265 Node2 source,
266 Node2 target,
267 size_t min = 0,
268 size_t max = POSITIVE_INFINITY) {
269 // source & target are validated in is_reachable.
270 if (!v4::word_graph::is_reachable(wg, source, target)) {
271 return cend_pstilo(wg);
272 }
273 return detail::const_pstilo_iterator<Node1>(&wg, source, target, min, max);
274 }
275
292 // not noexcept because detail::const_pstilo_iterator constructors aren't
293 template <typename Node>
294 [[nodiscard]] auto cend_pstilo(WordGraph<Node> const& wg) {
295 (void) wg;
296 return detail::const_pstilo_iterator<Node>();
297 }
298
341 // not noexcept because cbegin_pislo isn't
342 template <typename Node1, typename Node2>
343 [[nodiscard]] auto cbegin_pstislo(WordGraph<Node1> const& wg,
344 Node2 source,
345 Node2 target,
346 size_t min = 0,
347 size_t max = POSITIVE_INFINITY) {
348 // source & target are validated in is_reachable.
349 if (!v4::word_graph::is_reachable(wg, source, target)) {
350 return cend_pstislo(wg);
351 }
352 return detail::const_pstislo_iterator<Node1>(&wg, source, target, min, max);
353 }
354
373 // not noexcept because cend_pislo isn't
374 template <typename Node>
375 [[nodiscard]] auto cend_pstislo(WordGraph<Node> const& wg) {
376 (void) wg;
377 return detail::const_pstislo_iterator<Node>();
378 }
379
396 template <typename Node1, typename Node2>
397 [[deprecated]] [[nodiscard]] paths::algorithm
399 return v4::paths::count_algorithm(wg, source);
400 }
401
427 template <typename Node1, typename Node2>
428 [[deprecated]] [[nodiscard]] uint64_t
430 return v4::paths::count(wg, source);
431 }
432
456 // Not noexcept because v4::word_graph::topological_sort is not.
457 template <typename Node1, typename Node2>
458 [[deprecated]] [[nodiscard]] paths::algorithm
460 Node2 source,
461 size_t min,
462 size_t max) {
463 return v4::paths::count_algorithm(wg, source, min, max);
464 }
465
512 // not noexcept for example detail::number_of_paths_trivial can throw
513 template <typename Node1, typename Node2>
514 [[deprecated]] [[nodiscard]] uint64_t number_of_paths(
515 WordGraph<Node1> const& wg,
516 Node2 source,
517 size_t min,
518 size_t max,
519 v4::paths::algorithm lgrthm = v4::paths::algorithm::automatic) {
520 return v4::paths::count(wg, source, min, max, lgrthm);
521 }
522
548 // Not noexcept because v4::word_graph::topological_sort isn't
549 template <typename Node1, typename Node2>
550 [[deprecated]] [[nodiscard]] paths::algorithm
552 Node2 source,
553 Node2 target,
554 size_t min,
555 size_t max) {
556 return v4::paths::count_algorithm(wg, source, target, min, max);
557 }
558
606 // not noexcept because cbegin_pstilo isn't
607 template <typename Node1, typename Node2>
608 [[deprecated]] [[nodiscard]] uint64_t number_of_paths(
609 WordGraph<Node1> const& wg,
610 Node2 source,
611 Node2 target,
612 size_t min,
613 size_t max,
614 v4::paths::algorithm lgrthm = v4::paths::algorithm::automatic) {
615 return v4::paths::count(wg, source, target, min, max, lgrthm);
616 }
617
656 template <typename Node>
657 class Paths {
658 public:
664 using node_type = Node;
665
670
674 using output_type = word_type const&;
675
676 private:
677 using const_iterator = std::variant<detail::const_pstislo_iterator<Node>,
678 detail::const_pstilo_iterator<Node>,
679 detail::const_pislo_iterator<Node>,
680 detail::const_pilo_iterator<Node>>;
681
682 WordGraph<node_type> const* _word_graph;
683 Order _order;
684 size_type _max;
685 size_type _min;
686 mutable size_type _position;
687 node_type _source;
688 node_type _target;
689 mutable const_iterator _current;
690 mutable const_iterator _end;
691 mutable bool _current_valid;
692
693 bool set_iterator_no_checks() const;
694
695 // The following init function is private to avoid the case of constructing
696 // a Paths object without setting _word_graph. The subsequent default
697 // constructor is protected and still needed (instead of deleting it) to
698 // avoid problems with having uninitialised std::variants with copy
699 // constructors.
700
701 Paths& init();
702
703 protected:
704 Paths() = default;
705
706 public:
708 // Constructors + initialization
710
714 Paths(Paths const&);
715
719 Paths(Paths&&);
720
724 Paths& operator=(Paths const&);
725
729 Paths& operator=(Paths&&);
730
731 ~Paths();
732
745 explicit Paths(WordGraph<Node> const& wg) : Paths() {
746 init(wg);
747 }
748
765 Paths& init(WordGraph<Node> const& wg) {
766 init();
767 _word_graph = &wg;
768 return *this;
769 }
770
772 // Validation
774
781
783 // Functions + members required by rx::ranges
785
794 output_type get() const {
795 set_iterator_no_checks();
796 return std::visit(
797 [](auto& it) -> auto const& { return *it; }, _current);
798 }
799
807 void next() {
808 if (!at_end()) {
809 ++_position;
810 std::visit([](auto& it) { ++it; }, _current);
811 }
812 }
813
823 [[nodiscard]] bool at_end() const {
824 if (!set_iterator_no_checks()) {
825 return true;
826 }
827 return _current == _end;
828 }
829
842 [[nodiscard]] uint64_t size_hint() const;
843
856 [[nodiscard]] uint64_t count() const {
857 return size_hint();
858 }
859
860 static constexpr bool is_finite = true; // this isn't always true!
861 static constexpr bool is_idempotent = true;
862
864 // Settings
866
887 Paths& source_no_checks(node_type n) noexcept {
888 return source_no_checks(this, n);
889 }
890
912
922 [[nodiscard]] node_type source() const noexcept {
923 return _source;
924 }
925
946 Paths& target_no_checks(node_type n) noexcept {
947 return target_no_checks(this, n);
948 }
949
967 Paths& target(node_type n) {
968 if (n != UNDEFINED) {
970 }
971 return target_no_checks(n);
972 }
973
983 [[nodiscard]] node_type target() const noexcept {
984 return _target;
985 }
986
999 [[nodiscard]] node_type current_target() const;
1000
1013 Paths& min(size_type val) noexcept {
1014 return min(this, val);
1015 }
1016
1026 [[nodiscard]] size_type min() const noexcept {
1027 return _min;
1028 }
1029
1042 Paths& max(size_type val) noexcept {
1043 return max(this, val);
1044 }
1045
1055 [[nodiscard]] size_type max() const noexcept {
1056 return _max;
1057 }
1058
1070 Paths& order(Order val) {
1071 return order(this, val);
1072 }
1073
1083 [[nodiscard]] Order order() const noexcept {
1084 return _order;
1085 }
1086
1096 [[nodiscard]] WordGraph<Node> const& word_graph() const noexcept {
1097 return *_word_graph;
1098 }
1099
1100 protected:
1101 template <typename Subclass>
1102 Subclass& source_no_checks(Subclass* obj, node_type src) {
1103 _current_valid &= (src == _source);
1104 _source = src;
1105 return *obj;
1106 }
1107
1108 template <typename Subclass>
1109 Subclass& target_no_checks(Subclass* obj, node_type trgt) noexcept {
1110 _current_valid &= (trgt == _target);
1111 _target = trgt;
1112 return *obj;
1113 }
1114
1115 template <typename Subclass>
1116 Subclass& min(Subclass* obj, size_type min) noexcept {
1117 _current_valid &= (min == _min);
1118 _min = min;
1119 return *obj;
1120 }
1121
1122 template <typename Subclass>
1123 Subclass& max(Subclass* obj, size_type max) noexcept {
1124 _current_valid &= (max == _max);
1125 _max = max;
1126 return *obj;
1127 }
1128
1129 template <typename Subclass>
1130 Subclass& order(Subclass* obj, Order val);
1131 }; // class Paths
1132
1137 template <typename Node>
1138 Paths(WordGraph<Node> const&) -> Paths<Node>;
1139
1144 template <typename Node>
1145 Paths(WordGraph<Node>&&) -> Paths<Node>;
1146
1159 template <typename Node>
1161
1162} // namespace libsemigroups
1163
1164#include "paths.tpp"
1165
1166#endif // LIBSEMIGROUPS_PATHS_HPP_
auto cbegin_pstilo(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min=0, size_t max=POSITIVE_INFINITY)
Definition paths.hpp:264
Paths(WordGraph< Node > &&) -> Paths< Node >
void next()
Advance to the next path in the range.
Definition paths.hpp:807
typename WordGraph< uint32_t >::size_type size_type
Definition paths.hpp:669
uint32_t node_type
Definition paths.hpp:664
size_type min() const noexcept
Get the minimum length of path in the range.
Definition paths.hpp:1026
word_type const & output_type
Definition paths.hpp:674
Paths(WordGraph< Node > const &) -> Paths< Node >
auto cend_pstilo(WordGraph< Node > const &wg)
Definition paths.hpp:294
Paths(Paths const &)
Default copy constructor.
WordGraph< Node > const & word_graph() const noexcept
The underlying WordGraph.
Definition paths.hpp:1096
Paths(Paths &&)
Default move constructor.
auto cend_pilo(WordGraph< Node > const &wg)
Definition paths.hpp:138
Paths & min(size_type val) noexcept
Definition paths.hpp:1013
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source) noexcept
Definition paths.hpp:398
std::string to_human_readable_repr(Paths< Node > const &p)
Return a human readable representation of a Paths object.
Paths(WordGraph< Node > const &wg)
Construct from a WordGraph.
Definition paths.hpp:745
auto cbegin_pislo(WordGraph< Node1 > const &wg, Node2 source, size_t min=0, size_t max=POSITIVE_INFINITY)
Definition paths.hpp:190
auto cend_pstislo(WordGraph< Node > const &wg)
Definition paths.hpp:375
Paths & max(size_type val) noexcept
Definition paths.hpp:1042
Paths & operator=(Paths const &)
Default copy assignment operator.
node_type source() const noexcept
Get the current source node of every path in the range.
Definition paths.hpp:922
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source)
Definition paths.hpp:429
node_type current_target() const
Get the current target node of the path labelled by get.
size_type max() const noexcept
Get the maximum length of path in the range.
Definition paths.hpp:1055
bool at_end() const
Check if the range is exhausted.
Definition paths.hpp:823
Paths & source(node_type n)
Definition paths.hpp:908
void throw_if_source_undefined() const
Throw an exception if the source node has not been defined (using source).
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source, size_t min, size_t max, v4::paths::algorithm lgrthm=v4::paths::algorithm::automatic)
Definition paths.hpp:514
Paths & operator=(Paths &&)
Default move assignment operator.
output_type get() const
Get the current path in the range.
Definition paths.hpp:794
Paths & target_no_checks(node_type n) noexcept
Set the target node of every path in the range.
Definition paths.hpp:946
uint64_t count() const
Get the size of the range.
Definition paths.hpp:856
node_type target() const noexcept
Get the current target node of every path in the range.
Definition paths.hpp:983
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source, size_t min, size_t max)
Definition paths.hpp:459
Order order() const noexcept
Get the order of the paths in the range.
Definition paths.hpp:1083
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min, size_t max, v4::paths::algorithm lgrthm=v4::paths::algorithm::automatic)
Definition paths.hpp:608
auto cbegin_pilo(WordGraph< Node1 > const &wg, Node2 source, size_t min=0, size_t max=POSITIVE_INFINITY)
Returns an iterator for pilo (Path And Node In Lex Order).
Definition paths.hpp:112
Paths & target(node_type n)
Definition paths.hpp:967
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min, size_t max)
Definition paths.hpp:551
auto cend_pislo(WordGraph< Node > const &wg)
Definition paths.hpp:217
auto cbegin_pstislo(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min=0, size_t max=POSITIVE_INFINITY)
Definition paths.hpp:343
Paths & order(Order val)
Set the order of the paths in the range.
Definition paths.hpp:1070
uint64_t size_hint() const
Get the size of the range.
Paths & init(WordGraph< Node > const &wg)
Reinitialize a Paths object.
Definition paths.hpp:765
Paths & source_no_checks(node_type n) noexcept
Set the source node of every path in the range.
Definition paths.hpp:887
Class for representing word graphs.
Definition word-graph.hpp:83
std::size_t size_type
Unsigned integer type.
Definition word-graph.hpp:105
Undefined const UNDEFINED
Value for something undefined.
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:99
Order
The possible orderings of words and strings.
Definition order.hpp:54
Namespace containing helper functions for the Paths class.
Definition paths.hpp:61
void throw_if_node_out_of_bounds(WordGraph< Node1 > const &wg, Node2 n)
Throws if a node is out of bounds.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44