neo4jcypherneo4j-gds

How to Project a path in NEO4J for topological sorting?


I am making a package manager using a neo4j database with packages as nodes and DEPENDS_ON relationships. To build package A, I first need to build packages B and C, for example. And to do that I need to do a topological sort, but IDK how to project the paths using gds.graph.project().

Here is how I get the paths that I need to project:

MATCH path = (n:Package{name: 'neofetch'})-[r:DEPENDS_ON*]->(l:Leaf)

I project the above path to a GDS graph using:

RETURN gds.graph.project("g", n, l, {})

and then do topological sort:

CALL gds.dag.topologicalSort.stream("g", {computeMaxDistanceFromSource: true})
YIELD nodeId, maxDistanceFromSource
RETURN gds.util.asNode(nodeId).name AS name, maxDistanceFromSource
ORDER BY maxDistanceFromSource, name

The result is only the first node and last nodes no intermediate nodes appear in the sort.


Solution

  • You need to pass every adjacent pair of nodes in the path(s), not just the first and last node. This should work for you:

    MATCH (:Package{name: 'neofetch'})-[rels:DEPENDS_ON*]->(:Leaf)
    UNWIND rels AS r
    WITH DISTINCT r
    RETURN gds.graph.project("g", STARTNODE(r), ENDNODE(r))