Skip to content

Commit 18b5492

Browse files
action-digraph-helper: add follow_path
1 parent 75175a7 commit 18b5492

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

docs/source/api/digraph-helper.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ This page contains the documentation for helper function for the class
1616
is_acyclic
1717
add_cycle
1818
topological_sort
19+
follow_path
1920

2021
.. autofunction:: is_acyclic
2122
.. autofunction:: add_cycle
2223
.. autofunction:: topological_sort
24+
.. autofunction:: follow_path

libsemigroups_pybind11/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
wislo,
3434
number_of_words,
3535
tril,
36+
follow_path,
3637
)
3738

3839
from .transf import Transf

src/action-digraph.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,5 +799,28 @@ namespace libsemigroups {
799799
- **source** (int) the source node.
800800
:Returns: A list of ``int``.
801801
)pbdoc");
802+
m.def("follow_path",
803+
&action_digraph_helper::follow_path<size_t>,
804+
py::arg("ad"),
805+
py::arg("source"),
806+
py::arg("path"),
807+
R"pbdoc(
808+
Find the node that a path starting at a given node leads to.
809+
810+
:param ad: the ``ActionDigraph`` object to check.
811+
:type: ActionDigraph
812+
:param first: the starting node.
813+
:type: int
814+
:param path: the path to follow.
815+
:type: List[int]
816+
817+
:return:
818+
An ``int`` corresponding to the node at the end of the path or
819+
:py:class:`UNDEFINED` otherwise.
820+
821+
:raises RuntimeError:
822+
if ``source`` is not a node in the digraph or ``path`` contains a
823+
value that is not an edge-label.
824+
)pbdoc");
802825
}
803826
} // namespace libsemigroups

tests/test_actiondigraph.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
topological_sort,
2323
UNDEFINED,
2424
POSITIVE_INFINITY,
25+
follow_path,
2526
)
2627

2728

@@ -835,3 +836,17 @@ def test_010(self):
835836
UNDEFINED,
836837
],
837838
)
839+
840+
def test_follow_path(self):
841+
ad = binary_tree(8)
842+
self.assertEqual(ad.number_of_nodes(), 255)
843+
self.assertEqual(follow_path(ad, 0, [0, 1, 0, 1]), 20)
844+
self.assertEqual(follow_path(ad, 0, []), 0)
845+
for i, w in enumerate(ad.pislo_iterator(0, 0, 10)):
846+
self.assertEqual(follow_path(ad, 0, w), i)
847+
self.assertEqual(follow_path(ad, 1, [0] * 10), UNDEFINED)
848+
849+
with self.assertRaises(RuntimeError):
850+
follow_path(ad, 1, [2])
851+
with self.assertRaises(RuntimeError):
852+
follow_path(ad, 312, [0, 1, 1, 0])

0 commit comments

Comments
 (0)