differt_core.rt.DiGraph#

class DiGraph#

Bases: object

A directed graph.

Attributes

num_nodes

The number of nodes.

Methods

all_paths(from_, to, depth, *[, ...])

Return an iterator over all paths of length depth from node from_ to node to.

all_paths_array(from_, to, depth, *[, ...])

Return an array of all paths of length depth from node from_ to node to.

all_paths_array_chunks(from_, to, depth, *)

Return an iterator over all paths of length depth from node from_ to node to, 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_nodes nodes.

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_ and to.

Detailed documentation

all_paths(from_, to, depth, *, include_from_and_to=True)#

Return an iterator over all paths of length depth from node from_ to node to.

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_ and to nodes in the output paths. If set to False, the output paths will include depth - 2 nodes.

Returns:

An iterator over all paths.

Return type:

AllPathsFromDiGraphIter

all_paths_array(from_, to, depth, *, include_from_and_to=True)#

Return an array of all paths of length depth from node from_ to node to.

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_ and to nodes in the output paths. If set to False, the output paths will include depth - 2 nodes.

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 depth from node from_ to node to, 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_ and to nodes in the output paths. If set to False, the output paths will include depth - 2 nodes.

  • chunk_size (int) – The size of each chunk.

Returns:

An iterator over all paths, as array chunks.

Return type:

AllPathsFromDiGraphChunksIter

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:
  • nodes (int) – The nodes to be disconnected.

  • fast_mode (bool) – Whether to skip removing the path to any of the specified nodes. In practice, removing paths from the nodes is sufficient, and faster to perform, but can lead to a slower graph traversal when generating all possible paths.

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_nodes nodes.

This is equivalent to creating a directed graph from an adjacency matrix will all entries equal to False.

Parameters:

num_nodes (int) – The number of nodes.

Returns:

A directed graph.

Return type:

DiGraph

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_nodes when 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 where True means the node should remain connected, and False means 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 mask is 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 M contains boolean entries: if M[i, j] is True, then node i is connected to node j.

Parameters:

adjacency_matrix (Bool[ndarray, "num_nodes num_nodes"]) – The adjacency matrix.

Returns:

A directed graph.

Return type:

DiGraph

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:

DiGraph

insert_from_and_to_nodes(*, direct_path=True, from_adjacency=None, to_adjacency=None)#

Insert two additional nodes in the graph: from_ and to.

Unless specific adjacency information is provided, the nodes satisfy the following conditions:

  • from_ is connected to every other node in the graph;

  • and to is an endpoint, where every other node is connected to this node.

If direct_path is True, then the from_ node is also connected to the to node.

Parameters:
  • direct_path (bool) – Whether to create a direction connection between from_ and to nodes.

  • 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:

tuple[int, int]

num_nodes#

The number of nodes.

Type:

int