pytorch_geometric icon indicating copy to clipboard operation
pytorch_geometric copied to clipboard

`k_hop_subgraph` doesn't work on graph with cycle

Open fate1997 opened this issue 3 years ago • 3 comments

🐛 Describe the bug

As I understand, the 1-hop subgraph of the given node index should be the connected edges with this node, however, when I apply k_hop_subgrpah function to a 3-node cycle graph, it seems that the filtered edge_index will include the inconnected edge_index with the specific node.

import torch
from torch_geometric.utils import k_hop_subgraph

edge_index = torch.LongTensor([[0, 1], [1, 0], [1, 2], [2, 1], [2, 0], [0, 2]]).T
print(k_hop_subgraph(0, 1, edge_index)[1])

The result is:

tensor([[0, 1, 1, 2, 2, 0],
        [1, 0, 2, 1, 0, 2]])

You can see the [1, 2] and [2, 1] are included as a 1-hop subgraph of node 0

Environment

  • PyG version: 2.0.4
  • PyTorch version: 1.11.0
  • OS: windows
  • Python version: 3.8.0
  • CUDA/cuDNN version: 11.2
  • How you installed PyTorch and PyG (conda, pip, source): conda
  • Any other relevant information (e.g., version of torch-scatter): None

fate1997 avatar Jul 18 '22 07:07 fate1997

Sorry for the late reply, I was offline for a few days. I think the computation is correct. k_hop_subgraph will return the induced subgraph of all nodes reachable within k hops.

rusty1s avatar Jul 30 '22 10:07 rusty1s

Thank you for your reply. Strickly speaking, I think the k_hop_subgraph should generate all the edges reachable within k hops instead of nodes . However, since the generated additional edges don't influence the computational graph of the specific node, this little bug can be ignored.

fate1997 avatar Jul 31 '22 04:07 fate1997

I think such a method should be called k_hop_subtree. I am more than happy to add this in.

rusty1s avatar Aug 02 '22 13:08 rusty1s