Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-06-2021 04:14 AM
Hello all.
I am trying to find the reachability matrix for a graph.
Reachability matrix (is a N*N matrix) implies ---> If a directed edge from node i to node j in the reachability graph (i.e., reachability matrix element R ij = 1) indicates that one can reach node j from node i either directly or indirectly.
I used the below query:
match (n:Operator ), (m:Operator )
return n.id, m.id,
case
when n.id = m.id then 1
when (n)-->(m) then 1
else 0
end as value
Output which I am getting using above code is:
In line 5- It gives a output value of 1 if an edge exists between the nodes n & m , else gives 0.
But instead I'm trying to get output value 1 if a path exists to reach node n from node m either directly or indirectly .
How can I satisfy this condition?
Thank you.
Solved! Go to Solution.
05-06-2021 06:27 AM
I got the same problem
Here is the solution:
MATCH (n:Operator)
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
WITH a, b
RETURN a.id, b.id,
CASE
WHEN a = b THEN 1
WHEN length(shortestPath((a)-[*]->(b))) IS NOT NULL THEN 1 ELSE 0
END AS value
05-06-2021 04:53 AM
Hello @munavath.2001077
This query should do the job:
MATCH (n)
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
RETURN a.id AS n1, b.id AS n2,
CASE
WHEN a.id = b.id THEN 1
WHEN EXISTS((a)-[*]->(b)) THEN 1
ELSE 0
END AS value
You can also use EXISTS((a)-[*]-(b))
if you don't want to restrict the direction.
Regards,
Cobra
05-06-2021 05:26 AM
Thank you @Cobra for the reply. I have run the query, but it keeps on running but not showing the output? What would be the issue ?
05-06-2021 05:27 AM
How many nodes do you have?
Do there is only Operator nodes or do you have other types?
05-06-2021 05:31 AM
Below is the query which I have been using and I have 6 nodes, also has additional label only on start & end node:
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
WITH apoc.coll.flatten(collect([startNode(rel), endNode(rel)])) AS nodes
WITH [head(nodes), last(nodes)] AS startEnd
UNWIND startEnd AS n
MATCH (n)
SET n:StartEnd
05-06-2021 05:34 AM
This query tooks 4ms on my database:
MATCH (n:Operator)
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
WITH a, b, CASE WHEN a = b THEN 1 WHEN EXISTS((a)-[*]->(b)) THEN 1 ELSE 0 END AS value
RETURN a.id, b.id, value
05-06-2021 05:42 AM
It is working @Cobra , Thank you.
But in one case I had 10 nodes and 28 relationship there it keeps on running but not showing the output. I am assuming it takes time as the number of nodes & relationship increase?
05-06-2021 05:43 AM
That's weird, can you send me the file?
05-06-2021 05:53 AM
05-06-2021 05:55 AM
You can upload the file directly but you must replace the .csv by .txt.
05-06-2021 06:27 AM
I got the same problem
Here is the solution:
MATCH (n:Operator)
WITH collect(n) AS nodes
UNWIND nodes AS a
UNWIND nodes AS b
WITH a, b
RETURN a.id, b.id,
CASE
WHEN a = b THEN 1
WHEN length(shortestPath((a)-[*]->(b))) IS NOT NULL THEN 1 ELSE 0
END AS value
All the sessions of the conference are now available online