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.

Find the largest node of each segment in a series of paths

I'm trying to divide different short chains according to the head and tail I want in a long chain, and find the max duration in the short chains.

E.g..
Long chain:
NA1 -> NA2 -> NA3 -> NA4 -> NB1 -> NB2 -> NB3 -> NB4 ->...

I want to check whether the max duration in each chain is the second node.
NA1 -> NA2 -> NA3 -> NA4
NB1 -> NB2 -> NB3 -> NB4
( N means node, A,b and the number are the attribute and each node has its own duration )

MATCH p = (A:Task{FROMLOCTYPE:"1"})-[:path*]->(b:Task{TOLOCTYPE:"4"})
WITH reduce(output = , n IN nodes(p) | output + n ) as tasks

But I'm stuck here and don't know how to check the maximum duration in each list.
Or do any operation in each list.

1 ACCEPTED SOLUTION

I am not very sure if below query is what you looking for
Dynamic type can be coded as below
match (from:Chain)-[rel:CALLED]->(to:Chain) with distinct rel.type as xyz unwind xyz as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]->(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

If your requirement is different, then please frame your output and I will see if I am able to help you

View solution in original post

8 REPLIES 8

intouch_vivek
Graph Steward

Hi,
Could you please provide small dataset and the logic to break the big chain to smaller ones.

Regards
Vivek

This is the sample for my data.


Each node has FROMLOCTYPE,TOLOCTYPE,DURATION and Program.

I want to confirm that all nodes under the same program control, its longest time is in the second position (from B to C).

Thanks again for your reply.

I inserted your sample data as below

A B 10 typeA
B C 12 typeA
C D 9 typeA
D A 2 typeA
A B 11 typeB
B C 19 typeB
C D 10 typeB
D A 5 typeB
A B 2 typeC
![image 690x282](upload://2QZQci5kzESaOhSTVpIZtkxivfr.png)

**and applied Query as **
unwind ["typeA","typeB","typeC"] as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]->(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

**Output **

Hi, thank for your apply again.

Sorry my previous description was not clear enough.

I know how to find the max duration in one path.
By using

with max(n.attribute) as max_d
return (n.attribute:max_d)

But when I match to multiple path and try to find the max duration on each path.
I don't know how to find.
This is the data I use.

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
return p limit 3

I can group nodes according to the classification I want.
But I ’m not sure how to query for the max duration in each group with cypher.

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
WITH reduce(output = , n IN nodes(p) | output + n ) as nodeCollection

Sorry my previous description was not clear enough.
Thank you again for your reply

Hi,

So you can see that my code is performing based on the type of relationship..
Do you want to find the query dynamically?

Hi, thank for your apply again.
Yes I want to find the query dynamically.

I think my problem should be when I find a bunch of lists.
How do I search these nodes at the same time.
A bit like how to find the maximum value of an array in a two-dimensional array.

I want to find the max number in each list I find.

Sorry my previous description was not clear enough.
Thank you again for your reply

I am not very sure if below query is what you looking for
Dynamic type can be coded as below
match (from:Chain)-[rel:CALLED]->(to:Chain) with distinct rel.type as xyz unwind xyz as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]->(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

If your requirement is different, then please frame your output and I will see if I am able to help you

Thanks for your reply.

This is helpful for me.
But if I don't want to use unwind ["typeA","typeB","typeC"] as typeOf.
How can I match the all type of the relation and unwind it?

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online