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.

Match closest node and set properties on the shortest path

Hi guys, i trying to solve this all the week but i not able to do it. I have this graph:

I whould like to find for each blue node (:node4G) what is the shortest peering brown node (:peering). Then, extract the shortestpath in order to add on each link the customers added by each (:node4G) along the path until reaching the peering node.

I'm trying different approaches, but I'm not able to find a solution even for the first part of the problem:

I have extract all the paths between nodes, but idk how to keep only the closest (:peering) node, i always get 2 paths per (:Node4G) to their respective peering,

MATCH (a:Node4G), (b:peering), p = ((a)-[:link*]-(b)) 
WITH min(length(p)) as closest, a,b,p
WHERE length(p)= closest
RETURN a.name, length(p),b.name

I tried different approach, match all (:Node4G) and iterate over them to get the paths and only keep the closest, but i see that i can't perform a MATCH/ORDER inside FOREACH clause

MATCH (a:Node4G)
WITH collect(a) as nodes
FOREACH (access_node IN nodes | 
	MATCH (access_node),(b:peering), p = ((access_node)-[:link*]-(b))
    ORDER BY lenght(p) LIMIT 1
	RETURN access_node.name,b.name, length(p)
)

Another attempt, using shortestpath and trying to iterate over the result paths, but same problem again, i get two paths per (:node4G) and i need to add the customers figure only on the shortest path

 MATCH (a:Node4G), (b:peering), p = shortestPath((a)-[:link*]-(b))       
 WITH collect(distinct p) AS paths,a
 FOREACH (mypath IN paths | 
    FOREACH (r IN relationships(mypath) | 
       SET r.customers = toInteger(r.customers) + toInteger(a.customers)))      
 RETURN paths

If i get the solution to filter this path, i guess that using later a FOREACH clause i'll be able to set the customers property along the path using SET command on each segment of the path.

I'm sure that this is a trivial problem i would have liked to solve myself, but i'm spending so much time on this and i dont know how to make progress.

Thank you so much for your help!

3 REPLIES 3

Check out the pathfinding algorithms in the Graph Data Science Library: https://neo4j.com/docs/graph-data-science/current/algorithms/pathfinding/

Algorithms like Djikstra let you specify your start and end nodes.

Thanks Alice for taking your time for reply. I have seen the library, but my problem is how to iterate over all the (:peering) nodes to add the customers only on the shortestpath.

Thanks!
Victor

Any proposal to make progress?