The WordGraph class
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 \(\{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 UNDEFINED
). The number m
is referred to as
the out-degree of the word graph, or any of its nodes.
Contents
Class for representing word graphs. |
|
Add a number of new nodes. |
|
Add to the out-degree of every node. |
|
Copy a |
|
Unites a word graph in-place. |
|
Modify in-place to contain the subgraph induced by a range of nodes. |
|
Re-initialize the word graph to have m nodes and out-degree n. |
|
Returns an iterator yielding pairs consisting of edge labels and target nodes. |
|
Get the next target of an edge incident to a given node that doesn't equal |
|
Returns an iterator yielding the nodes of the word graph. |
|
Overloaded function. |
|
Returns the number of nodes. |
|
Returns the out-degree. |
|
Construct a random word graph from number of nodes and out-degree. |
|
Remove all of the edges in the word graph. |
|
Removes a given label from the word graph. |
|
Remove an edge from a node with a given label. |
|
Ensures that the word graph has capacity for a given number of nodes, and out-degree. |
|
Swaps the edge with specified label from one node with another. |
|
Overloaded function. |
|
Returns an iterator yielding the targets of the edges incident to a given node. |
Full API
- class WordGraph
- __init__(*args, **kwargs)
Overloaded function.
- __init__(self: WordGraph, m: int = 0, n: int = 0) None
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.
- __init__(self: WordGraph, num_nodes: int, targets: list[list[int | Undefined | PositiveInfinity | LimitMax]]) None
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.
- Parameters:
- Raises:
LibsemigroupsError – if any target is specified in targets is greater than or equal to num_nodes.
>>> from libsemigroups_pybind11 import WordGraph >>> WordGraph(5, [[0, 0], [1, 1], [2], [3, 3]]) <WordGraph with 5 nodes, 7 edges, & out-degree 2>
- add_nodes(self: WordGraph, nr: int) WordGraph
Add a number of new nodes.
This function modifies a word graph in-place so that it has nr new nodes added.
- add_to_out_degree(self: WordGraph, nr: int) WordGraph
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.
- disjoint_union_inplace(self: WordGraph, that: WordGraph) WordGraph
Unites a word graph in-place.
This function changes a
WordGraph
object in-place to contain the disjoint union of itself and that. The noden
of that is mapped tonumber_of_nodes() + n
.- Parameters:
that (WordGraph) – the word graph to unite.
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if self and that do not have the same out-degree.
- induced_subgraph(self: WordGraph, first: int, last: int) WordGraph
Modify in-place to contain the subgraph induced by a range of nodes.
This function modifies a
WordGraph
object in-place to contain its subgraph induced by the range of nodes first to last.- Parameters:
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if first or last is out of range.
LibsemigroupsError – if any edge with source in the range first to last has target outside the range first to last.
- init(self: WordGraph, m: int, n: int) WordGraph
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.
- labels_and_targets(self: WordGraph, source: int) collections.abc.Iterator[tuple[int, int | Undefined]]
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.
- Parameters:
source (int) – the source node.
- Returns:
An iterator.
- Return type:
- Raises:
LibsemigroupsError – if source is out of bounds.
- next_label_and_target(self: WordGraph, s: int, a: int) tuple[int | Undefined, int | Undefined]
Get the next target of an edge incident to a given node that doesn’t equal
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)
equalsUNDEFINED
for every valueb
in the range \([a, n)\), where \(n\) is the return value ofout_degree()
, thenx.first
andx.second
equalUNDEFINED
.- Parameters:
- 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
UNDEFINED
.- Return type:
- Raises:
LibsemigroupsError – if s does not represent a node in self.
- Complexity:
At worst \(O(n)\) where \(n\) equals
out_degree()
.
- nodes(self: WordGraph) collections.abc.Iterator[int]
Returns an iterator yielding the nodes of the word graph.
This function returns an iterator yielding the nodes of the word graph.
- Returns:
An iterator yielding the nodes.
- Return type:
- Complexity:
Constant.
- number_of_edges(self: WordGraph, s: int) int
Overloaded function.
- number_of_edges(self: WordGraph) int
Returns the number of edges. This function returns the total number of edges (i.e. values
s
anda
such thattarget(s, a)
is notUNDEFINED
) in the word graph.- Returns:
The total number of edges.
- Return type:
- Complexity:
\(O(mn)\) where
m
isnumber_of_nodes()
andn
isout_degree()
.
- number_of_edges(self: WordGraph, s: int) int
Returns the number of edges with given source node.
This function returns the number of edges incident to the given source node s.
- Parameters:
s (int) – the node.
- Returns:
The number of edge incident to s.
- Return type:
- Raises:
LibsemigroupsError – if s is not a node.
- Complexity:
\(O(n)\) where
n
isout_degree()
.
- number_of_nodes(self: WordGraph) int
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.
- Return type:
- Complexity:
Constant.
- out_degree(self: WordGraph) int
Returns the out-degree. This function returns the number of edge labels in the word graph.
- Returns:
The number of edge labels.
- Return type:
- Complexity:
Constant.
- static random(number_of_nodes: int, out_degree: int) WordGraph
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.
- Parameters:
- Returns:
A random word graph.
- Return type:
- Raises:
LibsemigroupsError – if number_of_nodes is less than
2
LibsemigroupsError – if out_degree is less than
2
- Complexity:
\(O(mn)\) where
m
is the number of nodes, andn
is the out-degree of the word graph.
- remove_all_targets(self: WordGraph) WordGraph
Remove all of the edges in the word graph. Set every target of every source with every possible label to
UNDEFINED
.- Returns:
self.
- Return type:
- Complexity:
\(O(mn)\) where
m
is the number of nodes andn
is the out-degree.
- remove_label(self: WordGraph, a: int) WordGraph
Removes a given label from the word graph.
This function removes the label a from a
WordGraph
object in-place. This reduces the out-degree by1
.- Parameters:
a (int) – the label to remove.
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if a is out of range.
- remove_target(self: WordGraph, s: int, a: int) WordGraph
Remove an edge from a node with a given label.
This function removes the edge with source node s labelled by a.
- Parameters:
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if s or a is out of range.
- Complexity:
Constant.
- reserve(self: WordGraph, m: int, n: int) WordGraph
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.
- swap_targets(self: WordGraph, m: int, n: int, a: int) WordGraph
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.
- Parameters:
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if any of m, n, and a is out of range.
- Complexity:
Constant
- target(*args, **kwargs)
Overloaded function.
- target(self: WordGraph, s: int, a: int, t: int) WordGraph
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.- Parameters:
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if s, a, or t is not valid.
- Complexity:
Constant.
- target(self: WordGraph, source: int, a: int) int
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.
- Parameters:
- Returns:
Returns the node adjacent to source via the edge labelled a, or
UNDEFINED
.- Return type:
- Raises:
LibsemigroupsError – if source or a is not valid.
- Complexity:
Constant.
- targets(self: WordGraph, source: int) collections.abc.Iterator[int | Undefined]
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
UNDEFINED
.- Parameters:
source (int) – the source node in the word graph.
- Returns:
An iterator yielding the targets.
- Return type:
- Raises:
LibsemigroupsError – if source is out of range (i.e. greater than or equal to
number_of_nodes
).- Complexity:
Constant.