Skip to content

LineGraph

LineGraph(force_directed: bool = False)

Bases: BaseTransform

Converts a graph to its line graph, as PyG's LineGraph.

\[ L(\mathcal{G}) &= (\mathcal{V}^{\prime}, \mathcal{E}^{\prime}) \mathcal{V}^{\prime} &= \mathcal{E} \mathcal{E}^{\prime} &= \{ (e_1, e_2) : e_1 \cap e_2 \neq \emptyset \} \]

The edges are first coalesced (sorted, duplicates merged by summing their edge_features), and the nodes of the line graph follow that order.

  • For directed graphs (or with force_directed), every edge is a node of the line graph, and there is a line edge from \((u, v)\) to every edge \((v, w)\).
  • For undirected graphs, every pair of reciprocal edges \((u, v)\), \((v, u)\) is a single node of the line graph (numbered in the order of the coalesced edges with \(u < v\)), and two such nodes are connected in both directions if they share an endpoint.

The new node_features are the (coalesced) old edge_features, the features of reciprocal edges being summed for undirected graphs, and edge_features is removed. The number of nodes is stored explicitly. As in PyG, the other attributes are left unchanged.

Parameters:

Name Type Description Default
force_directed bool

If True, the graph is always treated as directed. Default: False

False

Example:

# The path 0 - 1 - 2, undirected.
graph = GraphData(edge_index=mx.array([[0, 1, 1, 2], [1, 0, 2, 1]]))
graph = LineGraph()(graph)
graph.edge_index
# mx.array([[0, 1], [1, 0]])
graph.num_nodes
# 2

Methods:

Name Description
process

Converts data to its line graph.

Source code in mlx_graphs/transforms/topology.py
def __init__(self, force_directed: bool = False):
    self.force_directed = force_directed

process

process(data: GraphData) -> GraphData

Converts data to its line graph.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The line graph.

Source code in mlx_graphs/transforms/topology.py
def process(self, data: GraphData) -> GraphData:
    """Converts ``data`` to its line graph.

    Args:
        data: The graph to transform.

    Returns:
        The line graph.
    """
    num_nodes = _num_nodes(data)
    dtype = data.edge_index.dtype
    edge_features = data.edge_features
    if edge_features is not None and edge_features.shape[0] == data.num_edges:
        edge_index, edge_features = coalesce(
            data.edge_index, edge_features, num_nodes, "add"
        )
    else:
        edge_index = coalesce(data.edge_index, num_nodes=num_nodes)
    src, dst = np.asarray(edge_index).astype(np.int64)
    num_edges = src.size

    if self.force_directed or data.is_directed():
        # Coalesced edges are sorted by source: those leaving `v` are
        # `ptr[v]` to `ptr[v + 1] - 1`.
        ptr = np.zeros(num_nodes + 1, dtype=np.int64)
        np.cumsum(np.bincount(src, minlength=num_nodes), out=ptr[1:])
        line_src, line_dst = _expand(ptr, dst)
        line_edges = np.stack([line_src, line_dst])
        node_features = edge_features
        num_line_nodes = num_edges
    else:
        # Id of each undirected edge {u, v}, in the order of the
        # coalesced edges with u < v.
        not_loop = src != dst
        low, high = np.minimum(src, dst), np.maximum(src, dst)
        pair_keys = low * max(num_nodes, 1) + high
        unique_keys = np.unique(pair_keys[not_loop])
        edge_ids = np.searchsorted(unique_keys, pair_keys)
        num_line_nodes = unique_keys.size

        # Line nodes incident to each node, grouped by node: every
        # undirected edge {u, v} (u < v) is incident to u and to v.
        forward = src < dst
        ends = np.concatenate([src[forward], dst[forward]])
        ids = np.concatenate([edge_ids[forward], edge_ids[forward]])
        order = np.lexsort((ids, ends))
        ends, ids = ends[order], ids[order]
        ptr = np.zeros(num_nodes + 1, dtype=np.int64)
        np.cumsum(np.bincount(ends, minlength=num_nodes), out=ptr[1:])
        owner, position = _expand(ptr, ends)
        line_src, line_dst = ids[owner], ids[position]
        not_self = line_src != line_dst
        line_edges = _unique_pairs(
            line_src[not_self], line_dst[not_self], num_line_nodes
        )

        node_features = None
        if edge_features is not None:
            selected = mx.array(np.nonzero(not_loop)[0])
            values = edge_features[selected] if selected.size else None
            node_features = mx.zeros(
                (num_line_nodes, *edge_features.shape[1:]), edge_features.dtype
            )
            if values is not None:
                node_features = node_features.at[mx.array(edge_ids[not_loop])].add(
                    values
                )

    data.edge_index = mx.array(line_edges.astype(np.int64), dtype=dtype)
    data.node_features = node_features
    data.edge_features = None
    data.num_nodes = num_line_nodes
    return data

from mlx_graphs.transforms import LineGraph · mlx_graphs/transforms/topology.py