-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathword-graph.cpp
More file actions
1804 lines (1445 loc) · 54.4 KB
/
word-graph.cpp
File metadata and controls
1804 lines (1445 loc) · 54.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// libsemigroups_pybind11
// Copyright (C) 2021-2024 James D. Mitchell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// C++ stl headers....
#include <array> // for array
#include <cstddef> // for uint32_t
#include <cstdint> // for uint64_t
#include <initializer_list> // for initializer_list
#include <iosfwd> // for string
#include <string> // for to_string, basic_string
#include <vector> // for vector
// libsemigroups....
#include <libsemigroups/config.hpp> // for LIBSEMIGROUPS_EIGEN_ENABLED
#include <libsemigroups/constants.hpp> // for operator!=, operator==
#include <libsemigroups/detail/int-range.hpp> // for IntegralRange<>::value_type
#include <libsemigroups/word-graph-helpers.hpp> // for WordGraph helpers
#include <libsemigroups/word-graph.hpp> // for WordGraph
// pybind11....
#include <pybind11/operators.h> // for self, self_t, operator!=, operator*
#include <pybind11/pybind11.h> // for class_, make_iterator, init, enum_
#include <pybind11/stl.h> // for conversion of C++ to py types
#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
#include <pybind11/eigen.h> // for adjacency_matrix
#endif
// libsemigroups_pybind11....
#include "main.hpp" // for init_word_graph
namespace py = pybind11;
namespace libsemigroups {
void init_word_graph(py::module& m) {
using WordGraph_ = WordGraph<uint32_t>;
using node_type = typename WordGraph_::node_type;
using label_type = typename WordGraph_::label_type;
py::class_<WordGraph_> thing(m,
"WordGraph",
R"pbdoc(
Class for representing word graphs.
Instances of this class represent word graphs. If the word graph has ``n``
nodes, they are represented by the numbers :math:`\{0, ..., n - 1\}`, and every
node has the same number ``m`` of out-edges (edges with source that node and
target any other node or :any:`UNDEFINED`). The number ``m`` is referred to as
the *out-degree* of the word graph, or any of its nodes.)pbdoc");
thing.def("__repr__", [](WordGraph_ const& self) {
return to_human_readable_repr(self);
});
thing.def("__str__", [](WordGraph_ const& self) {
return to_input_string(self, "WordGraph(", "[]", ")");
});
thing.def(py::self != py::self);
thing.def(py::self < py::self);
thing.def(py::self <= py::self);
thing.def(py::self == py::self);
thing.def(py::self > py::self);
thing.def(py::self >= py::self);
thing.def("__hash__", &WordGraph_::hash_value);
thing.def("__copy__", [](WordGraph_ const& wg) { return WordGraph_(wg); });
thing.def(
"copy",
[](WordGraph_ const& wg) { return WordGraph_(wg); },
R"pbdoc(
Copy a :any:`WordGraph` object.
:returns: A copy.
:rtype: WordGraph
)pbdoc");
thing.def(py::init<size_t, size_t>(),
py::arg("m") = 0,
py::arg("n") = 0,
R"pbdoc(
Construct from number of nodes and out degree.
This function constructs a word graph with *m* nodes and where the maximum
out-degree of any node is *n*. There are no edges in the defined word graph.
:param m: the number of nodes in the word graph (default: ``0``).
:type m: int
:param n: the out-degree of every node (default: ``0``).
:type n: int
:complexity:
:math:`O(mn)` where *m* is the number of nodes, and *n* is the
out-degree of the word graph.)pbdoc");
thing.def(py::init([](size_t num_nodes,
std::vector<std::vector<node_type>> const& targets) {
return make<WordGraph_>(num_nodes, targets);
}),
py::arg("num_nodes"),
py::arg("targets"),
R"pbdoc(
Construct a word graph from a number of nodes and an list of targets.
This function constructs a word graph from its arguments whose
out-degree is specified by the length of the first item in *targets*.
:param num_nodes: the number of nodes in the word graph.
:type num_nodes: int
:param targets: list of the targets.
:type targets: List[List[int]]
:raises LibsemigroupsError: if any target is specified in *targets* is greater
than or equal to *num_nodes*.
.. doctest::
>>> from libsemigroups_pybind11 import WordGraph
>>> WordGraph(5, [[0, 0], [1, 1], [2], [3, 3]])
<WordGraph with 5 nodes, 7 edges, & out-degree 2>
)pbdoc");
thing.def("add_nodes",
&WordGraph_::add_nodes,
py::arg("nr"),
R"pbdoc(
Add a number of new nodes.
This function modifies a word graph in-place so that it has *nr* new nodes
added.
:param nr: the number of nodes to add.
:type nr: int
:returns: ``self``.
:rtype: WordGraph
:complexity: Linear in ``(number_of_nodes() + nr) * out_degree()``.)pbdoc");
thing.def("add_to_out_degree",
&WordGraph_::add_to_out_degree,
py::arg("nr"),
R"pbdoc(
Add to the out-degree of every node.
This function modifies a word graph in-place so that the out-degree is
increased by *nr*.
:param nr: the number of new out-edges for every node.
:type nr: int
:returns: ``self``.
:rtype: WordGraph
:complexity: :math:`O(mn)` where ``m`` is the number of nodes, and ``n`` is
the new out degree of the word graph.
)pbdoc");
thing.def(
"nodes",
[](WordGraph_ const& self) {
return py::make_iterator(self.cbegin_nodes(), self.cend_nodes());
},
R"pbdoc(
Returns an iterator yielding the nodes of the word graph.
This function returns an iterator yielding the nodes of
the word graph.
:returns:
An :any:`Iterator`.
:rtype:
Iterator
:complexity:
Constant.)pbdoc");
thing.def(
"targets",
[](WordGraph_ const& self, node_type source) {
return py::make_iterator(self.cbegin_targets(source),
self.cend_targets(source));
},
py::arg("source"),
R"pbdoc(
Returns an iterator yielding the targets of the edges incident to a given
node.
This function returns an iterator yielding the targets of the edges incident
to the source node *source*. This target might equal :any:`UNDEFINED`.
:param source: the source node in the word graph.
:type source: int
:returns: An :any:`Iterator`.
:rtype: Iterator
:raises LibsemigroupsError:
if *source* is out of range (i.e. greater than or equal to
:any:`number_of_nodes`).
:complexity: Constant.
)pbdoc");
thing.def("disjoint_union_inplace",
&WordGraph_::disjoint_union_inplace,
py::arg("that"),
R"pbdoc(
Unites a word graph in-place.
This function changes a :any:`WordGraph` object in-place to contain the
disjoint union of itself and *that*. The node ``n`` of *that* is mapped to
``number_of_nodes() + n``.
:param that: the word graph to unite.
:type that: WordGraph
:returns: ``self``.
:rtype: WordGraph
:raises LibsemigroupsError:
if ``self`` and *that* do not have the same out-degree.
)pbdoc");
thing.def(
"induced_subgraph",
[](WordGraph_& self, node_type first, node_type last) {
return self.induced_subgraph(first, last);
},
py::arg("first"),
py::arg("last"),
R"pbdoc(
Modify in-place to contain the subgraph induced by a range of nodes.
This function modifies a :any:`WordGraph` object in-place to contain its
subgraph induced by the range of nodes *first* to *last*.
:param first: the first node.
:type first: int
:param last: one more than the last node.
:type last: int
:returns: ``self``.
:rtype: WordGraph
:raises LibsemigroupsError: if *first* or *last* is out of range.
:raises LibsemigroupsError:
if any edge with source in the range *first* to *last* has target outside
the range *first* to *last*.
)pbdoc");
thing.def(
"init",
[](WordGraph_& self, size_t m, size_t n) { return self.init(m, n); },
py::arg("m"),
py::arg("n"),
R"pbdoc(
Re-initialize the word graph to have *m* nodes and out-degree *n*.
This function puts a word graph into the state that it would have been in if it
had just been newly constructed with the same parameters *m* and *n*.
:param m: the number of nodes in the word graph.
:type m: int
:param n: the out-degree of every node.
:type n: int
:returns: ``self``.
:rtype: WordGraph
:complexity:
:math:`O(mn)` where :math:`m` is the number of nodes, and :math:`n` is the
out-degree of the word graph.)pbdoc");
thing.def(
"labels_and_targets",
[](WordGraph_ const& self, node_type source) {
auto r = self.labels_and_targets(source);
return py::make_iterator(rx::begin(r), rx::end(r));
},
py::arg("source"),
R"pbdoc(
Returns an iterator yielding pairs consisting of edge labels and
target nodes.
This function returns an iterator yielding all the edge labels and
targets of edges with source *source*.
:param source: the source node.
:type source: int
:returns: An iterator.
:rtype: Iterator
:raises LibsemigroupsError: if *source* is out of bounds.)pbdoc");
thing.def("next_label_and_target",
&WordGraph_::next_label_and_target,
py::arg("s"),
py::arg("a"),
R"pbdoc(
Get the next target of an edge incident to a given node that doesn't equal
:any:`UNDEFINED`.
This function returns the next target of an edge with label greater than or
equal to *a* that is incident to the node *s*. If ``target(s, b)`` equals
:any:`UNDEFINED` for every value ``b`` in the range :math:`[a, n)`, where
:math:`n` is the return value of :any:`out_degree()` then ``x.first`` and
``x.second`` equal :any:`UNDEFINED`.
:param s: the node.
:type s: int
:param a: the label.
:type a: int
:returns:
Returns a pair where the first entry is the next label after *a* and the
second is the next target of *s* that is not :any:`UNDEFINED`.
:rtype: Tuple[int,int]
:complexity: At worst :math:`O(n)` where :math:`n` equals :any:`out_degree()`.
:raises LibsemigroupsError:
if *s* does not represent a node in ``self``, or *a* is not a valid edge
label.)pbdoc");
thing.def(
"number_of_edges",
[](WordGraph_ const& self) { return self.number_of_edges(); },
R"pbdoc(
Returns the number of edges. This function returns the total number of edges
(i.e. values ``s`` and ``a`` such that ``target(s, a)`` is not
:any:`UNDEFINED`) in the word graph.
:returns:
The total number of edges.
:rtype:
int
:complexity:
:math:`O(mn)` where ``m`` is :any:`number_of_nodes()` and ``n`` is
:any:`out_degree()`.)pbdoc");
thing.def(
"number_of_edges",
[](WordGraph_ const& self, node_type s) {
return self.number_of_edges(s);
},
py::arg("s"),
R"pbdoc(
Returns the number of edges with given source node.
This function returns the number of edges incident to the given source node
*s*.
:param s: the node.
:type s: int
:returns: The number of edge incident to *s*.
:rtype: int
:raises LibsemigroupsError: if *s* is not a node.
:complexity: :math:`O(n)` where ``n`` is :any:`out_degree()`.)pbdoc");
thing.def("number_of_nodes",
&WordGraph_::number_of_nodes,
R"pbdoc(
Returns the number of nodes. This function returns the number of nodes
in the word graph.
:returns:
The number of nodes in the word graph.
:rtype:
int
:complexity:
Constant.)pbdoc");
thing.def("out_degree",
&WordGraph_::out_degree,
R"pbdoc(
Returns the out-degree. This function returns the number of edge labels
in the word graph.
:returns:
The number of edge labels.
:rtype:
int
:complexity:
Constant.)pbdoc");
thing.def("remove_all_targets",
&WordGraph_::remove_all_targets,
R"pbdoc(
Remove all of the edges in the word graph. Set every target of every
source with every possible label to :any:`UNDEFINED`.
:returns: ``self``.
:rtype: WordGraph
:complexity:
:math:`O(mn)` where ``m`` is the number of nodes and ``n`` is the
out-degree.
)pbdoc");
thing.def("remove_label",
&WordGraph_::remove_label,
py::arg("a"),
R"pbdoc(
Removes a given label from the word graph.
This function removes the label *a* from a :any:`WordGraph` object in-place.
This reduces the out-degree by ``1``.
:param a: the label to remove.
:type a: int
:returns: ``self``.
:rtype: WordGraph
:raises LibsemigroupsError: if *a* is out of range.
)pbdoc");
thing.def("remove_target",
&WordGraph_::remove_target,
py::arg("s"),
py::arg("a"),
R"pbdoc(
Remove an edge from a node with a given label.
This function removes the edge with source node *s* labelled by *a*.
:param s: the source node.
:type s: int
:param a: the label of the edge from s.
:type a: int
:returns: ``self``.
:rtype: WordGraph
:complexity: Constant.
:raises LibsemigroupsError: if *s* or *a* is out of range.)pbdoc");
thing.def("reserve",
&WordGraph_::reserve,
py::arg("m"),
py::arg("n"),
R"pbdoc(
Ensures that the word graph has capacity for a given number of nodes, and
out-degree.
This function ensures that the word graph has capacity for *m* nodes and
*n* labels.
:param m: the number of nodes.
:type m: int
:param n: the out-degree.
:type n: int
:returns: ``self``.
:rtype: WordGraph
:complexity: :math:`O(mn)` where ``m`` is the number of nodes and ``n`` is the
out-degree.)pbdoc");
thing.def("swap_targets",
&WordGraph_::swap_targets,
py::arg("m"),
py::arg("n"),
py::arg("a"),
R"pbdoc(
Swaps the edge with specified label from one node with another.
This function swaps the target of the edge from the node *m* labelled *a*
with the target of the edge from the node *n* labelled *a*.
:param m: the first node.
:type m: int
:param n: the second node.
:type n: int
:param a: the label.
:type a: int
:returns: ``self``.
:rtype: WordGraph
:complexity: Constant
:raises LibsemigroupsError: if any of *m* , *n* , and *a* is out of
range.)pbdoc");
thing.def(
"target",
[](WordGraph_& self, node_type s, label_type a, node_type t) {
return self.target(s, a, t);
},
py::arg("s"),
py::arg("a"),
py::arg("t"),
R"pbdoc(
Add an edge from one node to another with a given label.
If *s* and *t* are nodes in ``self`` , and *a* is in the range ``[0,
out_degree())`` , then this function adds an edge from *a* to *b* labelled *a*.
:param s: the source node.
:type s: int
:param a: the label of the edge.
:type a: int
:param t: the range node.
:type t: int
:returns: ``self``.
:rtype: WordGraph
:raises LibsemigroupsError: if *s* , *a* , or *t* is not valid.
:complexity: Constant.)pbdoc");
thing.def(
"target",
[](WordGraph_ const& self, node_type source, label_type a) {
return self.target(source, a);
},
py::arg("source"),
py::arg("a"),
R"pbdoc(
Get the target of the edge with given source node and label.
This function returns the target of the edge with source node *source* and
label *a*.
:param source: the node.
:type source: int
:param a: the label.
:type a: int
:returns:
Returns the node adjacent to *source* via the edge labelled *a* , or
:any:`UNDEFINED`.
:rtype: int
:raises LibsemigroupsError: if *source* or *a* is not valid.
:complexity: Constant.)pbdoc");
thing.def_static(
"random",
[](size_t number_of_nodes, size_t out_degree) {
return WordGraph_::random(number_of_nodes, out_degree);
},
py::arg("number_of_nodes"),
py::arg("out_degree"),
R"pbdoc(
Construct a random word graph from number of nodes and out-degree.
This function constructs a random word graph with *number_of_nodes* nodes and
out-degree *out_degree*.
:param number_of_nodes: the number of nodes.
:type number_of_nodes: int
:param out_degree: the out-degree of every node.
:type out_degree: int
:returns: A random word graph.
:rtype: WordGraph
:raises LibsemigroupsError: if *number_of_nodes* is less than ``2``
:raises LibsemigroupsError: if *out_degree* is less than ``2``
:complexity: :math:`O(mn)` where ``m`` is the number of nodes, and ``n`` is
the out-degree of the word graph.)pbdoc");
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
m.def(
"add_cycle",
[](WordGraph_& wg, size_t N) { return word_graph::add_cycle(wg, N); },
py::arg("wg"),
py::arg("N"),
R"pbdoc(
Adds a cycle consisting of *N* new nodes.
:param wg:
the WordGraph object to add a cycle to.
:type wg:
WordGraph
:param N:
the length of the cycle and number of new nodes to add.
:type N:
int
:complexity:
:math:`O(N)` where :math:`N` is the second parameter.
)pbdoc");
m.def(
"adjacency_matrix",
[](WordGraph_ const& wg) { return word_graph::adjacency_matrix(wg); },
py::arg("wg"),
R"pbdoc(
:sig=(wg: WordGraph) -> numpy.ndarray[numpy.float64[m, n]] | Matrix:
Returns the adjacency matrix of a word graph.
This function returns the adjacency matrix of the word graph *wg*. The
type of the returned matrix depends on whether or not ``libsemigroups`` is
compiled with `eigen <http://eigen.tuxfamily.org/>`_ enabled. The returned
matrix has the number of edges with source ``s`` and target ``t`` in the
``(s, t)``-entry.
:param wg: the word graph.
:type wg: WordGraph
:returns: The adjacency matrix.
:rtype: numpy.ndarray | Matrix
)pbdoc");
m.def(
"word_graph_dot",
[](WordGraph_ const& wg) { return word_graph::dot(wg); },
py::arg("wg"),
R"pbdoc(
:sig=(wg: WordGraph) -> Dot:
Returns a :any:`Dot` object representing a word graph.
This function returns a :any:`Dot` object representing the word graph *wg*.
:param wg: the word graph.
:type wg: WordGraph
:returns: A :any:`Dot` object.
:rtype: Dot
)pbdoc");
m.def(
"equal_to",
[](WordGraph_ const& x,
WordGraph_ const& y,
node_type first,
node_type last) { return word_graph::equal_to(x, y, first, last); },
py::arg("x"),
py::arg("y"),
py::arg("first"),
py::arg("last"),
R"pbdoc(
Compares two word graphs on a range of nodes.
This function returns ``True`` if the word graphs *x* and *y* are equal
on the range of nodes from *first* to *last* ; and ``False`` otherwise.
The word graphs *x* and *y* are equal at a node *s* if:
* the out-degrees of *x* and *y* coincide;
* the edges with source ``s`` and label ``a`` have equal targets in *x*
and *y* for every label ``a``.
:param x: the first word graph for comparison.
:type x: WordGraph
:param y: the second word graph for comparison.
:type y: WordGraph
:param first: the first node in the range.
:type first: int
:param last: the last node in the range plus 1.
:type last: int
:returns:
Whether or not the word graphs are equal at the specified range of nodes.
:rtype: bool
:raises LibsemigroupsError:
if *first* is not a node in *x* or not a node in *y* ; or if ``last - 1``
is not a node in *x* or not a node in *y*.
.. note::
It is also possible to compare two entire word graphs using ``==``.)pbdoc");
m.def(
"follow_path",
[](WordGraph_ const& wg, node_type from, word_type const& path) {
return word_graph::follow_path(wg, from, path);
},
py::arg("wg"),
py::arg("from"),
py::arg("path"),
R"pbdoc(
Find the node that a path starting at a given node leads to (if any).
This function attempts to follow the path in the word graph *wg* starting
at the node *from* labelled by the word *path*. If this path exists,
then the last node on that path is returned. If this path does not exist,
then :any:`UNDEFINED` is returned.
:param wg: a word graph.
:type wg: WordGraph
:param from: the starting node.
:type from: int
:param path: the path to follow.
:type path: word_type
:returns: The last node on the path or :any:`UNDEFINED`.
:rtype: int | UNDEFINED
:raises LibsemigroupsError:
if *from* is not a node in the word graph or *path* contains a value that
is not an edge-label.
:complexity: Linear in the length of *path*.)pbdoc");
m.def(
"is_acyclic",
[](WordGraph_ const& wg) { return word_graph::is_acyclic(wg); },
py::arg("wg"),
R"pbdoc(
Check if a word graph is acyclic.
This function returns ``True`` if the word graph *wg* is acyclic and
``False`` otherwise. A word graph is acyclic if every directed cycle in the
word graph is trivial.
:param wg: the WordGraph object to check.
:type wg: WordGraph
:returns: Whether or not the word graph is acyclic.
:rtype: bool
:complexity:
:math:`O(m + n)` where :math:`m` is the number of nodes in the
:any:`WordGraph` *wg* and :math:`n` is the number of edges. Note that for
:any:`WordGraph` objects the number of edges is always at most :math:`mk`
where :math:`k` is the :any:`WordGraph.out_degree`.
.. doctest::
>>> from libsemigroups_pybind11 import WordGraph, word_graph
>>> wg = WordGraph()
>>> wg.add_nodes(2)
<WordGraph with 2 nodes, 0 edges, & out-degree 0>
>>> wg.add_to_out_degree(1)
<WordGraph with 2 nodes, 0 edges, & out-degree 1>
>>> wg.target(0, 0, 1)
<WordGraph with 2 nodes, 1 edges, & out-degree 1>
>>> wg.target(1, 0, 0)
<WordGraph with 2 nodes, 2 edges, & out-degree 1>
>>> word_graph.is_acyclic(wg)
False)pbdoc");
m.def(
"is_acyclic",
[](WordGraph_ const& wg, node_type source) {
return word_graph::is_acyclic(wg, source);
},
py::arg("wg"),
py::arg("source"),
R"pbdoc(
Check if the word graph induced by the nodes reachable from a source node is
acyclic.
This function returns ``True`` if the word graph consisting of the nodes
reachable from *source* in the word graph *wg* is acyclic and ``False``
if not. A word graph is *acyclic* if every directed cycle in the word graph is
trivial.
:param wg: the WordGraph object to check.
:type wg: WordGraph
:param source: the source node.
:type source: int
:returns:
Whether the induced subgraph of *wg* consisting of those nodes
reachable from *source* is acyclic or not.
:rtype: bool
:complexity:
:math:`O(m + n)` where :math:`m` is the number of nodes in the
:any:`WordGraph` *wg* and :math:`n` is the number of edges. Note that for
:any:`WordGraph` objects the number of edges is always at most :math:`mk`
where :math:`k` is the :any:`WordGraph.out_degree`.
.. doctest::
>>> from libsemigroups_pybind11 import WordGraph, word_graph
>>> wg = WordGraph()
>>> wg.add_nodes(4).add_to_out_degree(1)
<WordGraph with 4 nodes, 0 edges, & out-degree 1>
>>> wg.target(0, 0, 1).target(1, 0, 0).target(2, 0, 3)
<WordGraph with 4 nodes, 3 edges, & out-degree 1>
>>> word_graph.is_acyclic(wg)
True
>>> word_graph.is_acyclic(wg, 0)
True
>>> word_graph.is_acyclic(wg, 1)
True
>>> word_graph.is_acyclic(wg, 2)
True
>>> word_graph.is_acyclic(wg, 3)
True)pbdoc");
m.def(
"is_acyclic",
[](WordGraph_ const& wg, node_type source, node_type target) {
return word_graph::is_acyclic(wg, source, target);
},
py::arg("wg"),
py::arg("source"),
py::arg("target"),
R"pbdoc(
Check if the word graph induced by the nodes reachable from a source node and
from which a target node can be reached is acyclic.
This function returns ``True`` if the word graph consisting of the nodes
reachable from *source* and from which *target* is reachable, in the word
graph *wg*, is acyclic; and ``False`` if not. A word graph is *acyclic* if
every directed cycle of the word graph is trivial.
:param wg: the WordGraph object to check.
:type wg: WordGraph
:param source: the source node.
:type source: int
:param target: the target node.
:type target: int
:returns: Whether or not the subgraph is acyclic.
:rtype: bool
:complexity:
:math:`O(m + n)` where :math:`m` is the number of nodes in the
:any:`WordGraph` *wg* and :math:`n` is the number of edges. Note that for
:any:`WordGraph` objects the number of edges is always at most :math:`mk`
where :math:`k` is the :any:`WordGraph.out_degree`.)pbdoc");
m.def(
"is_compatible",
[](WordGraph_ const& wg,
node_type first_node,
node_type last_node,
word_type const& lhs,
word_type const& rhs) {
return word_graph::is_compatible(wg,
wg.cbegin_nodes() + first_node,
wg.cbegin_nodes() + last_node,
lhs,
rhs);
},
py::arg("wg"),
py::arg("first_node"),
py::arg("last_node"),
py::arg("lhs"),
py::arg("rhs"),
R"pbdoc(
Check if a word graph is compatible with some relations at a range of nodes.
This function returns ``True`` if the word graph *wg* is compatible with the
word *lhs* and *rhs* with source node equal to every node in the range from
*first_node* to *last_node*. This means that the paths with given sources that
are labelled by *lhs* lead to the same nodes as the paths labelled by *rhs*.
:param wg: the word graph.
:type wg: WordGraph
:param first_node: the first node.
:type first_node: int
:param last_node: one more than the last node.
:type last_node: int
:param lhs: the first rule.
:type lhs: List[int]
:param rhs: the second rule.
:type rhs: List[int]
:returns:
Whether or not the word graph is compatible with the given rules at each one
of the given nodes.
:rtype: bool
:raises LibsemigroupsError:
if any of the nodes in the range between *first_node* and *last_node*
does not belong to *wg* (i.e. is greater than or equal to
:any:`WordGraph.number_of_nodes`).
:raises LibsemigroupsError:
if *lhs* or *rhs* contains an invalid label (i.e. one greater than or equal
to :any:`WordGraph.out_degree`).)pbdoc");
m.def(
"is_complete",
[](WordGraph_ const& wg) { return word_graph::is_complete(wg); },
py::arg("wg"),
R"pbdoc(
Check if every node has exactly WordGraph::out_degree out-edges.
This function returns ``True`` if the word graph *wg* is complete, meaning that
every node is the source of an edge with every possible label.
:param wg: the word graph.
:type wg: WordGraph
:returns: Whether or not the word graph is complete.
:rtype: bool
:complexity:
:math:`O(mn)` where ``m`` is :any:`WordGraph.number_of_nodes` and ``n`` is
:any:`WordGraph.out_degree`.)pbdoc");
m.def(
"is_complete",
[](WordGraph_ const& wg, node_type first_node, node_type last_node) {
return word_graph::is_complete(wg,
wg.cbegin_nodes() + first_node,
wg.cbegin_nodes() + last_node);
},
py::arg("wg"),
py::arg("first_node"),
py::arg("last_node"),
R"pbdoc(
Check if every node in a range has exactly WordGraph::out_degree out-edges.
This function returns ``True`` if every node in the range defined by
*first_node* and *last_node* is complete, meaning that every such node is
the source of an edge with every possible label.
:param wg: the word graph.
:type wg: WordGraph
:param first_node: the first node.
:type first_node: int
:param last_node: one more than the last node.
:type last_node: int
:returns:
Whether or not the word graph is complete on the given range of
nodes.
:rtype: bool
:complexity:
:math:`O(mn)` where ``m`` is the number of nodes in the range and ``n`` is
:any:`WordGraph.out_degree`.
:raises LibsemigroupsError:
if any node in the range defined by *first_node* and *last_node* is not a
node of *wg*.)pbdoc");
m.def(
"is_connected",
[](WordGraph_& wg) { return word_graph::is_connected(wg); },
py::arg("wg"),
R"pbdoc(
Check if a word graph is connected.
This function returns ``True`` if the word graph *wg* is connected and
``False`` if it is not. A word graph is *connected* if for every pair of
nodes ``s`` and ``t`` in the graph there exists a sequence :math:`u_0 = s,
\ldots, u_{n}= t` for some :math:`n\in \mathbb{N}` such that for every
:math:`i` there exists a label ``a`` such that :math:`(u_i, a, u_{i + 1})` or
:math:`(u_{i + 1}, a, u_i)` is an edge in the graph.
:param wg: the word graph.
:type wg: WordGraph
:returns: Whether or not the word graph is connected.
:rtype: bool
:raises LibsemigroupsError:
if any target in *wg* is out of bounds, i.e. if any target ``t`` is not
equal to :any:`UNDEFINED` and not in the nodes of *wg*.)pbdoc");
m.def(
"is_reachable",
[](WordGraph_ const& wg, node_type source, node_type target) {
return word_graph::is_reachable(wg, source, target);
},
py::arg("wg"),