Skip to content

get_laplacian

get_laplacian(
    edge_index: array,
    edge_weights: Optional[array] = None,
    normalization: Optional[Literal["sym", "rw"]] = None,
    num_nodes: Optional[int] = None,
) -> tuple[array, array]

Computes the graph Laplacian of the graph given by edge_index and optional edge_weights, in sparse (edge list) form.

Existing self-loops are removed first. The degree \(\mathbf{D}\) is the weighted out-degree (the sum of the weights of the edges leaving a node).

Parameters:

Name Type Description Default
edge_index array

a [2, num_edges] array representing the source and target nodes of each edge

required
edge_weights Optional[array]

Optional weight of each edge, of shape [num_edges]. Default: None (all ones).

None
normalization Optional[Literal['sym', 'rw']]

The normalization of the Laplacian:

  • None: no normalization, \(\mathbf{L} = \mathbf{D} - \mathbf{A}\)
  • "sym": symmetric normalization, \(\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1/2} \mathbf{A} \mathbf{D}^{-1/2}\)
  • "rw": random-walk normalization, \(\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1} \mathbf{A}\)
None
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index if not given.

None

Returns:

Type Description
array

A tuple (edge_index, edge_weights) with the non-zero entries of the

Laplacian

the off-diagonal entries (one per edge that is not a

tuple[array, array]

self-loop, in the input order) followed by the num_nodes diagonal

tuple[array, array]

entries.

Example:

edge_index = mx.array([[0, 1, 1, 2], [1, 0, 2, 1]])

get_laplacian(edge_index)
# (mx.array([[0, 1, 1, 2, 0, 1, 2], [1, 0, 2, 1, 0, 1, 2]]),
#  mx.array([-1, -1, -1, -1, 1, 2, 1]))

get_laplacian(edge_index, normalization="rw")
# (mx.array([[0, 1, 1, 2, 0, 1, 2], [1, 0, 2, 1, 0, 1, 2]]),
#  mx.array([-1, -0.5, -0.5, -1, 1, 1, 1]))
Source code in mlx_graphs/utils/laplacian.py
@validate_edge_index
def get_laplacian(
    edge_index: mx.array,
    edge_weights: Optional[mx.array] = None,
    normalization: Optional[Literal["sym", "rw"]] = None,
    num_nodes: Optional[int] = None,
) -> tuple[mx.array, mx.array]:
    r"""
    Computes the graph Laplacian of the graph given by ``edge_index`` and
    optional ``edge_weights``, in sparse (edge list) form.

    Existing self-loops are removed first. The degree :math:`\mathbf{D}` is the
    weighted out-degree (the sum of the weights of the edges leaving a node).

    Args:
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        edge_weights: Optional weight of each edge, of shape ``[num_edges]``.
            Default: ``None`` (all ones).
        normalization: The normalization of the Laplacian:

            - ``None``: no normalization,
              :math:`\mathbf{L} = \mathbf{D} - \mathbf{A}`
            - ``"sym"``: symmetric normalization,
              :math:`\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1/2} \mathbf{A}
              \mathbf{D}^{-1/2}`
            - ``"rw"``: random-walk normalization,
              :math:`\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1} \mathbf{A}`

        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.

    Returns:
        A tuple ``(edge_index, edge_weights)`` with the non-zero entries of the
        Laplacian: the off-diagonal entries (one per edge that is not a
        self-loop, in the input order) followed by the ``num_nodes`` diagonal
        entries.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 1, 2], [1, 0, 2, 1]])

        get_laplacian(edge_index)
        # (mx.array([[0, 1, 1, 2, 0, 1, 2], [1, 0, 2, 1, 0, 1, 2]]),
        #  mx.array([-1, -1, -1, -1, 1, 2, 1]))

        get_laplacian(edge_index, normalization="rw")
        # (mx.array([[0, 1, 1, 2, 0, 1, 2], [1, 0, 2, 1, 0, 1, 2]]),
        #  mx.array([-1, -0.5, -0.5, -1, 1, 1, 1]))
    """
    if normalization not in (None, "sym", "rw"):
        raise ValueError(
            f"Invalid normalization '{normalization}', expected None, 'sym' or 'rw'"
        )
    num_edges = edge_index.shape[1]
    if edge_weights is not None and edge_weights.size != num_edges:
        raise ValueError(
            "edge_weights must be 1 per edge "
            f"(got {num_edges} edges and {edge_weights.size} weights)"
        )
    num_nodes = _infer_num_nodes(edge_index, num_nodes)
    edges = _to_numpy(edge_index)
    if edges.size > 0 and int(edges.max()) >= num_nodes:
        raise ValueError(
            f"num_nodes={num_nodes} but `edge_index` contains node {int(edges.max())}"
        )

    dtype = mx.float32
    if edge_weights is None:
        weights = np.ones(num_edges, dtype=np.float64)
    else:
        if mx.issubdtype(edge_weights.dtype, mx.floating):
            dtype = edge_weights.dtype
        weights = _to_numpy(edge_weights).reshape(-1).astype(np.float64)

    not_loop = edges[0] != edges[1]
    edges, weights = edges[:, not_loop], weights[not_loop]
    # int64 indices: `np.bincount` rejects uint64. It also returns integers for
    # an empty input, even with weights.
    row, col = edges[0].astype(np.int64), edges[1].astype(np.int64)
    deg = np.bincount(row, weights=weights, minlength=num_nodes).astype(np.float64)

    if normalization is None:
        diagonal = deg
        off_diagonal = -weights
    else:
        diagonal = np.ones(num_nodes, dtype=np.float64)
        if normalization == "sym":
            deg_inv_sqrt = _inverse(deg, -0.5)
            off_diagonal = -deg_inv_sqrt[row] * weights * deg_inv_sqrt[col]
        else:
            off_diagonal = -_inverse(deg, -1.0)[row] * weights

    nodes = np.arange(num_nodes, dtype=edges.dtype)
    laplacian_index = np.concatenate([edges, np.stack([nodes, nodes])], axis=1)
    laplacian_weights = np.concatenate([off_diagonal, diagonal])
    return (
        mx.array(laplacian_index, dtype=edge_index.dtype),
        mx.array(laplacian_weights.astype(np.float32)).astype(dtype),
    )

from mlx_graphs.utils import get_laplacian · mlx_graphs/utils/laplacian.py