topological sort graph

443 views
Skip to first unread message

TC

unread,
Jun 4, 2014, 12:50:48 AM6/4/14
to ne...@googlegroups.com
Does neo4j has topological sort function for directed graph data(with loops)?
If I follow
 for ( Path position : db.traversalDescription()
                .depthFirst()
                .relationships( Rels.LIKES, Direction.INCOMING )
                .traverse( node ) )
but how to choose the starting node in topo sort order for this graph?

Michael Hunger

unread,
Jun 4, 2014, 2:12:21 AM6/4/14
to ne...@googlegroups.com
You mean in general? I think toposort is only defined for trees? You can ignore the loops though in a traversal description (Uniqueness.Relationship-Global) and in theory start at any random node in the graph to find the topologically first (tree-root). 


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

TC

unread,
Jun 4, 2014, 3:35:15 AM6/4/14
to ne...@googlegroups.com
The graph maybe has loop inside and multiple source node(no incoming edges).
Even using traversal description (Uniqueness.Relationship-Global), it will need a starting node from users to begin. So it cannot deal with multiple source node graph. 

TC

unread,
Jun 4, 2014, 4:42:25 AM6/4/14
to ne...@googlegroups.com
i follow the algo in the wiki page below: So during while loop, i remove a node n from s and traversal each path(depth 1). and remove this path from graph. I can set property to this relationship like visited. but how to check no other incoming edges into node m. Does this mean i have make traversal from m inside the traversal from n? 
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
    remove a node n from S
    add n to tail of L
    for each node m with an edge e from n to m do
        remove edge e from the graph
        if m has no other incoming edges then
            insert m into S
if graph has edges then
    return error (graph has at least one cycle)
else 
    return L (a topologically sorted order)
Reply all
Reply to author
Forward
0 new messages