DFS execution on neo4j chart

I have a database with the following structure. enter image description here

The node property is of type

 create (A:Property {value:"abc"})

      

How do I make dfs so that it can print all the values ​​in the graph. In order A-> B-> E-> F-> C-> G-> H-> D-> I-> J

The ratio r is in the downward direction (one direction) with no properties. I tried this link but it looks complicated to me.

Is there an easier way to make a simple dfc in a pre-existing Neo4j database

+3


source to share


1 answer


The link you linked is very detailed to cover all the different things you can do with the powerful Neo4j API.

I think all you have to do is:

TraversalDescription traversalDescription = graphDb.traversalDescription()
            .depthFirst()
            .relationships(YourRelationShipTypeR, Direction.OUTGOING);

    Node a = ... // however you find your node A

    try(ResourceIterator<Node> nodes =traversalDescription.traverse(a)
                                                       .nodes()
                                                       .iterator()){
        while(nodes.hasNext()){
            Node n = nodes.next();
            //or whatever property name you use to get your names for nodes
            System.out.print(n.getProperty("id") + "->");
        }

    }

      

Should print A->B->E->F->C->G->H->D->I->J->

You can make the print statement smarter without adding an arrow on the last node, but I'll leave that up to you

EDIT

After trying the code itself, I got the first depth search, but the iterator order was out of order. It seemed to randomly choose which child node to go first. So I got the output as A->D->J->I->C->H->G->B->F->E->

.



So, you need to sort the returned paths TraversalDescription

that the method has sort(Comparator<Path> )

.

To match the required workaround, I sorted the paths using the node property, which gives node its name, which I called "id". Here's the updated workaround code:

 TraversalDescription traversalDescription = graphDb.traversalDescription()
            .depthFirst()
            .sort(new PathComparatorByName())
            .relationships(YourRelationShipTypeR, Direction.OUTGOING);

      

Where PathComparatorByName is the comparator, I wrote that it sorts paths based on the nodes traversed in the path, lexically sorted by name:

private class PathComparatorByName implements Comparator<Path>{

    @Override
    public int compare(Path o1, Path o2) {
        Iterator<Node> iter1 = o1.nodes().iterator();
        Iterator<Node> iter2 = o2.nodes().iterator();


        while(iter1.hasNext()){
            if(!iter2.hasNext()){
                //return shorter path?
                return 1;
            }

            Node n1 = iter1.next();
            Node n2 = iter2.next();
            int nodeCmp = compareByNodeName(n1, n2);
            if(nodeCmp !=0){
                return nodeCmp;
            }

        }
        if(iter2.hasNext()){
            //return shorter path?
            return -1;
        }
        return 0;
    }

    private int compareByNodeName(Node node1, Node node2) {
        String name1 = (String)node1.getProperty("id");

         String name2 = (String)node2.getProperty("id");

        return name1.compareTo(name2);
    }

}

      

Replay now with a comparator will output:

A->B->E->F->C->G->H->D->I->J-> 

      

+3


source







All Articles