Forest helpers
This page contains the documentation for various helper classes and functions
for manipulating Forest objects. All such classes and functions
are in the submodule forest.
Contents
Range object for iterating through paths in a |
|
Range object for iterating through paths in a |
|
|
Returns the depth of a node in the forest, i.e. the distance, in terms of the number of edges, from a root. |
|
Overloaded function. |
|
Check if a node is the root of any tree in the |
|
Returns the maximum label of any edge in a |
Returns a word containing the labels of the edges on the path from a root node to n. |
|
|
Returns a list containing the labels of the edges on the path from node n to a root node. |
Full API
This page contains the documentation for various helper classes and functions
for manipulating Forest objects. All such classes and functions are
contained in the submodule forest.
- class forest.PathsFromRoots
Range object for iterating through paths in a
Forest.This class represents a range object that facilitates iterating through the paths from every node to the root of its subtree in a
Forestobject. These nodes are taken in numerical order, so the first value returned bygetis the word to a root from node0, afternextis called,getreturns the word to a root from node1, and so on. The point of this class is to permit more efficient iteration over many paths in aForestobject thanpath_from_root(and its associated helper functions).path_from_roottraverses theForestfrom the given node to the root of its tree. If the path from nodesmandnto the root of their tree share a long common suffix, then this suffix will be traversed twice in two calls topath_from_root.PathsFromRootsavoids this by finding the least common ancestor ofmandn, so that the suffix is not re-traversed. This works best when the nodes in theForestare specified in a sensible order (such as via a depth-first or breadth-first traversal).- at_end(self: PathsFromRoots) bool
Check if the range is exhausted.
This function returns
Trueif there are no more paths in the range, andFalseotherwise.- Returns:
Whether or not the range is exhausted.
- Return type:
- copy(self: PathsFromRoots) PathsFromRoots
Copy a
PathsFromRootsobject.- Returns:
A copy.
- Return type:
- count(self: PathsFromRoots) int
Get the size of the range.
This function returns the number of paths in the range.
- Returns:
the number of paths in the range.
- Return type:
- forest(self: PathsFromRoots) Forest
Returns the underlying Forest object.
This function returns the
Forestobject used to constructor or initialise self.
- get(self: PathsFromRoots) list[int]
Returns a reference to the current path.
This function returns a const reference to the current path from the root of the tree containing
targettotarget.
- init(self: PathsFromRoots, f: Forest) PathsFromRoots
Re-initialize from a
Forest.This function re-initializes self so that its underlying
Forestobject is f. This puts self back into the same state it would have been in if it had been newly constructed from f.- Parameters:
f (Forest) – the Forest.
- Returns:
self- Return type:
- next(self: PathsFromRoots) None
Advance to the next path in the range.
This function advances to the current path in the range. If
at_endreturnsTrue, then this function does nothing.- Returns:
None- Return type:
None
- skip_n(self: PathsFromRoots, n: int) PathsFromRoots
Skip a number of paths in the range.
This function skips n paths in the range. If n is
1, then this is the same as callingnext. If n is0, then this function does nothing.- Parameters:
n (int) – the number of paths to skip.
- Returns:
self.- Return type:
- target(self: PathsFromRoots) int
Get the current target of the path.
This function returns the current target of the path returned by
get.- Returns:
The target of the current path.
- Return type:
- class forest.PathsToRoots
Range object for iterating through paths in a
Forest.This class represents a range object that facilitates iterating through the paths from every node to the root of its subtree in a
Forestobject. These nodes are taken in numerical order, so the first value returned bygetis the word to a root from node0, afternextis called,getreturns the word to a root from node1, and so on. The point of this class is to permit more efficient iteration over many paths in aForestobject thanpath_to_root(and its associated helper functions).path_to_roottraverses theForestfrom the given node to the root of its tree. If the path from nodesmandnto the root of their tree share a long common suffix, then this suffix will be traversed twice in two calls topath_to_root.PathsToRootsavoids this by finding the least common ancestor ofmandn, so that the suffix is not re-traversed. This works best when the nodes in theForestare specified in a sensible order (such as via a depth-first or breadth-first traversal).- at_end(self: PathsToRoots) bool
Check if the range is exhausted.
This function returns
Trueif there are no more paths in the range, andFalseotherwise.- Returns:
Whether or not the range is exhausted.
- Return type:
- copy(self: PathsToRoots) PathsToRoots
Copy a
PathsToRootsobject.- Returns:
A copy.
- Return type:
- count(self: PathsToRoots) int
Get the size of the range.
This function returns the number of paths in the range.
- Returns:
the number of paths in the range.
- Return type:
- forest(self: PathsToRoots) Forest
Returns the underlying Forest object.
This function returns the
Forestobject used to constructor or initialise self.
- get(self: PathsToRoots) list[int]
Returns a reference to the current path.
This function returns a const reference to the current path from the root of the tree containing
targettotarget.
- init(self: PathsToRoots, f: Forest) PathsToRoots
Re-initialize from a
Forest.This function re-initializes self so that its underlying
Forestobject is f. This puts self back into the same state it would have been in if it had been newly constructed from f.- Parameters:
f (Forest) – the Forest.
- Returns:
self- Return type:
- next(self: PathsToRoots) None
Advance to the next path in the range.
This function advances to the current path in the range. If
at_endreturnsTrue, then this function does nothing.- Returns:
None- Return type:
None
- skip_n(self: PathsToRoots, n: int) PathsToRoots
Skip a number of paths in the range.
This function skips n paths in the range. If n is
1, then this is the same as callingnext. If n is0, then this function does nothing.- Parameters:
n (int) – the number of paths to skip.
- Returns:
self.- Return type:
- target(self: PathsToRoots) int
Get the current target of the path.
This function returns the current target of the path returned by
get.- Returns:
The target of the current path.
- Return type:
- forest.depth(f: Forest, n: int) int
Returns the depth of a node in the forest, i.e. the distance, in terms of the number of edges, from a root.
This function returns the length of the word returned by
path_to_rootandpath_from_root.- Parameters:
- Returns:
The depth of n.
- Return type:
- Raises:
LibsemigroupsError – if n is out of bounds (i.e. it is greater than or equal to
Forest.number_of_nodes).
- forest.dot(f: Forest, labels: list[str]) Dot
Overloaded function.
- forest.dot(f: Forest) Dot
Returns a
Dotobject representing a Forest.This function returns a
Dotobject representing theForestf.
- forest.dot(f: Forest, labels: list[str]) Dot
Returns a
Dotobject representing a Forest.This function returns a
Dotobject representing theForestf. If labels is not empty, then each node is labelled with the path from that node to the root of its tree with each letter replaced by the string in the corresponding position of labels. If labels is empty, then the nodes are not labelled by their paths.
- forest.is_root(f: Forest, n: int) bool
Check if a node is the root of any tree in the
Forest.This function returns
Trueif the node n in theForestf is a root node, andFalseif it is not.- Parameters:
- Returns:
Whether or not n is a root of f.
- Return type:
- Raises:
LibsemigroupsError – if n is out of bounds (i.e. it is greater than or equal to
Forest.number_of_nodes).
- forest.max_label(f: Forest) int | Undefined
Returns the maximum label of any edge in a
Forest.This function returns the maximum label of any edge in the
Forestf orUNDEFINEDif there are no edges.
- forest.path_from_root(f: Forest, n: int) list[int]
Returns a word containing the labels of the edges on the path from a root node to n.
This function returns a word containing the labels of the edges on the path from a root node to the node n.
- Parameters:
- Returns:
The word labelling the path from a root node to n.
- Return type:
See also
- forest.path_to_root(f: Forest, n: int) list[int]
Returns a list containing the labels of the edges on the path from node n to a root node.
- Parameters:
- Returns:
The word labelling the path from the root to n.
- Return type:
- Raises:
LibsemigroupsError – if n is greater than or equal to
Forest.number_of_nodes.