k_hop_subgraph
¶
k_hop_subgraph(
node_idx: Union[int, Sequence[int], array],
num_hops: int,
edge_index: array,
relabel_nodes: bool = False,
num_nodes: Optional[int] = None,
directed: bool = False,
) -> tuple[array, array, array, array]
Computes the num_hops-hop subgraph around the nodes node_idx.
Edges are followed backwards from the target nodes: the subgraph contains
every node whose messages reach node_idx within num_hops message
passing steps (i.e. the receptive field of node_idx for a GNN with
num_hops layers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_idx
|
Union[int, Sequence[int], array]
|
The central node(s), as an int, a list or an array. |
required |
num_hops
|
int
|
Number of hops to follow. |
required |
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge |
required |
relabel_nodes
|
bool
|
If |
False
|
num_nodes
|
Optional[int]
|
Number of nodes in the graph. Inferred from |
None
|
directed
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
- `subset`
|
the sorted ids of the nodes of the subgraph; |
- `edge_index`
|
the edges of the subgraph; |
- `mapping`
|
the position of each node of |
- `edge_mask`
|
boolean mask of shape |
Example:
edge_index = mx.array([[0, 1, 2, 3], [1, 2, 3, 4]]) # path 0 -> ... -> 4
subset, sub_edge_index, mapping, edge_mask = k_hop_subgraph(3, 2, edge_index)
# subset: mx.array([1, 2, 3])
# sub_edge_index: mx.array([[1, 2], [2, 3]])
# mapping: mx.array([2])
# edge_mask: mx.array([False, True, True, False])
Source code in mlx_graphs/utils/subgraph.py
from mlx_graphs.utils import k_hop_subgraph · mlx_graphs/utils/subgraph.py