How to get the depth after calling the bfs algorithm in graph data science?

How to get the depth of every node after calling the BFS algorithm in gds?It seems that the result 'path' is a graph, but I want a map with a node id and its depth. I don't know how to write cypher statement.

The document of gds only gives three alternative results of BFS, not including the depth:Breadth First Search - Neo4j Graph Data Science

I tried many ways to get the depth, but failed.

I searched for the solution in the forum, but the answer tells me to change the algorithm and use something called APOC, not matching my need.

I'll appreciate it a lot if you can help me ^ ^

Hi there,

the path that is returned is a Path type from Cypher, you can use the length function to get its length:

MATCH (source:Node{ID:1})
CALL gds.bfs.stream
'directed_unweighted_test',
{
sourceNode: source
})
YIELD
sourceNode, path
RETURN sourceNode, length(path) AS depth

You can also use size to inspect the length of the nodeIds return field.

1 Like

Sorry, I think this is wrong :hear_no_evil:.Using the cypher code you offered, we can only get one row returned. And the path is in the traversal order of nodes, not the depth order. There exsists edge between the nodes with the same depth.

I think there are no ways to get the depth of every node through gds.bfs... But this is really strange for me to understand...

I see in your diagram you have ‘Next’ and ‘Edge’ relationship types. Does ‘Next’ represent a change in depth and ‘Edge’ represents nodes at the same depth? If so, you could derive the depth of each node along the path from the relationship types.