differt_core.rt.DiGraph#
- class DiGraph#
Bases:
objectA directed graph.
Attributes
The number of nodes.
Methods
all_paths(from_, to, depth, *[, ...])Return an iterator over all paths of length
depthfrom nodefrom_to nodeto.all_paths_array(from_, to, depth, *[, ...])Return an array of all paths of length
depthfrom nodefrom_to nodeto.all_paths_array_chunks(from_, to, depth, *)Return an iterator over all paths of length
depthfrom nodefrom_to nodeto, grouped in chunks of size of max.disconnect_nodes(*nodes[, fast_mode])Disconnect one or more nodes from the graph.
empty(cls, num_nodes)Create an edgeless directed graph with
num_nodesnodes.filter_by_mask(mask[, fast_mode])Disconnect all nodes where the mask is False, keeping only nodes where the mask is True.
from_adjacency_matrix(cls, adjacency_matrix)Create a directed graph from an adjacency matrix.
from_complete_graph(cls, graph)Create a directed graph from a complete graph.
insert_from_and_to_nodes(*[, direct_path, ...])Insert two additional nodes in the graph:
from_andto.Detailed documentation
- all_paths(from_, to, depth, *, include_from_and_to=True)#
Return an iterator over all paths of length
depthfrom nodefrom_to nodeto.- Parameters:
from_ (int) – The node index to find the paths from.
to (int) – The node index to find the paths to.
depth (int) – The number of nodes to include in each path.
include_from_and_to (bool) – Whether to include or not
from_andtonodes in the output paths. If set toFalse, the output paths will includedepth - 2nodes.
- Returns:
An iterator over all paths.
- Return type:
- all_paths_array(from_, to, depth, *, include_from_and_to=True)#
Return an array of all paths of length
depthfrom nodefrom_to nodeto.- Parameters:
from_ (int) – The node index to find the paths from.
to (int) – The node index to find the paths to.
depth (int) – The number of nodes to include in each path.
include_from_and_to (bool) – Whether to include or not
from_andtonodes in the output paths. If set toFalse, the output paths will includedepth - 2nodes.
- Returns:
An array of all paths.
- Return type:
UInt[ndarray, "num_paths path_depth"]
- all_paths_array_chunks(from_, to, depth, *, include_from_and_to=True, chunk_size=1000)#
Return an iterator over all paths of length
depthfrom nodefrom_to nodeto, grouped in chunks of size of max.chunk_size.- Parameters:
from_ (int) – The node index to find the paths from.
to (int) – The node index to find the paths to.
depth (int) – The number of nodes to include in each path.
include_from_and_to (bool) – Whether to include or not
from_andtonodes in the output paths. If set toFalse, the output paths will includedepth - 2nodes.chunk_size (int) – The size of each chunk.
- Returns:
An iterator over all paths, as array chunks.
- Return type:
- disconnect_nodes(*nodes, fast_mode=True)#
Disconnect one or more nodes from the graph.
This has two effects:
all paths from any of the specified nodes will be removed;
and all paths to any of the specified nodes will be removed.
- Parameters:
- Raises:
IndexError – If any of the specified nodes is not part of the graph.
- classmethod empty(cls, num_nodes)#
Create an edgeless directed graph with
num_nodesnodes.This is equivalent to creating a directed graph from an adjacency matrix will all entries equal to
False.
- filter_by_mask(mask, fast_mode=True)#
Disconnect all nodes where the mask is False, keeping only nodes where the mask is True.
This is a more efficient version of
disconnect_nodeswhen working with NumPy boolean arrays, as it avoids creating intermediate Vec<usize> collections.This has two effects:
all paths from nodes where mask is False will be removed;
and all paths to nodes where mask is False will be removed.
- Parameters:
mask (
Bool[ndarray, "num_nodes"]) – A boolean mask array whereTruemeans the node should remain connected, andFalsemeans the node should be disconnected.fast_mode (bool) – If set to
True(default), only disconnecting all paths (i.e., edges) from the nodes is sufficient, and faster to perform, but can lead to a slower graph traversal when generating all possible paths.
- Raises:
ValueError – If the length of
maskis larger from the number of nodes in the graph.
- classmethod from_adjacency_matrix(cls, adjacency_matrix)#
Create a directed graph from an adjacency matrix.
Each row of the adjacency matrix
Mcontains boolean entries: ifM[i, j]isTrue, then nodeiis connected to nodej.- Parameters:
adjacency_matrix (
Bool[ndarray, "num_nodes num_nodes"]) – The adjacency matrix.- Returns:
A directed graph.
- Return type:
- classmethod from_complete_graph(cls, graph)#
Create a directed graph from a complete graph.
This is equivalent to creating a directed graph from an adjacency matrix will all entries equal to
True, except on the main diagonal (i.e., no loop).- Parameters:
graph (CompleteGraph) – The complete graph.
- Returns:
A directed graph.
- Return type:
- insert_from_and_to_nodes(*, direct_path=True, from_adjacency=None, to_adjacency=None)#
Insert two additional nodes in the graph:
from_andto.Unless specific adjacency information is provided, the nodes satisfy the following conditions:
from_is connected to every other node in the graph;and
tois an endpoint, where every other node is connected to this node.
If
direct_pathisTrue, then thefrom_node is also connected to thetonode.- Parameters:
direct_path (bool) – Whether to create a direction connection between
from_andtonodes.from_adjacency (
Bool[ndarray, "num_nodes"]) –An optional array indicating nodes that should be connected from
from_node.If not specified, all nodes are connected.
to_adjacency (
Bool[ndarray, "num_nodes"]) –An optional array indicating nodes that should be connected to
to_node.If not specified, all nodes are connected.
- Returns:
The indices of the two added nodes in the graph.
- Return type: