Reputation: 11
How can I find a path between given two nodes in neo4j Java API, the product of all weights of the path is maximum in all paths between two nodes, what can I do? In my graph database, there are two elements: one is node, another is relationship, and all have a name property, but the relationship have an extra property: weight (double type, and values in (0,1]). My code as follows: How do I modify?
public static ArrayList<Path> getAllOptPaths(Long startNodeId, Long endNodeId, GraphDatabaseService db){
ArrayList<Path> optPathsBetweenTwoNodes = new ArrayList<Path>();
try (Transaction tx = db.beginTx()){
Node node1 = db.getNodeById(startNodeId);
Node node2 = db.getNodeById(endNodeId);
PathExpander<Object> pathExpander = PathExpanders.allTypesAndDirections();
CostEvaluator<Double> costEvaluator = CommonEvaluators.doubleCostEvaluator("Cost");
// find all paths between given two nodes
PathFinder<WeightedPath> dijkstraPathsFinder = GraphAlgoFactory.dijkstra(pathExpander, costEvaluator);
WeightedPath path = dijkstraPathsFinder.findSinglePath(node1, node2);
optPathsBetweenTwoNodes.add(path);
tx.success();
} catch (Exception e) {
e.printStackTrace();
}
return optPathsBetweenTwoNodes;
}
Upvotes: 1
Views: 696
Reputation: 1
Changing the codebase is not a good idea as I think. You can set the weight of the edges with key "Cost" when you set the property of the relationship (weight of the edge for example) using function setProperty().
Upvotes: 0
Reputation: 11
This problem has been resolved by myself, and the method is modify the source of neo4j core JAVA API.In the process of modifying source involved some specific documents as follows:Dijkstra.java, BestFirstSelectoryFactory.java, GraphAlgoFactory.java, Path.java, WeightedPathImpl.java, WeightedPathIterator.java. If anyone is interested in the detail of modify, you can send mail to me.
Upvotes: 0
Reputation: 31
Exploring paths is also possible in a cypher query. This can deliver you the result of a weighted path calculation (and thus the path with the minimum weight on it) as a result.
This webpage contains an example of a weighted path query. Also, the Neo4j reduce function will help you to specify how to calculate (and weigh) your paths.
Upvotes: 0