2
\$\begingroup\$

I am trying to get familiar with traversals algorithms implementations in Neo4j and to practice I am implementing classic graph algorithms (not judging if is the best approach or not, just for the sake of practising).

I implemented this find dense nodes class, but i am not completely happy with it, I feel it can be improved but I don't know how.

public class DenseNodesTraverser {

    private GraphDatabaseService db;

    private Log logger;

    public DenseNodesTraverser(GraphDatabaseService db, Log logger) {
        this.db = db;
        this.logger = logger;
    }

    public List<Node> traverse(Node node) {
        DenseNodeEvaluator eval = new DenseNodeEvaluator();
        Traverser traverser =
                            db
                            .traversalDescription()
                            .depthFirst()
                            .relationships(Rels.Weight)
                            .uniqueness(Uniqueness.NODE_GLOBAL)
                            .evaluator(eval)
                            .traverse(node);

        for (Path current : traverser) {
            System.out.println("Path " + current);
        }
        return eval.getDenseNodes();
    }

    private class DenseNodeEvaluator implements Evaluator {

        List<Node> denseNodes = new ArrayList<>();

        @Override
        public Evaluation evaluate(Path path) {
            Node lastNode = path.endNode();

            int numberOfRelationships = Iterators.size(lastNode.getRelationships().iterator());

            if (numberOfRelationships >= 3) {
                denseNodes.add(lastNode);
            }

            return Evaluation.INCLUDE_AND_CONTINUE;
        }

        public List<Node> getDenseNodes() {
            return denseNodes;
        }

    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.