libsemigroups  v3.0.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) check code coverage
23
24#ifndef LIBSEMIGROUPS_PATHS_HPP_
25#define LIBSEMIGROUPS_PATHS_HPP_
26
27#include <algorithm> // for any_of, for_each, max_element
28#include <cstddef> // for size_t, ptrdiff_t
29#include <cstdint> // for uint64_t
30#include <iterator> // for distance, forward_iterator_tag
31#include <numeric> // for accumulate
32#include <string> // for std::string
33#include <type_traits> // for true_type
34#include <variant> // for visit, variant
35#include <vector> // for vector, allocator
36
37#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_ENA...
38#include "constants.hpp" // for Max, UNDEFINED, Positive...
39#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
40#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
41#include "order.hpp" // for order
42#include "ranges.hpp" // for is_input_range
43#include "types.hpp" // for word_type
44#include "word-graph.hpp" // for WordGraph
45#include "word-range.hpp" // for number_of_words
46
47#include "detail/containers.hpp" // for DynamicArray2
48#include "detail/path-iterators.hpp" // for default_postfix_increment
49
50namespace libsemigroups {
56 namespace paths {
71 } // namespace paths
72
117 // not noexcept because constructors of const_pilo_iterator aren't
118 template <typename Node1, typename Node2>
119 [[nodiscard]] auto cbegin_pilo(WordGraph<Node1> const& wg,
120 Node2 source,
121 size_t min = 0,
122 size_t max = POSITIVE_INFINITY) {
123 word_graph::throw_if_node_out_of_bounds(wg, static_cast<Node1>(source));
124 return detail::const_pilo_iterator<Node1>(&wg, source, min, max);
125 }
126
137 // not noexcept because constructors of const_pilo_iterator aren't
138 template <typename Node>
139 [[nodiscard]] auto cend_pilo(WordGraph<Node> const& wg) {
140 return detail::const_pilo_iterator<Node>(&wg, 0, 0, 0);
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
208 // not noexcept because detail::const_pislo_iterator constructors aren't
209 template <typename Node>
210 [[nodiscard]] auto cend_pislo(WordGraph<Node> const& wg) {
211 return detail::const_pislo_iterator<Node>(&wg, UNDEFINED, 0, 0);
212 }
213
254 // not noexcept because detail::const_pstilo_iterator constructors aren't
255 template <typename Node1, typename Node2>
256 [[nodiscard]] auto cbegin_pstilo(WordGraph<Node1> const& wg,
257 Node2 source,
258 Node2 target,
259 size_t min = 0,
260 size_t max = POSITIVE_INFINITY) {
261 // source & target are validated in is_reachable.
263 return cend_pstilo(wg);
264 }
265 return detail::const_pstilo_iterator<Node1>(&wg, source, target, min, max);
266 }
267
278 // not noexcept because detail::const_pstilo_iterator constructors aren't
279 template <typename Node>
280 [[nodiscard]] auto cend_pstilo(WordGraph<Node> const& wg) {
281 return detail::const_pstilo_iterator<Node>(&wg, 0, 0, 0, 0);
282 }
283
326 // not noexcept because cbegin_pislo isn't
327 template <typename Node1, typename Node2>
328 [[nodiscard]] auto cbegin_pstislo(WordGraph<Node1> const& wg,
329 Node2 source,
330 Node2 target,
331 size_t min = 0,
332 size_t max = POSITIVE_INFINITY) {
333 // source & target are validated in is_reachable.
335 return cend_pstislo(wg);
336 }
337 return detail::const_pstislo_iterator<Node1>(&wg, source, target, min, max);
338 }
339
352 // not noexcept because cend_pislo isn't
353 template <typename Node>
354 [[nodiscard]] auto cend_pstislo(WordGraph<Node> const& wg) {
355 return detail::const_pstislo_iterator<Node>(
356 &wg, UNDEFINED, UNDEFINED, 0, 0);
357 }
358
372 template <typename Node1, typename Node2>
373 [[nodiscard]] paths::algorithm
375 (void) wg;
376 (void) source;
378 }
379
397 template <typename Node1, typename Node2>
398 [[nodiscard]] uint64_t number_of_paths(WordGraph<Node1> const& wg,
399 Node2 source);
400
421 // Not noexcept because word_graph::topological_sort is not.
422 template <typename Node1, typename Node2>
423 [[nodiscard]] paths::algorithm
425 Node2 source,
426 size_t min,
427 size_t max);
428
472 // not noexcept for example detail::number_of_paths_trivial can throw
473 template <typename Node1, typename Node2>
474 [[nodiscard]] uint64_t number_of_paths(WordGraph<Node1> const& wg,
475 Node2 source,
476 size_t min,
477 size_t max,
478 paths::algorithm lgrthm
480
503 // Not noexcept because word_graph::topological_sort isn't
504 template <typename Node1, typename Node2>
505 [[nodiscard]] paths::algorithm
507 Node2 source,
508 Node2 target,
509 size_t min,
510 size_t max);
511
552 // not noexcept because cbegin_pstilo isn't
553 template <typename Node1, typename Node2>
554 [[nodiscard]] uint64_t number_of_paths(WordGraph<Node1> const& wg,
555 Node2 source,
556 Node2 target,
557 size_t min,
558 size_t max,
559 paths::algorithm lgrthm
561
600 template <typename Node>
601 class Paths {
602 public:
608 using node_type = Node;
609
614
618 using output_type = word_type const&;
619
620 private:
621 using const_iterator = std::variant<detail::const_pstislo_iterator<Node>,
622 detail::const_pstilo_iterator<Node>,
623 detail::const_pislo_iterator<Node>,
624 detail::const_pilo_iterator<Node>>;
625
626 WordGraph<node_type> const* _word_graph;
627 Order _order;
628 size_type _max;
629 size_type _min;
630 mutable size_type _position;
631 node_type _source;
632 node_type _target;
633 mutable const_iterator _current;
634 mutable const_iterator _end;
635 mutable bool _current_valid;
636
637 bool set_iterator_no_checks() const;
638
639 // The following init function is private to avoid the case of constructing
640 // a Paths object without setting _word_graph. The subsequent default
641 // constructor is protected and still needed (instead of deleting it) to
642 // avoid problems with having uninitialised std::variants with copy
643 // constructors.
644
645 Paths& init();
646
647 protected:
648 Paths() = default;
649
650 public:
652 // Constructors + initialization
654
658 Paths(Paths const&);
659
663 Paths(Paths&&);
664
668 Paths& operator=(Paths const&);
669
673 Paths& operator=(Paths&&);
674
675 ~Paths();
676
689 explicit Paths(WordGraph<Node> const& wg) : Paths() {
690 init(wg);
691 }
692
709 Paths& init(WordGraph<Node> const& wg) {
710 init();
711 _word_graph = &wg;
712 return *this;
713 }
714
716 // Validation
718
725
727 // Functions + members required by rx::ranges
729
738 output_type get() const {
739 set_iterator_no_checks();
740 return std::visit(
741 [](auto& it) -> auto const& { return *it; }, _current);
742 }
743
751 void next() {
752 if (!at_end()) {
753 ++_position;
754 std::visit([](auto& it) { ++it; }, _current);
755 }
756 }
757
767 [[nodiscard]] bool at_end() const {
768 if (!set_iterator_no_checks()) {
769 return true;
770 }
771 return _current == _end;
772 }
773
786 [[nodiscard]] uint64_t size_hint() const;
787
800 [[nodiscard]] uint64_t count() const {
801 return size_hint();
802 }
803
804 static constexpr bool is_finite = true; // this isn't always true!
805 static constexpr bool is_idempotent = true;
806
808 // Settings
810
831 Paths& source_no_checks(node_type n) noexcept {
832 return source_no_checks(this, n);
833 }
834
856
866 [[nodiscard]] node_type source() const noexcept {
867 return _source;
868 }
869
890 Paths& target_no_checks(node_type n) noexcept {
891 return target_no_checks(this, n);
892 }
893
911 Paths& target(node_type n) {
912 if (n != UNDEFINED) {
914 }
915 return target_no_checks(n);
916 }
917
927 [[nodiscard]] node_type target() const noexcept {
928 return _target;
929 }
930
943 [[nodiscard]] node_type current_target() const;
944
957 Paths& min(size_type val) noexcept {
958 return min(this, val);
959 }
960
970 [[nodiscard]] size_type min() const noexcept {
971 return _min;
972 }
973
986 Paths& max(size_type val) noexcept {
987 return max(this, val);
988 }
989
999 [[nodiscard]] size_type max() const noexcept {
1000 return _max;
1001 }
1002
1014 Paths& order(Order val) {
1015 return order(this, val);
1016 }
1017
1027 [[nodiscard]] Order order() const noexcept {
1028 return _order;
1029 }
1030
1040 [[nodiscard]] WordGraph<Node> const& word_graph() const noexcept {
1041 return *_word_graph;
1042 }
1043
1044 protected:
1045 template <typename Subclass>
1046 Subclass& source_no_checks(Subclass* obj, node_type src) {
1047 _current_valid &= (src == _source);
1048 _source = src;
1049 return *obj;
1050 }
1051
1052 template <typename Subclass>
1053 Subclass& target_no_checks(Subclass* obj, node_type trgt) noexcept {
1054 _current_valid &= (trgt == _target);
1055 _target = trgt;
1056 return *obj;
1057 }
1058
1059 template <typename Subclass>
1060 Subclass& min(Subclass* obj, size_type min) noexcept {
1061 _current_valid &= (min == _min);
1062 _min = min;
1063 return *obj;
1064 }
1065
1066 template <typename Subclass>
1067 Subclass& max(Subclass* obj, size_type max) noexcept {
1068 _current_valid &= (max == _max);
1069 _max = max;
1070 return *obj;
1071 }
1072
1073 template <typename Subclass>
1074 Subclass& order(Subclass* obj, Order val);
1075 }; // class Paths
1076
1081 template <typename Node>
1082 Paths(WordGraph<Node> const&) -> Paths<Node>;
1083
1088 template <typename Node>
1089 Paths(WordGraph<Node>&&) -> Paths<Node>;
1090
1103 template <typename Node>
1105
1106} // namespace libsemigroups
1107
1108#include "paths.tpp"
1109
1110#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:256
Paths(WordGraph< Node > &&) -> Paths< Node >
void next()
Advance to the next path in the range.
Definition paths.hpp:751
typename WordGraph< uint32_t >::size_type size_type
Definition paths.hpp:613
uint32_t node_type
Definition paths.hpp:608
size_type min() const noexcept
Get the minimum length of path in the range.
Definition paths.hpp:970
word_type const & output_type
Definition paths.hpp:618
Paths(WordGraph< Node > const &) -> Paths< Node >
auto cend_pstilo(WordGraph< Node > const &wg)
Definition paths.hpp:280
Paths(Paths const &)
Default copy constructor.
WordGraph< Node > const & word_graph() const noexcept
The underlying WordGraph.
Definition paths.hpp:1040
Paths(Paths &&)
Default move constructor.
auto cend_pilo(WordGraph< Node > const &wg)
Definition paths.hpp:139
Paths & min(size_type val) noexcept
Definition paths.hpp:957
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source) noexcept
Definition paths.hpp:374
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:689
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:354
Paths & max(size_type val) noexcept
Definition paths.hpp:986
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:866
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source)
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:999
bool at_end() const
Check if the range is exhausted.
Definition paths.hpp:767
Paths & source(node_type n)
Definition paths.hpp:852
void throw_if_source_undefined() const
Throw an exception if the source node has not been defined (using source).
Paths & operator=(Paths &&)
Default move assignment operator.
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source, size_t min, size_t max, paths::algorithm lgrthm=paths::algorithm::automatic)
output_type get() const
Get the current path in the range.
Definition paths.hpp:738
Paths & target_no_checks(node_type n) noexcept
Set the target node of every path in the range.
Definition paths.hpp:890
uint64_t count() const
Get the size of the range.
Definition paths.hpp:800
node_type target() const noexcept
Get the current target node of every path in the range.
Definition paths.hpp:927
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source, size_t min, size_t max)
Order order() const noexcept
Get the order of the paths in the range.
Definition paths.hpp:1027
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:119
Paths & target(node_type n)
Definition paths.hpp:911
paths::algorithm number_of_paths_algorithm(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min, size_t max)
auto cend_pislo(WordGraph< Node > const &wg)
Definition paths.hpp:210
auto cbegin_pstislo(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min=0, size_t max=POSITIVE_INFINITY)
Definition paths.hpp:328
uint64_t number_of_paths(WordGraph< Node1 > const &wg, Node2 source, Node2 target, size_t min, size_t max, paths::algorithm lgrthm=paths::algorithm::automatic)
Paths & order(Order val)
Set the order of the paths in the range.
Definition paths.hpp:1014
uint64_t size_hint() const
Get the size of the range.
Paths & init(WordGraph< Node > const &wg)
Reinitialize a Paths object.
Definition paths.hpp:709
Paths & source_no_checks(node_type n) noexcept
Set the source node of every path in the range.
Definition paths.hpp:831
Class for representing word graphs.
Definition word-graph.hpp:82
std::size_t size_type
Unsigned integer type.
Definition word-graph.hpp:104
Undefined const UNDEFINED
Value for something undefined.
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
Order
The possible orderings of words and strings.
Definition order.hpp:48
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:101
Namespace containing helper functions for the Paths class.
Definition paths.hpp:56
algorithm
An enum for specifying the algorithm to the functions number_of_paths().
Definition paths.hpp:58
@ trivial
Try to utilise some corner cases.
Definition paths.hpp:66
@ matrix
Use the adjacency matrix and matrix multiplication.
Definition paths.hpp:62
@ automatic
Definition paths.hpp:69
@ dfs
Use a depth-first-search.
Definition paths.hpp:60
@ acyclic
Use a dynamic programming approach for acyclic word graphs.
Definition paths.hpp:64
void throw_if_node_out_of_bounds(WordGraph< Node1 > const &wg, Node2 n)
Throws if a node is out of bounds.
bool is_reachable(WordGraph< Node1 > const &wg, Node2 source, Node2 target)
Check if there is a path from one node to another.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44