How to get neighboring nodes of a given node in dgl graph object?

I have a dgl graph g as follows

u, v = torch.tensor([0, 0, 0, 1]), torch.tensor([1, 2, 3, 3]) 
g = dgl.graph((u, v))

How can I obtain the neighbors of node 3 directly from the graph object?

Hi! I’ve dealt with something similar and I’ve done two things:

1- Convert DGL graph to networks: dgl.to_networkx — DGL 0.6.1 documentation
2- Use networks neighbours function: Graph.neighbors — NetworkX 2.7.1 documentation

Thanks for the reply!

I could use different workarounds to identify the neighbors of a node like you have suggested ( also one can use an adjacency list). However, I was hoping that the DGLGraph object would have such a capability, given that, we can add or remove edges for a dgl graph.

These could help: DGLGraph.predecessors and DGLGraph.successors

Besides what @neo said, in_edges and out_edges also work (and it could give you the neighboring edges as well).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.