Contract paths

Hello everyone,
Neo4j : 3.5.12

I'm looking to contract relationships of shortestpaths regarding some constraints. But, I can not find how to do it. I have a graph with two labels (Airport and Station) and distance as a property of my relationships.

My problem that the weight should be a cumulative value of precedent weights.

Example :
Given this example of a path : a - 1 -b -c -2-d where 1 and 2 are not airport, the query to create the path:

CREATE (a:Airport{id: 'a'})-[:LINKS {distance: 3 }]->(b:Station {id: '1'})-[:LINKS {distance: 4 }]->(c:Airport {id: 'b'})-[:LINKS {distance: 1}]->(d:Airport{id: 'c'})-[:LINKS {distance: 2}]->(e:Station{id: '2'})-[:LINKS {distance: 1}]->(f:Airport{id: 'd'})

Expectation (see the picture below) :
** a - 1 with weight 0
a - b with weight 0
a - c with weight 1
a - 2 with weight 1
a - d with weight 2 (since there two intermediate airports (b and c).**

The cumulative weight are illustrated by the CONTRACT relationships which has two properties: weight and distance which is the sum of contracted relationships.

Output : add shortcuts with weights (new relationships with number of airports) and the sum of distance of relations.

Thank you so much

Hi @assia.elafouani,

Your example and expectation is not very clear, kindly elaborate more.
However for cumulative weight you can try reduce function.

@intouch.vivek thank you for your reply.
I edited my post with an example that I created in neo4j.
I tried to use REDUCE function with CASE WHEN but I did not get the cumulative weight, I got the total.

I hope that my post is clear now.

Finally, I fix the problem using this query:

Match p=(a:Airport{id:'a'})-[:LINKS*]->(d)
where d:Airport or d:Station
with a, d,p, reduce(sum=0, x IN Nodes(p)[1..-1] |case when x: Airport then sum + 1 else sum end) as reductionW
return a, d,p, reduce(sum=0, x IN Relationships(p) | sum + x.distance) as reductionD, reductionW

@intouch.vivek, @andrew.bowman and @michael.hunger thank you for the explanation.