3

私は平凡なコーディング スキルを持つデータ サイエンティストなので、これまで neo4j を使用して Java を避け、Cypher と Gremlin を使用してきました。

各エッジに重みプロパティを付けて、多数のグラフ データを neo4j に押し込みました。可能であれば、gremlin コンソール内からこのグラフで aStar または dijkstra を実行したいと思います。JUNG には加重最短経路アルゴリズムがないように思われるので、私は GraphAlgoFactory に目を向けましたが、オンラインでの議論はすべてストレート Java で行われているようです。

私の目標は、重みがエッジの重みプロパティによって決定される、選択した 2 つの頂点間の最短の重み付きパスを取得することです。

ありがとう!

4

1 に答える 1

3

I'll give you an example to use as a starting point to explore your own graph. Start by firing up Gremlin and defining a utility method to create edges:

def edge(Graph g, Vertex v1, Vertex v2, int weight) {
    e = g.addEdge(null, v1, v2, "connects");
    e.setProperty("weight", weight);
}

Now create a simple graph with 4 nodes and 4 edges:

g = new Neo4jGraph("/tmp/dijkstra")
v1 = g.addVertex(1);
v2 = g.addVertex(2);
v3 = g.addVertex(3);
v4 = g.addVertex(4);

edge(g, v1, v2, 13);
edge(g, v1, v4, 20);
edge(g, v2, v3, 3);
edge(g, v4, v3, 40);

Since the version of Gremlin in the console runs on Groovy, you can mix and match Groovy and Java seamlessly to use the GraphAlgoFactory:

import org.neo4j.graphalgo.GraphAlgoFactory;
import org.neo4j.graphalgo.CommonEvaluators;
import org.neo4j.kernel.Traversal;

dijkstra = GraphAlgoFactory.dijkstra(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("weight"))
path = dijkstra.findSinglePath(((Neo4jVertex)v1).getRawVertex(), ((Neo4jVertex)v3).getRawVertex())
println path
// (1)--[connects,0]-->(2)--[connects,2]-->(3) weight:16.0

The code to use the GraphAlgoFactory comes mainly from the Neo4j documentation. The only difference is that you need to cast each Blueprints Vertex to an org.neo4j.graphdb.Node, since the GraphAlgoFactory doesn't understand Blueprints.

于 2012-07-27T05:32:43.923 に答える