Given that I have a torch_geometric
graph with a given connectivity, e.g.
edge_index = torch.tensor([[0, 1, 2, 3, 2, 0],
[1, 0, 3, 2, 0, 2]], dtype=torch.long)
I would like to implement a function that computes K-th order neighbors from the given graph and returns a new graph with an updated connectivity. In this case for example, considering 2-hop neighbors I would end up with a new graph whose connectivity looks like this:
edge_index = torch.tensor([[0, 3, 1, 2, 1, 3],
[3, 0, 2, 1, 3, 1]], dtype=torch.long)
I'm wondering how this function could be implemented.
To create a new graph where two vertices have a link if and only if those two vertices are separated in the old graph by a shortest path that has exactly two hops, the algorithm is:
I know nothing about pytorch but any decent graph-theory library ( e.g. networkx ) will allow this to be easily implemented, though for greatest efficiency you will probably need to recode DFS.
Here is some C++ code to implement this algorithm using the PathFinder graph theory library.
The output is
Note that 1,3 is absent because vertices 1 and 3 are 3 hops apart.