Can DGLGraph get the shortest path between two node?

I wanna get the loss weighted by the path length between the negative node and the positive node (maybe the sampling weight also generate by this) in a hierarchical graph.

Dose DGL provide graph API for this usage?

1 Like

You could implement single-source-shortest-path using message passing:

g.ndata['x'] = torch.zeros(g.num_nodes())
g.ndata['x'][:] = 1000000    # very big number
g.ndata['x'][source] = 0
for _ in range(graph_diameter):
    g.update_all(fn.u_add_e(β€˜x’, β€˜distance’, β€˜m’), fn.min(β€˜m’, β€˜c’))
    g.ndata[β€˜x’] = torch.min(g.ndata[β€˜x’], g.ndata[β€˜c’])

If you have multiple sources you can parallelize similarly. Note that this will give you a num_sources x num_nodes distance tensor.

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