I'm trying to find the shortest path taking into consideration the weights properties on the edges my work is on TinkerGraph and i want to do it in java.
gremlin is not very helpful for me
g.V().has(id1).
repeat(both().simplePath()).
until(has(id2)).as("path").
map(unfold().coalesce(values("weight"),
constant(0)).
sum()).as("cost").
select("cost","path").next().get("path");
this gives me the shortest path without taking into consideration the weight property on the edges .
EDITED: my example:
edges inserted:
source,target
b1,b2
b1,b2
b1,b2
b1,b2
b1,b3
b3,b2
private void add(Vertex source,Vertex target){
if(!checkEdgeExist(graph,source,target))
source.addEdge(target).property(WEIGHT,1.0);
else {
Edge e = getEdgeBetweenTwoVertices(graph,source,target);
source.edges(Direction.OUT).forEachRemaining(edge -> {
if(edge.inVertex().equals(target))
edge.property(WEIGHT,(double)e.property(WEIGHT).value()+1);
});
private static boolean checkEdgeExist(TinkerGraph graph,Vertex source,Vertex target){
return graph.traversal().V(source).outE().filter(p -> p.get().inVertex().equals(target)).hasNext();
}
in other words the weight of the edge gets updated according to the frequency of an edge, for example if b1,b2 appeared 4 time the edge will be of weight 4.Now i want Dijkstra to return the shortest path in terms of weight and not the shortest in terms of edges. path(b1,b2) = b1->b3->b2