2017-08-22 8 views
0

私は、ノードが都市を表し、関係が都市を結ぶ道路を表すneo4jのグラフを持っています。関係は重み付けされ、「距離」と呼ばれるプロパティを持ちます。 私は2つの都市AとBの間で最短(最軽量の経路)を探したいと思っています。これはDijkstraのアルゴリズムを使って簡単に行えます。難しい部分は、私はAからBへの道で覆われる一連の都市があるということです。言い換えれば、AからBへの道はすべての中間地点をカバーすべきです、順序は重要ではありません。 送信元、宛先、ウェイポイントのリストを提供し、最適化するようなもの:Google APIでtrue。私は以下でサイファーを使用して、これを試してみました はneo4jのウェイポイントを通る2つのノード間の最短経路

match(s:City{ID:"A"}) 
match(f:City{ID:"B"}) 
match (n:City) where n.name in ['City1','City2','City3','City4'] 
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f)) 
where ALL (n in wps WHERE n in nodes(path)) with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as    
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance 

をquery-しかし、これは動作しませんでした。たぶん、パスがすべてのウェイポイントを通過していなかったからかもしれません。 私はA *やDijkstra(Neo4j Traversalを使用しています)を使用しようとしていますが、すべてのウェイポイントを訪れる方法はわかりません。

ありがとうございます!

答えて

0

ANYではなくALLをお試しください。 neo4j documentation

Allから

- 述語は、このリストのすべての要素のために保持しているかどうかをテストします。

Any一方

- 述語が リスト内の少なくとも1つの要素にも当てはまるかどうかをテスト。

match(s:City{ID:"A"}) 
match(f:City{ID:"B"}) 
match (n:City) where n.name in ['City1','City2','City3','City4'] 
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f)) 
where ANY (n in wps WHERE n in nodes(path)) with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as    
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance 

編集 - 私たちはどんなwhere句に複数組み合わせることにより、すべての都市が含まれていることを確認するために、これを変更することができます。

match(s:City{ID:"A"}) 
match(f:City{ID:"B"}) 
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f)) 
where ANY (n in nodes(wps) WHERE n.name = 'City1') AND 
     ANY (n in nodes(wps) WHERE n.name = 'City2) 
with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as    
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance 
+0

は、私はパスがすべて含まれている必要があることを望みますリスト内のポイント。私は "ANY"はそれを解決しないだろうと思う。 – mayankgupta565

+0

@ mayankgupta565すべてのウェイポイントが含まれていることを確認するために私の答えを修正しました –

関連する問題