libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
word-graph.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2019-2025 Finn Smith + 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 word graphs (which are basically
20// deterministic automata without initial or accept states).
21
22#ifndef LIBSEMIGROUPS_WORD_GRAPH_HPP_
23#define LIBSEMIGROUPS_WORD_GRAPH_HPP_
24
25#include <algorithm> // for uniform_int_distribution
26#include <cstddef> // for size_t
27#include <cstdint>
28#include <ostream> // for operator<<
29#include <queue> // for queue
30#include <random> // for mt19937
31#include <stack> // for stack
32#include <string> // for to_string
33#include <tuple> // for tie
34#include <type_traits> // for is_integral, is_unsigned
35#include <unordered_set> // for unordered_set
36#include <utility> // for pair
37#include <vector> // for vector
38
39#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_EN...
40#include "constants.hpp" // for UNDEFINED
41#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
42#include "dot.hpp" // for Dot
43#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
44#include "forest.hpp" // for Forest
45#include "order.hpp" // for Order
46#include "ranges.hpp" // for ??
47#include "types.hpp" // for word_type, enable_if_is_same
48
49#include "detail/containers.hpp" // for DynamicArray2
50#include "detail/int-range.hpp" // for IntRange
51#include "detail/stl.hpp" // for IsIterator
52#include "detail/uf.hpp" // for Duf
53
54#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
55#include "detail/eigen.hpp"
56#else
57#include "matrix.hpp"
58#endif
59
60namespace libsemigroups {
61
66
81 template <typename Node>
82 class WordGraph {
83 static_assert(std::is_integral<Node>(),
84 "the template parameter Node must be an integral type!");
85 static_assert(
87 "the template parameter Node must be an unsigned integral type!");
88
89 template <typename N>
90 friend class WordGraph;
91
92 public:
94 // WordGraph - typedefs - public
96
98 using node_type = Node;
99
101 using label_type = Node;
102
105
106#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
109 = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
110#else
113#endif
114
117 typename detail::IntRange<Node>::const_iterator;
118
121 typename detail::IntRange<Node>::const_reverse_iterator;
122
125 typename detail::DynamicArray2<Node>::const_iterator;
126
128 // WordGraph - constructors + destructor - public
130
146 // Not noexcept
147 explicit WordGraph(size_type m = 0, size_type n = 0);
148
167 WordGraph& init(size_type m = 0, size_type n = 0);
168
170 WordGraph(WordGraph const&);
171
181 template <typename OtherNode>
182 WordGraph(WordGraph<OtherNode> const& that);
183
204 template <typename OtherNode>
205 WordGraph& init(WordGraph<OtherNode> const& that);
206
208 WordGraph(WordGraph&&);
209
211 WordGraph& operator=(WordGraph const&);
212
214 WordGraph& operator=(WordGraph&&);
215
216 ~WordGraph();
217
238 static WordGraph random(size_type number_of_nodes,
240 std::mt19937 mt
242
266 // Not noexcept because DynamicArray2::add_cols isn't.
267 WordGraph& reserve(size_type m, size_type n);
268
270 // WordGraph - modifiers - public
272
291 // Not noexcept because DynamicArray2::add_rows isn't.
292 WordGraph& add_nodes(size_type nr);
293
313 // Not noexcept because DynamicArray2::add_cols isn't.
314 WordGraph& add_to_out_degree(size_type nr);
315
333 // Not noexcept because throw_if_node_out_of_bounds/label aren't
334 // return *this by reference.
335 WordGraph& target(node_type s, label_type a, node_type t);
336
355 //! No checks whatsoever on the validity of the arguments are performed.
356 inline WordGraph& target_no_checks(node_type s, label_type a, node_type t) {
357 _dynamic_array_2.set(s, a, t);
358 return *this;
359 }
360
377 //! No checks whatsoever on the validity of the arguments are performed.
378 inline WordGraph& remove_target_no_checks(node_type s, label_type a) {
379 _dynamic_array_2.set(s, a, UNDEFINED);
380 return *this;
381 }
382
396 WordGraph& remove_target(node_type s, label_type a);
397
408 WordGraph& remove_label(label_type a);
409
425
438 //! out-degree.
439 inline WordGraph& remove_all_targets() {
440 std::fill(_dynamic_array_2.begin(), _dynamic_array_2.end(), UNDEFINED);
441 return *this;
442 }
443
463 // swap m - a - > m' and n - a -> n'
465 _dynamic_array_2.swap(m, a, n, a);
466 return *this;
467 }
468
484 WordGraph& swap_targets(node_type m, node_type n, label_type a);
485
501 //! is the out-degree of the word graph.
502 [[nodiscard]] bool operator==(WordGraph const& that) const {
503 return _dynamic_array_2 == that._dynamic_array_2;
504 }
505
521 //! is the out-degree of the word graph.
522 [[nodiscard]] bool operator!=(WordGraph const& that) const {
523 return !operator==(that);
524 }
525
541 //! is the out-degree of the word graph.
542 [[nodiscard]] bool operator<(WordGraph const& that) const {
543 return _dynamic_array_2 < that._dynamic_array_2;
544 }
545
561 //! is the out-degree of the word graph.
562 [[nodiscard]] bool operator<=(WordGraph const& that) const {
563 return _dynamic_array_2 <= that._dynamic_array_2;
564 }
565
581 //! is the out-degree of the word graph.
582 [[nodiscard]] bool operator>(WordGraph const& that) const {
583 return _dynamic_array_2 > that._dynamic_array_2;
584 }
585
601 //! is the out-degree of the word graph.
602 [[nodiscard]] bool operator>=(WordGraph const& that) const {
603 return _dynamic_array_2 > that._dynamic_array_2;
604 }
605
616 // not noexcept because Hash<T>::operator() isn't
617 [[nodiscard]] size_t hash_value() const {
618 return std::hash<decltype(_dynamic_array_2)>()(_dynamic_array_2);
619 }
620
622 // WordGraph - nodes, targets, etc - public
624
642 // Not noexcept because throw_if_node_out_of_bounds/label aren't
643 [[nodiscard]] node_type target(node_type source, label_type a) const;
644
664 // Not noexcept because DynamicArray2::get is not
665 [[nodiscard]] node_type inline target_no_checks(node_type s,
666 label_type a) const {
667 return _dynamic_array_2.get(s, a);
668 }
669
698 // Not noexcept because DynamicArray2::get is not
701
732 // Not noexcept because next_label_and_target_no_checks is not
735
747 //! Constant.
748 [[nodiscard]] size_type inline number_of_nodes() const noexcept {
749 return _nr_nodes;
750 }
751
752#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
753 WordGraph& number_of_active_nodes(size_type val) {
754 _num_active_nodes = val;
755 return *this;
756 }
757
758 [[nodiscard]] size_type inline number_of_active_nodes() const noexcept {
759 return _num_active_nodes;
760 }
761#endif
762
776 // Not noexcept because std::count isn't
777 [[nodiscard]] size_type number_of_edges() const;
778
793 [[nodiscard]] size_type number_of_edges(node_type s) const;
794
810 [[nodiscard]] size_type number_of_edges_no_checks(node_type s) const;
811
823 //! Constant.
824 [[nodiscard]] size_type out_degree() const noexcept {
825 return _degree;
826 }
827
841 //! Constant.
842 const_iterator_nodes cbegin_nodes() const noexcept {
843 return detail::IntRange<node_type>(0, number_of_nodes()).cbegin();
844 }
845
859 //! Constant.
860 [[nodiscard]] const_iterator_nodes cend_nodes() const noexcept {
861 return detail::IntRange<node_type>(0, number_of_nodes()).cend();
862 }
863
881 // Not noexcept because throw_if_node_out_of_bounds isn't
882 [[nodiscard]] const_iterator_targets cbegin_targets(node_type source) const;
883
908 cbegin_targets_no_checks(node_type source) const noexcept {
909 return _dynamic_array_2.cbegin_row(source);
910 }
911
929 // Not noexcept because throw_if_node_out_of_bounds isn't
930 [[nodiscard]] const_iterator_targets cend_targets(node_type source) const;
931
956 cend_targets_no_checks(node_type source) const noexcept {
957 return _dynamic_array_2.cbegin_row(source) + _degree;
958 }
959
968 //! \noexcept
969 [[nodiscard]] auto nodes() const noexcept {
970 return rx::seq<node_type>() | rx::take(number_of_nodes());
971 }
972
982 //! \noexcept
983 [[nodiscard]] auto labels() const noexcept {
984 return rx::seq<label_type>() | rx::take(out_degree());
985 }
986
1004 [[nodiscard]] rx::iterator_range<const_iterator_targets>
1005 targets_no_checks(node_type source) const noexcept {
1006 return rx::iterator_range(cbegin_targets_no_checks(source),
1007 cend_targets_no_checks(source));
1008 }
1009
1022 [[nodiscard]] rx::iterator_range<const_iterator_targets>
1023 targets(node_type source) const;
1024
1042 [[nodiscard]] auto
1043 labels_and_targets_no_checks(node_type source) const noexcept {
1044 return rx::enumerate(targets_no_checks(source));
1045 }
1046
1058 [[nodiscard]] auto labels_and_targets(node_type source) const;
1059
1078 WordGraph& induced_subgraph_no_checks(node_type first, node_type last);
1079
1094 WordGraph& induced_subgraph(node_type first, node_type last);
1095
1118 template <typename Iterator,
1119 typename = std::enable_if_t<detail::IsIterator<Iterator>::value>>
1120 WordGraph& induced_subgraph_no_checks(Iterator first, Iterator last);
1121
1143 template <typename Iterator,
1144 typename = std::enable_if_t<detail::IsIterator<Iterator>::value>>
1145 WordGraph& induced_subgraph(Iterator first, Iterator last);
1146
1162 WordGraph& disjoint_union_inplace_no_checks(WordGraph<Node> const& that);
1163
1176 WordGraph& disjoint_union_inplace(WordGraph<Node> const& that);
1177
1178#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1179 // These are currently undocumented, because they are hard to use correctly,
1180 // shouldn't p and q be actual permutation objects?
1181
1182 // requires access to apply_row_permutation so can't be helper
1183 WordGraph& permute_nodes_no_checks(std::vector<node_type> const& p,
1184 std::vector<node_type> const& q,
1185 size_t m);
1186
1187 WordGraph& permute_nodes_no_checks(std::vector<node_type> const& p,
1188 std::vector<node_type> const& q) {
1189 return permute_nodes_no_checks(p, q, p.size());
1190 }
1191#endif
1192
1193 protected:
1194 // from WordGraphWithSources
1195 template <typename S>
1196 void apply_row_permutation(S const& p) {
1197 _dynamic_array_2.apply_row_permutation(p);
1198 }
1199
1200 private:
1202 // WordGraph - data members - private
1204
1205 size_type _degree;
1206 size_type _nr_nodes;
1207 // TODO(1) remove when WordGraphView is implemented
1208 size_type _num_active_nodes;
1209 mutable detail::DynamicArray2<Node> _dynamic_array_2;
1210 };
1211
1221 namespace word_graph {
1222
1224 // WordGraph - helper functions - in alphabetical order!!!
1226
1249 // TODO(1) add add_cycle with checks version.
1250 template <typename Node, typename Iterator>
1252 Iterator first,
1253 Iterator last);
1254
1270 template <typename Node>
1271 void add_cycle(WordGraph<Node>& wg, size_t N) {
1272 size_t M = wg.number_of_nodes();
1273 wg.add_nodes(N);
1274 add_cycle_no_checks(wg, wg.cbegin_nodes() + M, wg.cend_nodes());
1275 }
1276
1291 template <typename Node>
1292 [[nodiscard]] auto adjacency_matrix(WordGraph<Node> const& wg);
1293
1304 template <typename Node>
1305 [[nodiscard]] Dot dot(WordGraph<Node> const& wg);
1306
1333 template <typename Node>
1334 [[nodiscard]] bool equal_to_no_checks(WordGraph<Node> const& x,
1335 WordGraph<Node> const& y,
1336 Node first,
1337 Node last);
1338
1363 template <typename Node>
1364 [[nodiscard]] bool equal_to(WordGraph<Node> const& x,
1365 WordGraph<Node> const& y,
1366 Node first,
1367 Node last);
1368
1400 template <typename Node1, typename Node2, typename Iterator>
1401 [[nodiscard]] Node1 follow_path(WordGraph<Node1> const& wg,
1402 Node2 source,
1403 Iterator first,
1404 Iterator last);
1405
1433 // TODO(2) example
1434 // not noexcept because WordGraph::target isn't
1435 template <typename Node1, typename Node2>
1436 [[nodiscard]] Node1 follow_path(WordGraph<Node1> const& wg,
1437 Node2 from,
1438 word_type const& path) {
1439 static_assert(sizeof(Node2) <= sizeof(Node1));
1440 return follow_path(wg, from, path.cbegin(), path.cend());
1441 }
1442
1469 template <typename Node1, typename Node2, typename Iterator>
1470 [[nodiscard]] Node1 follow_path_no_checks(WordGraph<Node1> const& wg,
1471 Node2 from,
1472 Iterator first,
1473 Iterator last) noexcept;
1474
1501 template <typename Node1, typename Node2>
1502 [[nodiscard]] Node1 follow_path_no_checks(WordGraph<Node1> const& wg,
1503 Node2 from,
1504 word_type const& path) noexcept {
1505 static_assert(sizeof(Node2) <= sizeof(Node1));
1506 return follow_path_no_checks(wg, from, path.cbegin(), path.cend());
1507 }
1508
1541 // Not noexcept because detail::is_acyclic isn't
1542 template <typename Node>
1543 [[nodiscard]] bool is_acyclic(WordGraph<Node> const& wg);
1544
1589 // Not noexcept because detail::is_acyclic isn't
1590 template <typename Node1, typename Node2>
1591 [[nodiscard]] bool is_acyclic(WordGraph<Node1> const& wg, Node2 source);
1592
1622 // Not noexcept because detail::is_acyclic isn't
1623 template <typename Node1, typename Node2>
1624 [[nodiscard]] bool is_acyclic(WordGraph<Node1> const& wg,
1625 Node2 source,
1626 Node2 target);
1627
1667 template <typename Node,
1668 typename Iterator1,
1669 typename Iterator2,
1670 typename Iterator3>
1671 [[nodiscard]] bool is_compatible_no_checks(WordGraph<Node> const& wg,
1672 Iterator1 first_node,
1673 Iterator2 last_node,
1674 Iterator3 first_rule,
1675 Iterator3 last_rule);
1676
1718 template <
1719 typename Node,
1720 typename Iterator1,
1721 typename Iterator2,
1722 typename Iterator3,
1723 std::enable_if_t<!std::is_same_v<std::decay_t<Iterator3>, word_type>>>
1724 [[nodiscard]] bool is_compatible(WordGraph<Node> const& wg,
1725 Iterator1 first_node,
1726 Iterator2 last_node,
1727 Iterator3 first_rule,
1728 Iterator3 last_rule);
1729
1761 template <typename Node, typename Iterator1, typename Iterator2>
1763 Iterator1 first_node,
1764 Iterator2 last_node,
1765 word_type const& lhs,
1766 word_type const& rhs);
1767
1803 template <typename Node, typename Iterator1, typename Iterator2>
1805 Iterator1 first_node,
1806 Iterator2 last_node,
1807 word_type const& lhs,
1808 word_type const& rhs);
1809
1841 template <typename Node, typename Iterator1, typename Iterator2>
1842 [[nodiscard]] bool is_complete_no_checks(WordGraph<Node> const& wg,
1843 Iterator1 first_node,
1844 Iterator2 last_node);
1845
1875 template <typename Node, typename Iterator1, typename Iterator2>
1876 [[nodiscard]] bool is_complete(WordGraph<Node> const& wg,
1877 Iterator1 first_node,
1878 Iterator2 last_node);
1879
1897 template <typename Node>
1898 [[nodiscard]] bool is_complete(WordGraph<Node> const& wg) noexcept {
1899 return wg.number_of_edges() == wg.number_of_nodes() * wg.out_degree();
1900 }
1901
1923 template <typename Node>
1924 [[nodiscard]] bool is_connected(WordGraph<Node> const& wg);
1925
1975 template <typename Node1, typename Node2>
1976 [[nodiscard]] bool is_reachable_no_checks(WordGraph<Node1> const& wg,
1977 Node2 source,
1978 Node2 target);
1979
2011 template <typename Node1, typename Node2>
2012 [[nodiscard]] bool is_reachable(WordGraph<Node1> const& wg,
2013 Node2 source,
2014 Node2 target);
2015
2047 // TODO(1) should have a version that returns the node that everything is
2048 // reachable from
2049 template <typename Node>
2050 [[nodiscard]] bool is_strictly_cyclic(WordGraph<Node> const& wg);
2051
2078 template <typename Node1, typename Node2, typename Iterator>
2079 [[nodiscard]] std::pair<Node1, Iterator>
2081 Node2 source,
2082 Iterator first,
2083 Iterator last) noexcept;
2084
2109 template <typename Node1, typename Node2, typename Iterator>
2110 [[nodiscard]] std::pair<Node1, Iterator>
2112 Node2 source,
2113 Iterator first,
2114 Iterator last);
2115
2137 template <typename Node1, typename Node2>
2140 Node2 source,
2141 word_type const& w);
2142
2164 template <typename Node1, typename Node2>
2167 Node2 source,
2168 word_type const& w);
2169
2190 // TODO(1) tests
2191 // TODO(1) version where std::unordered_set is passed by reference, or make
2192 // this a class that stores its stack and unordered_set, not clear why we'd
2193 // single out the unordered_set to be passed by reference.
2194 // TODO(2) version which is an iterator i.e. returns an iterator or range
2195 // object that allows use to step through the nodes reachable from a given
2196 // node
2197 template <typename Node1, typename Node2>
2198 [[nodiscard]] std::unordered_set<Node1>
2199 nodes_reachable_from(WordGraph<Node1> const& wg, Node2 source);
2200
2201 template <typename Node1, typename Node2>
2202 [[nodiscard]] std::unordered_set<Node1>
2203 ancestors_of(WordGraph<Node1> const& wg, Node2 source);
2204
2226 template <typename Node1, typename Node2>
2227 [[nodiscard]] std::unordered_set<Node1>
2229
2230 template <typename Node1, typename Node2>
2231 [[nodiscard]] std::unordered_set<Node1>
2232 ancestors_of_no_checks(WordGraph<Node1> const& wg, Node2 source);
2233
2254 template <typename Node1, typename Node2>
2255 [[nodiscard]] size_t
2257 return nodes_reachable_from(wg, source).size();
2258 }
2259
2278 template <typename Node1, typename Node2>
2279 [[nodiscard]] size_t
2281 Node2 source) {
2282 return nodes_reachable_from_no_checks(wg, source).size();
2283 }
2284
2306 template <typename Node>
2307 WordGraph<Node> random_acyclic(size_t number_of_nodes,
2308 size_t out_degree,
2309 std::mt19937 mt
2311
2331 template <typename Node1, typename Node2>
2333 Node2 root,
2334 Forest& f);
2335
2354 template <typename Node1, typename Node2>
2355 void spanning_tree(WordGraph<Node1> const& wg, Node2 root, Forest& f);
2356
2377 template <typename Node1, typename Node2>
2379 Node2 root);
2380
2400 template <typename Node1, typename Node2>
2401 [[nodiscard]] Forest spanning_tree(WordGraph<Node1> const& wg, Node2 root);
2402
2426 // Not nodiscard because sometimes we just don't want the output
2427 template <typename Graph>
2428 bool standardize(Graph& wg, Forest& f, Order val);
2429
2453 // Not nodiscard because sometimes we just don't want the output
2454 template <typename Graph>
2456
2473 template <typename Node>
2475 Order val = Order::shortlex);
2476
2490 template <typename Node>
2492
2514 template <typename Node, typename Iterator>
2516 Iterator first,
2517 Iterator last);
2518
2530 // not noexcept because it throws an exception!
2531 template <typename Node>
2533 typename WordGraph<Node>::label_type a);
2534
2547 template <typename Node>
2549 word_type const& word);
2550
2566 template <typename Node, typename Iterator>
2568 Iterator first,
2569 Iterator last);
2570
2583 // not noexcept because it throws an exception!
2584 template <typename Node1, typename Node2>
2586
2602 // not noexcept because it throws an exception!
2603 template <typename Node, typename Iterator1, typename Iterator2>
2605 Iterator1 first,
2606 Iterator2 last);
2607
2631 template <typename Node>
2633
2660 template <typename Node1, typename Node2>
2661 [[nodiscard]] std::vector<Node1>
2662 topological_sort(WordGraph<Node1> const& wg, Node2 source);
2663
2664 } // namespace word_graph
2665
2666 namespace detail {
2667
2668 template <typename T>
2669 struct IsWordGraphHelper : std::false_type {};
2670
2671 template <typename Node>
2672 struct IsWordGraphHelper<WordGraph<Node>> : std::true_type {};
2673 } // namespace detail
2674
2676 // WordGraph - non-member functions
2678
2686 template <typename T>
2687 static constexpr bool IsWordGraph = detail::IsWordGraphHelper<T>::value;
2688
2708 template <typename Node>
2710
2721
2749 // Passing the 2nd parameter "targets" by value disambiguates it from the
2750 // other make<WordGraph>.
2751 template <typename Return>
2752 [[nodiscard]] std::enable_if_t<IsWordGraph<Return>, Return>
2753 make(size_t num_nodes,
2755
2758 // clang-format off
2760 // clang-format on
2761 template <typename Return>
2762 [[nodiscard]] std::enable_if_t<IsWordGraph<Return>, Return>
2763 make(size_t num_nodes,
2765
2766 namespace detail {
2767 template <typename Subclass>
2768 class JoinerMeeterCommon {
2769 private:
2770 template <typename Node1, typename Node2>
2771 void throw_if_bad_args(WordGraph<Node1> const& x,
2772 Node2 xroot,
2773 WordGraph<Node1> const& y,
2774 Node2 yroot);
2775
2776 public:
2777 template <typename Node>
2778 void call_no_checks(WordGraph<Node>& xy,
2779 WordGraph<Node> const& x,
2780 Node xroot,
2781 WordGraph<Node> const& y,
2782 Node yroot);
2783
2784 template <typename Node>
2785 void call_no_checks(WordGraph<Node>& xy,
2786 WordGraph<Node> const& x,
2787 WordGraph<Node> const& y) {
2788 return call_no_checks(
2789 xy, x, static_cast<Node>(0), y, static_cast<Node>(0));
2790 }
2791
2792 template <typename Node, typename... Args>
2793 [[nodiscard]] auto call_no_checks(WordGraph<Node> const& x,
2794 Args&&... args)
2795 -> std::enable_if_t<sizeof...(Args) % 2 == 1, WordGraph<Node>> {
2796 // The versions of this function changing the 1st argument in-place
2797 // always have an odd number of arguments, so we check that it's even
2798 // here (the argument x and an odd number of further arguments).
2799 WordGraph<Node> xy;
2800 static_cast<Subclass&>(*this).call_no_checks(
2801 xy, x, std::forward<Args>(args)...);
2802 return xy;
2803 }
2804
2805 // There's no operator() with the number of nodes reachable from the
2806 // roots as arguments (7 args in total) because we'd have to check that
2807 // they were valid, and the only way to do this is to recompute them.
2808
2809 template <typename Node>
2810 void operator()(WordGraph<Node>& xy,
2811 WordGraph<Node> const& x,
2812 Node xroot,
2813 WordGraph<Node> const& y,
2814 Node yroot) {
2815 throw_if_bad_args(x, xroot, y, yroot);
2816 call_no_checks(xy, x, xroot, y, yroot);
2817 }
2818
2819 template <typename Node>
2820 void operator()(WordGraph<Node>& xy,
2821 WordGraph<Node> const& x,
2822 WordGraph<Node> const& y) {
2823 return operator()(xy, x, static_cast<Node>(0), y, static_cast<Node>(0));
2824 }
2825
2826 template <typename Node, typename... Args>
2827 [[nodiscard]] auto operator()(WordGraph<Node> const& x, Args&&... args)
2828 -> std::enable_if_t<sizeof...(Args) % 2 == 1, WordGraph<Node>> {
2829 // The versions of this function changing the 1st argument in-place
2830 // always have an odd number of arguments, so we check that it's even
2831 // here (the argument x and an odd number of further arguments).
2832 WordGraph<Node> xy;
2833 operator()(xy, x, std::forward<Args>(args)...);
2834 return xy;
2835 }
2836
2837 template <typename Node1, typename Node2>
2838 bool is_subrelation_no_checks(WordGraph<Node1> const& x,
2839 Node2 xroot,
2840 WordGraph<Node1> const& y,
2841 Node2 yroot);
2842
2843 template <typename Node>
2844 bool is_subrelation_no_checks(WordGraph<Node> const& x,
2845 WordGraph<Node> const& y) {
2846 return is_subrelation_no_checks(
2847 x, static_cast<Node>(0), y, static_cast<Node>(0));
2848 }
2849
2850 // There's no subrelation with the number of nodes reachable from the
2851 // roots as arguments (6 args in total) because we'd have to check that
2852 // they were valid, and the only way to do this is to recompute them.
2853
2854 template <typename Node1, typename Node2>
2855 bool is_subrelation(WordGraph<Node1> const& x,
2856 Node2 xroot,
2857 WordGraph<Node1> const& y,
2858 Node2 yroot) {
2859 throw_if_bad_args(x, xroot, y, yroot);
2860 return is_subrelation_no_checks(x, xroot, y, yroot);
2861 }
2862
2863 template <typename Node>
2864 bool is_subrelation(WordGraph<Node> const& x, WordGraph<Node> const& y) {
2865 return is_subrelation(x, static_cast<Node>(0), y, static_cast<Node>(0));
2866 }
2867 }; // JoinerMeeterCommon
2868 } // namespace detail
2869
2881 // This class is intentionally not a template so that we don't have to
2882 // specify the types of the nodes when constructing one of these objects.
2883 // Instead every member function has a template parameter Node, which is
2884 // deduced from the argument.
2885 class Joiner : public detail::JoinerMeeterCommon<Joiner> {
2886 private:
2887 detail::Duf<> _uf;
2889 std::vector<uint64_t> _lookup;
2890
2891 template <typename Node>
2892 [[nodiscard]] Node find(WordGraph<Node> const& x,
2893 size_t xnum_nodes_reachable_from_root,
2894 WordGraph<Node> const& y,
2895 uint64_t n,
2896 typename WordGraph<Node>::label_type a) const;
2897
2898 template <typename Node>
2899 void run(WordGraph<Node> const& x,
2900 size_t xnum_nodes_reachable_from_root,
2901 Node xroot,
2902 WordGraph<Node> const& y,
2903 size_t ynum_nodes_reachable_from_root,
2904 Node yroot);
2905
2906 public:
2911
2916
2921
2926
2931
2932 ~Joiner();
2933
2962 template <typename Node>
2964 WordGraph<Node> const& x,
2965 size_t xnum_nodes_reachable_from_root,
2966 Node xroot,
2967 WordGraph<Node> const& y,
2968 size_t ynum_nodes_reachable_from_root,
2969 Node yroot);
2970
3004 // Is x a subrelation of y?
3005 template <typename Node1, typename Node2>
3006 [[nodiscard]] bool
3008 size_t xnum_nodes_reachable_from_root,
3009 Node2 xroot,
3010 WordGraph<Node1> const& y,
3011 size_t ynum_nodes_reachable_from_root,
3012 Node2 yroot);
3013#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3037 template <typename Node>
3039 WordGraph<Node> const& x,
3040 Node xroot,
3041 WordGraph<Node> const& y,
3042 Node yroot);
3043
3063 template <typename Node>
3065 WordGraph<Node> const& x,
3066 WordGraph<Node> const& y);
3067
3087 template <typename Node, typename... Args>
3088 [[nodiscard]] auto call_no_checks(WordGraph<Node> const& x, Args&&... args);
3089
3115 template <typename Node>
3117 WordGraph<Node> const& x,
3118 Node xroot,
3119 WordGraph<Node> const& y,
3120 Node yroot);
3121
3143 template <typename Node>
3145 WordGraph<Node> const& x,
3146 WordGraph<Node> const& y);
3147
3165 template <typename Node, typename... Args>
3166 [[nodiscard]] auto operator()(WordGraph<Node> const& x, Args&&... args);
3167
3197 // Is x a subrelation of y?
3198 template <typename Node1, typename Node2>
3200 Node2 xroot,
3201 WordGraph<Node1> const& y,
3202 Node2 yroot);
3203
3226 template <typename Node>
3228 WordGraph<Node> const& y);
3229
3230 // There's no subrelation with the number of nodes reachable from the
3231 // roots as arguments (6 args in total) because we'd have to check that
3232 // they were valid, and the only way to do this is to recompute them.
3233
3262 template <typename Node1, typename Node2>
3264 Node2 xroot,
3265 WordGraph<Node1> const& y,
3266 Node2 yroot);
3267
3292 template <typename Node>
3294
3295#else
3296 using detail::JoinerMeeterCommon<Joiner>::call_no_checks;
3297 using detail::JoinerMeeterCommon<Joiner>::operator();
3298 using detail::JoinerMeeterCommon<Joiner>::is_subrelation_no_checks;
3299 using detail::JoinerMeeterCommon<Joiner>::is_subrelation;
3300#endif
3301 }; // Joiner
3302
3315 // Class for forming the meet of two word graphs
3316 class Meeter : public detail::JoinerMeeterCommon<Meeter> {
3317 private:
3318 using node_type = std::pair<uint64_t, uint64_t>;
3319
3322 std::vector<node_type> _todo_new;
3323
3324 public:
3329
3334
3339
3344
3349
3350 ~Meeter();
3351
3354 template <typename Node>
3356 WordGraph<Node> const& x,
3357 size_t xnum_nodes_reachable_from_root,
3358 Node xroot,
3359 WordGraph<Node> const& y,
3360 size_t ynum_nodes_reachable_from_root,
3361 Node yroot);
3362
3365 // is x a subrelation of y
3366 template <typename Node1, typename Node2>
3367 [[nodiscard]] bool
3369 size_t xnum_nodes_reachable_from_root,
3370 Node2 xroot,
3371 WordGraph<Node1> const& y,
3372 size_t ynum_nodes_reachable_from_root,
3373 Node2 yroot);
3374
3375#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3378 template <typename Node>
3380 WordGraph<Node> const& x,
3381 Node xroot,
3382 WordGraph<Node> const& y,
3383 Node yroot);
3384
3387 template <typename Node>
3389 WordGraph<Node> const& x,
3390 WordGraph<Node> const& y);
3391
3411 template <typename Node, typename... Args>
3412 [[nodiscard]] auto call_no_checks(WordGraph<Node> const& x, Args&&... args);
3413
3416 template <typename Node>
3418 WordGraph<Node> const& x,
3419 Node xroot,
3420 WordGraph<Node> const& y,
3421 Node yroot);
3422
3425 template <typename Node>
3427 WordGraph<Node> const& x,
3428 WordGraph<Node> const& y);
3429
3447 template <typename Node, typename... Args>
3448 [[nodiscard]] auto operator()(WordGraph<Node> const& x, Args&&... args);
3449
3452 template <typename Node1, typename Node2>
3454 Node2 xroot,
3455 WordGraph<Node1> const& y,
3456 Node2 yroot);
3457
3460 template <typename Node>
3462 WordGraph<Node> const& y);
3463
3466 template <typename Node1, typename Node2>
3468 Node2 xroot,
3469 WordGraph<Node1> const& y,
3470 Node2 yroot);
3471
3474 template <typename Node>
3476#else
3477 using detail::JoinerMeeterCommon<Meeter>::call_no_checks;
3478 using detail::JoinerMeeterCommon<Meeter>::operator();
3479 using detail::JoinerMeeterCommon<Meeter>::is_subrelation_no_checks;
3480 using detail::JoinerMeeterCommon<Meeter>::is_subrelation;
3481#endif
3482 }; // class Meeter
3483
3498 template <typename Node>
3500
3513 [[nodiscard]] static inline std::string
3515 (void) meet;
3516 return "<Meeter of word graphs>";
3517 }
3518
3531 [[nodiscard]] static inline std::string
3533 (void) join;
3534 return "<Joiner of word graphs>";
3535 }
3536
3556 template <typename Node>
3558 std::string const& prefix = "",
3559 std::string const& braces = "{}",
3560 std::string const& suffix = "");
3561} // namespace libsemigroups
3562
3563#include "word-graph.tpp"
3564
3565#endif // LIBSEMIGROUPS_WORD_GRAPH_HPP_
T cbegin(T... args)
A representation of a graph in the DOT language of Graphviz.
Definition dot.hpp:115
Class representing a collection of spanning trees of a word graph.
Definition forest.hpp:42
Class for taking joins of word graphs.
Definition word-graph.hpp:2885
bool is_subrelation_no_checks(WordGraph< Node > const &x, WordGraph< Node > const &y)
Check if the language accepted by one word graph is contained in that defined by another word graph.
auto call_no_checks(WordGraph< Node > const &x, Args &&... args)
Returns a word graph containing the join/meet of two given word graphs.
auto operator()(WordGraph< Node > const &x, Args &&... args)
Returns a word graph containing the join/meet of two given word graphs.
Joiner(Joiner const &)
Default copy constructor.
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, Node xroot, WordGraph< Node > const &y, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
bool is_subrelation(WordGraph< Node1 > const &x, Node2 xroot, WordGraph< Node1 > const &y, Node2 yroot)
Check if the language accepted by one word graph is contained in that defined by another word graph.
Joiner(Joiner &&)
Default move constructor.
bool is_subrelation_no_checks(WordGraph< Node1 > const &x, Node2 xroot, WordGraph< Node1 > const &y, Node2 yroot)
Check if the language accepted by one word graph is contained in that defined by another word graph.
Joiner()
Default constructor.
Joiner & operator=(Joiner const &)
Default copy assignment operator.
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, WordGraph< Node > const &y)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Joiner & operator=(Joiner &&)
Default move assignment operator.
bool is_subrelation(WordGraph< Node > const &x, WordGraph< Node > const &y)
Check if the language accepted by one word graph is contained in that defined by another word graph.
void operator()(WordGraph< Node > &xy, WordGraph< Node > const &x, WordGraph< Node > const &y)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, size_t xnum_nodes_reachable_from_root, Node xroot, WordGraph< Node > const &y, size_t ynum_nodes_reachable_from_root, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
bool is_subrelation_no_checks(WordGraph< Node1 > const &x, size_t xnum_nodes_reachable_from_root, Node2 xroot, WordGraph< Node1 > const &y, size_t ynum_nodes_reachable_from_root, Node2 yroot)
Check if the language accepted by one word graph is contained in that accepted by another word graph.
void operator()(WordGraph< Node > &xy, WordGraph< Node > const &x, Node xroot, WordGraph< Node > const &y, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Class for taking meets of word graphs.
Definition word-graph.hpp:3316
bool is_subrelation_no_checks(WordGraph< Node > const &x, WordGraph< Node > const &y)
Check if the language accepted by one word graph is contained in that defined by another word graph.
auto call_no_checks(WordGraph< Node > const &x, Args &&... args)
Returns a word graph containing the join/meet of two given word graphs.
auto operator()(WordGraph< Node > const &x, Args &&... args)
Returns a word graph containing the join/meet of two given word graphs.
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, Node xroot, WordGraph< Node > const &y, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
bool is_subrelation(WordGraph< Node1 > const &x, Node2 xroot, WordGraph< Node1 > const &y, Node2 yroot)
Check if the language accepted by one word graph is contained in that defined by another word graph.
bool is_subrelation_no_checks(WordGraph< Node1 > const &x, Node2 xroot, WordGraph< Node1 > const &y, Node2 yroot)
Check if the language accepted by one word graph is contained in that defined by another word graph.
Meeter & operator=(Meeter const &)
Default copy assignment operator.
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, WordGraph< Node > const &y)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Meeter()
Default constructor.
Meeter & operator=(Meeter &&)
Default move assignment operator.
bool is_subrelation(WordGraph< Node > const &x, WordGraph< Node > const &y)
Check if the language accepted by one word graph is contained in that defined by another word graph.
void operator()(WordGraph< Node > &xy, WordGraph< Node > const &x, WordGraph< Node > const &y)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Meeter(Meeter &&)
Default move constructor.
void call_no_checks(WordGraph< Node > &xy, WordGraph< Node > const &x, size_t xnum_nodes_reachable_from_root, Node xroot, WordGraph< Node > const &y, size_t ynum_nodes_reachable_from_root, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Meeter(Meeter const &)
Default copy constructor.
bool is_subrelation_no_checks(WordGraph< Node1 > const &x, size_t xnum_nodes_reachable_from_root, Node2 xroot, WordGraph< Node1 > const &y, size_t ynum_nodes_reachable_from_root, Node2 yroot)
Check if the language accepted by one word graph is contained in that accepted by another word graph.
void operator()(WordGraph< Node > &xy, WordGraph< Node > const &x, Node xroot, WordGraph< Node > const &y, Node yroot)
Replace the contents of a word graph with the join/meet of two given word graphs with respect to give...
Class for representing word graphs.
Definition word-graph.hpp:82
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > adjacency_matrix_type
Definition word-graph.hpp:108
bool operator<(WordGraph const &that) const
Check if a word graph is less than another.
Definition word-graph.hpp:541
element_index_type node_type
Definition word-graph.hpp:98
auto nodes() const noexcept
Returns a range object containing all nodes in a word graph.
Definition word-graph.hpp:968
auto labels_and_targets(node_type source) const
Returns a range object containing pairs consisting of edge labels and target nodes.
const_iterator_nodes cend_nodes() const noexcept
Returns a random access iterator pointing one-past-the-last node of the word graph.
Definition word-graph.hpp:859
WordGraph & disjoint_union_inplace(WordGraph< Node > const &that)
Unites a word graph in-place.
std::pair< label_type, node_type > next_label_and_target_no_checks(node_type s, label_type a) const
Get the next target of an edge incident to a given node that doesn't equal UNDEFINED.
WordGraph & remove_label(label_type a)
Removes a given label from the word graph.
WordGraph & swap_targets(node_type m, node_type n, label_type a)
Swaps the edge with specified label from one node with another.
size_type number_of_edges() const
Returns the number of edges.
bool operator>(WordGraph const &that) const
Check if a word graph is greater than another.
Definition word-graph.hpp:581
const_iterator_targets cbegin_targets_no_checks(node_type source) const noexcept
Returns a random access iterator pointing at the target of the edge with label 0 incident to a given ...
Definition word-graph.hpp:907
element_index_type label_type
Definition word-graph.hpp:101
bool operator<=(WordGraph const &that) const
Check if a word graph is less than or equal to another.
Definition word-graph.hpp:561
auto labels_and_targets_no_checks(node_type source) const noexcept
Returns a range object containing pairs consisting of edge labels and target nodes.
Definition word-graph.hpp:1042
WordGraph & induced_subgraph(node_type first, node_type last)
Modify in-place to contain the subgraph induced by a range of nodes.
rx::iterator_range< const_iterator_targets > targets_no_checks(node_type source) const noexcept
Returns a range object containing all the targets of edges with a given source.
Definition word-graph.hpp:1004
WordGraph & remove_all_targets()
Remove all of the edges in the word graph.
Definition word-graph.hpp:438
const_iterator_targets cbegin_targets(node_type source) const
Returns a random access iterator pointing at the target of the edge with label 0 incident to a given ...
WordGraph & disjoint_union_inplace_no_checks(WordGraph< Node > const &that)
Unites a word graph in-place.
WordGraph & remove_target(node_type s, label_type a)
Remove an edge from a node with a given label.
WordGraph & swap_targets_no_checks(node_type m, node_type n, label_type a)
Swaps the edge with specified label from one node with another.
Definition word-graph.hpp:463
bool operator>=(WordGraph const &that) const
Check if a word graph is greater than or equal to another.
Definition word-graph.hpp:601
bool operator!=(WordGraph const &that) const
Check if two word graphs are inequal.
Definition word-graph.hpp:521
auto labels() const noexcept
Returns a range object containing all labels of edges in a word graph.
Definition word-graph.hpp:982
WordGraph & reserve(size_type m, size_type n)
Ensures that the word graph has capacity for a given number of nodes, and out-degree.
const_iterator_targets cend_targets_no_checks(node_type source) const noexcept
Returns a random access iterator pointing one beyond the target of the edge with label out_degree() -...
Definition word-graph.hpp:955
size_type out_degree() const noexcept
Definition word-graph.hpp:823
const_iterator_nodes cbegin_nodes() const noexcept
Returns a random access iterator pointing at the first node of the word graph.
Definition word-graph.hpp:841
bool operator==(WordGraph const &that) const
Check if two word graphs are equal.
Definition word-graph.hpp:501
typename detail::DynamicArray2< element_index_type >::const_iterator const_iterator_targets
Definition word-graph.hpp:123
rx::iterator_range< const_iterator_targets > targets(node_type source) const
Returns a range object containing all the targets of edges with a given source.
WordGraph & operator=(WordGraph const &)
Default copy assignment constructor.
WordGraph & target(node_type s, label_type a, node_type t)
Add an edge from one node to another with a given label.
WordGraph & add_nodes(size_type nr)
Add a number of new nodes.
WordGraph & target_no_checks(node_type s, label_type a, node_type t)
Add an edge from one node to another with a given label.
Definition word-graph.hpp:355
const_iterator_targets cend_targets(node_type source) const
Returns a random access iterator pointing one beyond the target of the edge with label out_degree() -...
WordGraph & init(size_type m=0, size_type n=0)
Re-initialize the word graph to have m nodes and out-degree n.
WordGraph & remove_target_no_checks(node_type s, label_type a)
Remove an edge from a node with a given label.
Definition word-graph.hpp:377
std::pair< label_type, node_type > next_label_and_target(node_type s, label_type a) const
Get the next target of an edge incident to a given node that doesn't equal UNDEFINED.
typename detail::IntRange< element_index_type >::const_reverse_iterator const_reverse_iterator_nodes
Definition word-graph.hpp:119
WordGraph & remove_label_no_checks(label_type a)
Removes a given label from the word graph.
WordGraph & add_to_out_degree(size_type nr)
Add to the out-degree of every node.
static WordGraph random(size_type number_of_nodes, size_type out_degree, std::mt19937 mt=std::mt19937(std::random_device()()))
Construct a random word graph from number of nodes and out-degree.
size_t hash_value() const
Returns a hash value.
Definition word-graph.hpp:616
size_type number_of_nodes() const noexcept
Definition word-graph.hpp:747
typename detail::IntRange< element_index_type >::const_iterator const_iterator_nodes
Definition word-graph.hpp:115
std::size_t size_type
Definition word-graph.hpp:104
WordGraph & induced_subgraph_no_checks(node_type first, node_type last)
Modify in-place to contain the subgraph induced by a range of nodes.
size_type number_of_edges_no_checks(node_type s) const
Returns the number of edges with given source node.
T cend(T... args)
T fill(T... args)
T forward(T... args)
std::string to_human_readable_repr(AhoCorasick const &ac)
Return a string representation.
std::ostringstream & operator<<(std::ostringstream &os, BMat8 const &x)
Insertion operator.
Undefined const UNDEFINED
Value for something undefined.
std::conditional_t< R==0||C==0, DynamicIntMat< Scalar >, StaticIntMat< R, C, Scalar > > IntMat
Alias template for integer matrices.
Definition matrix.hpp:4287
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:798
Order
The possible orderings of words and strings.
Definition order.hpp:48
@ shortlex
Definition order.hpp:54
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:101
std::string to_input_string(WordGraph< Node > const &wg, std::string const &prefix="", std::string const &braces="{}", std::string const &suffix="")
Return a string that can be used to recreate a word graph.
static constexpr bool IsWordGraph
Helper variable template.
Definition word-graph.hpp:2687
Namespace containing helper functions for the WordGraph class.
Definition word-graph.hpp:1221
auto adjacency_matrix(WordGraph< Node > const &wg)
Returns the adjacency matrix of a word graph.
void spanning_tree_no_checks(WordGraph< Node1 > const &wg, Node2 root, Forest &f)
Replace the contents of a Forest by a spanning tree of the nodes reachable from a given node in a wor...
void throw_if_any_target_out_of_bounds(WordGraph< Node > const &wg)
Throws if the target of any edge is out of bounds.
void throw_if_node_out_of_bounds(WordGraph< Node1 > const &wg, Node2 n)
Throws if a node is out of bounds.
std::pair< Node1, Iterator > last_node_on_path(WordGraph< Node1 > const &wg, Node2 source, Iterator first, Iterator last)
Returns the last node on the path labelled by a word and an iterator to the position in the word reac...
bool is_strictly_cyclic(WordGraph< Node > const &wg)
Check if every node is reachable from some node.
std::unordered_set< Node1 > nodes_reachable_from(WordGraph< Node1 > const &wg, Node2 source)
Returns the std::unordered_set of nodes reachable from a given node in a word graph.
bool is_connected(WordGraph< Node > const &wg)
Check if a word graph is connected.
bool equal_to_no_checks(WordGraph< Node > const &x, WordGraph< Node > const &y, Node first, Node last)
Compares two word graphs on a range of nodes.
bool equal_to(WordGraph< Node > const &x, WordGraph< Node > const &y, Node first, Node last)
Compares two word graphs on a range of nodes.
size_t number_of_nodes_reachable_from(WordGraph< Node1 > const &wg, Node2 source)
Returns the number of nodes reachable from a given node in a word graph.
Definition word-graph.hpp:2256
bool is_reachable(WordGraph< Node1 > const &wg, Node2 source, Node2 target)
Check if there is a path from one node to another.
bool is_compatible(WordGraph< Node > const &wg, Iterator1 first_node, Iterator2 last_node, Iterator3 first_rule, Iterator3 last_rule)
Check if a word graph is compatible with some relations at a range of nodes.
std::vector< Node > topological_sort(WordGraph< Node > const &wg)
Returns the nodes of the word graph in topological order (see below) if possible.
bool is_complete(WordGraph< Node > const &wg, Iterator1 first_node, Iterator2 last_node)
Check if every node in a range has exactly WordGraph::out_degree out-edges.
std::pair< Node1, Iterator > last_node_on_path_no_checks(WordGraph< Node1 > const &wg, Node2 source, Iterator first, Iterator last) noexcept
Returns the last node on the path labelled by a word and an iterator to the position in the word reac...
std::unordered_set< Node1 > nodes_reachable_from_no_checks(WordGraph< Node1 > const &wg, Node2 source)
Returns the std::unordered_set of nodes reachable from a given node in a word graph.
WordGraph< Node > random_acyclic(size_t number_of_nodes, size_t out_degree, std::mt19937 mt=std::mt19937(std::random_device()()))
Construct a random connected acyclic word graph with given number of nodes, and out-degree.
void throw_if_label_out_of_bounds(WordGraph< Node > const &wg, typename WordGraph< Node >::label_type a)
Throws if a label is out of bounds.
void add_cycle(WordGraph< Node > &wg, size_t N)
Adds a cycle consisting of N new nodes.
Definition word-graph.hpp:1271
bool is_compatible_no_checks(WordGraph< Node > const &wg, Iterator1 first_node, Iterator2 last_node, Iterator3 first_rule, Iterator3 last_rule)
Check if a word graph is compatible with some relations at a range of nodes.
bool is_reachable_no_checks(WordGraph< Node1 > const &wg, Node2 source, Node2 target)
Check if there is a path from one node to another.
bool standardize(Graph &wg, Forest &f, Order val)
Standardizes a word graph in-place.
size_t number_of_nodes_reachable_from_no_checks(WordGraph< Node1 > const &wg, Node2 source)
Returns the number of nodes reachable from a given node in a word graph.
Definition word-graph.hpp:2280
bool is_complete_no_checks(WordGraph< Node > const &wg, Iterator1 first_node, Iterator2 last_node)
Check if every node in a range has exactly WordGraph::out_degree out-edges.
Node1 follow_path_no_checks(WordGraph< Node1 > const &wg, Node2 from, Iterator first, Iterator last) noexcept
Follow the path from a specified node labelled by a word.
bool is_standardized(WordGraph< Node > const &wg, Order val=Order::shortlex)
Check if a word graph is standardized.
bool is_acyclic(WordGraph< Node > const &wg)
Check if a word graph is acyclic.
void spanning_tree(WordGraph< Node1 > const &wg, Node2 root, Forest &f)
Replace the contents of a Forest by a spanning tree of the nodes reachable from a given node in a wor...
Node1 follow_path(WordGraph< Node1 > const &wg, Node2 source, Iterator first, Iterator last)
Find the node that a path starting at a given node leads to (if any).
void add_cycle_no_checks(WordGraph< Node > &wg, Iterator first, Iterator last)
Adds a cycle involving the specified range of nodes to a word graph.
Dot dot(WordGraph< Node > const &wg)
Returns a Dot object representing a word graph.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T size(T... args)