mojolearn.connected_components#

mojolearn.connected_components(A, directed=True, connection='weak', return_labels=True, numeric_mode=None)[source]#

Connected components of a graph.

Follows scipy.sparse.csgraph.connected_components. An edge is any nonzero entry of the adjacency matrix. Weak connectivity ignores edge direction. Components are numbered in order of their lowest node, so component 0 holds node 0, as scipy numbers them.

Parameters:
  • A (array-like of shape (n, n), scipy.sparse matrix, or tuple) – Adjacency matrix: a dense square array, a scipy.sparse matrix (any format), or an (indptr, indices, n) CSR tuple.

  • directed (bool, default True) – Whether the graph is directed.

  • connection ({‘weak’, ‘strong’}, default ‘weak’) – Connectivity type. 'strong' on a directed graph raises NotImplementedError; on an undirected graph it equals 'weak'. Other values raise ValueError.

  • return_labels (bool, default True) – Also return the component label of each node.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for the call ('fast' is Apple only). None uses the process default.

Returns:

  • n_components (int) – Number of connected components.

  • labels (Array of shape (n,), int32) – Component of each node; returned only when return_labels is True.

Examples

>>> import numpy as np, mojolearn
>>> A = (np.random.default_rng(0).random((500, 500)) < 0.002).astype(np.float32)
>>> n_components, labels = mojolearn.connected_components(A, directed=False)