cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to get shotest path according to relationships property

ta-oka
Node Link

Hi,
I'm trying to get shotest path according to relationships property "Length" that have length of able.

I tried to use "algo.shortestPath.stream" but it does not work.
I think it instead of other algorism.
does anyone know what algorism should i use?

-neo4j version, desktop 1.4.3,
-query
MATCH (start:Point{name:"90000000049676"}), (finish:Point{name:"390000000040694"})
CALL algo.shortestPath.stream(start, finish, "Length")
YIELD nodeId, cost
MATCH (n) WHERE id(n)=nodeId
return n.name, cost

1 ACCEPTED SOLUTION

The Neo4j Graph Algorithms plugin has been replace by the Neo4j Graph Data Science GDS plugin.

  • So you must install GDS on your database.
  • Then create an in-memory graph to execute the algorithm on it (you can replace the * by the relationship type or the list of relationship types):
CALL gds.graph.create(
    'myGraph',
    'Point',
    '*',
    {
        relationshipProperties: 'Length'
    }
)
MATCH (start:Point {name:"90000000049676"}), (finish:Point {name:"390000000040694"})
CALL gds.beta.shortestPath.dijkstra.stream('myGraph', {
    sourceNode: id(start),
    targetNode: id(finish),
    relationshipWeightProperty: 'Length'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN
    index,
    gds.util.asNode(sourceNode).name AS sourceNodeName,
    gds.util.asNode(targetNode).name AS targetNodeName,
    totalCost,
    [nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
    costs
ORDER BY index

Regards,
Cobra

View solution in original post

14 REPLIES 14

Hello @ta-oka

Can you give us the version of your Neo4j database?
It doesn't work because there is an error or the results are not correct?

Regards,
Cobra

ta-oka
Node Link

Hi Cobra-san,
Thank you for your reply.

Here is version of DBMS.
Version 4.2.4

And here is error message.

Neo.ClientError.Procedure.ProcedureNotFound

There is no procedure with the name algo.shortestPath.stream registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.

The Neo4j Graph Algorithms plugin has been replace by the Neo4j Graph Data Science GDS plugin.

  • So you must install GDS on your database.
  • Then create an in-memory graph to execute the algorithm on it (you can replace the * by the relationship type or the list of relationship types):
CALL gds.graph.create(
    'myGraph',
    'Point',
    '*',
    {
        relationshipProperties: 'Length'
    }
)
MATCH (start:Point {name:"90000000049676"}), (finish:Point {name:"390000000040694"})
CALL gds.beta.shortestPath.dijkstra.stream('myGraph', {
    sourceNode: id(start),
    targetNode: id(finish),
    relationshipWeightProperty: 'Length'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN
    index,
    gds.util.asNode(sourceNode).name AS sourceNodeName,
    gds.util.asNode(targetNode).name AS targetNodeName,
    totalCost,
    [nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
    costs
ORDER BY index

Regards,
Cobra

ta-oka
Node Link

Hello Cobra-san,
Thank you for your response.

Syntax is OK, but result is "(no changes, no records)".
I think I mistake something due to I am newer about neo4j.
I will try to take look and resolve.

anyway thanks for your useful response

BR oka

No problem, don't hesitate to ask if you can't find

Hello @ta-oka

What is the problem?

Regards,
Cobra

ta-oka
Node Link

Hi,
problem is "no change ,no record" on my neo4j.
I doubt name type and change to string to integer but does not work.
I attached some node and relation info, do you have any idea what should i check any point?

Can you give me the Cypher requests to create the graph please?

ta-oka
Node Link

Hi Cobra-san,
Sorry for bothering you.

I attached sample csv files for test.
Could you import below files.

Thank you for your support
BR//Oka

・Node_0416.csv (need to change file name txt to csv due to upload issue)

LOAD CSV WITH HEADERS FROM 'file:///Node_0416.csv' AS line
CREATE (:Point { name:line.FromID})

・Relation(need to change file name txt to csv due to upload issue)

LOAD CSV WITH HEADERS FROM "file:///Rel_0416.csv" AS csvLine

MATCH (a:Point {name:csvLine.FromID}), (b:Point {name:csvLine.ToID})
CREATE (a)-[:Cable {Length: csvLine.Length,Structure: csvLine.Structure,Capa: csvLine.Capa,Used: csvLine.Used,Rate: csvLine.Rate}]->(b)

ta-oka
Node Link

sorry attached file are here.Node_0416.txt (65 Bytes) Rel_0416.txt (247 Bytes)

Convert Length property to floats:

MATCH p=()-[r:Cable]->() SET r.Length = toFloat(r.Length)

Create the in-memory graph:

CALL gds.graph.create(
    'myGraph',
    'Point',
    'Cable',
    {
        relationshipProperties: 'Length'
    }
)

Get the shortest path:

MATCH (start:Point {name:"1001"}), (finish:Point {name:"1007"})
CALL gds.beta.shortestPath.dijkstra.stream('myGraph', {
    sourceNode: id(start),
    targetNode: id(finish),
    relationshipWeightProperty: 'Length'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN
    index,
    gds.util.asNode(sourceNode).name AS sourceNodeName,
    gds.util.asNode(targetNode).name AS targetNodeName,
    totalCost,
    [nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
    costs
ORDER BY index

It returns:

Regards,
Cobra

ta-oka
Node Link

Hi Cobra-san,
Thank you so much for your support!

Your cypher is working and that result is I want.
I really appreciate for your support.

BR//Okano

ta-oka
Node Link

Hi,
I have additional question.
Can I get multiple path according to in the order of relation property that is length.
Sometimes cannot use shortest path.so i would like to know top5 path.

Hello @ta-oka

You should always open a new topic since it's a different problem.
You should have a look at the allShortestPaths() function.

Regards,
Cobra