Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-10-2022 01:49 AM
Hi there. Trying to formalize my issue and find the direction to the solution.
I have many graphs of parent/child relationships.
In a VALID graph:
Not sure I did define the graph in a way it can be formalized. The idea is to find not valid graphs, and it looks like defining a valid graph is easier than defining an invalid one (because there can be many combinations of invalid relations).
I'll try to show it in the picture.
for the spanning tree by some property (account in that case)
MATCH (p:Listing {accountId: '.......'})
CALL apoc.path.spanningTree(p, {
minLevel: 1,
maxLevel: 5
})
YIELD path
RETURN path;
Asking for direction how can I detect invalid graphs. I was thinking about taking the spanning-tree query result, and programmatically iterating the nodes and checking for the constraints. for example:
But maybe there is a simpler solution just by using query only, or another by a transformation of the graph representation to be known problem with well know algorithm (kruskal, Dijkstra, whatever).
P.S.
Thanks!
05-11-2022 04:18 AM
Hello @spaizadv
Do you have one graph by database of several graphs in the same database?
What I would do is write as many queries for each check to do, then apply those queries to each graph. I think it would be more efficient.
Regards,
Cobra
05-11-2022 08:12 AM
I agree with @Cobra. You can write individual queries to detect each of the invalid conditions you enumerated above. You can add remediate steps to each script since you will know the specific invalid condition that script is addressing.
As an example using the scenario you highlighted in your screenshot, you are looking for a node that has more than one incoming HAS_CHILD relationships. This represents a node with multiple parents.
match(n)<-[:HAS_CHILD]-(m)
with n, count(m) as noOfParents
where noOfParents > 1
return n
Based on your model, a node can also have multiple parents if it has more than one outgoing HAS_PARENT relationship, which is invalid. You can write a query to detect this relation as well.
Write a script for each invalid scenario.
05-11-2022 02:10 PM
Btw, if you want to create one query that identifies all invalid scenarios to give you a consolidated report, you can string them together using the UNION clause. The example below shows two; add as many as needed.
Call {
match(n)<-[:HAS_CHILD]-(m)
with n, count(m) as noOfParents
where noOfParents > 1
return id(n) as id , ‘multiple incoming HAS_CHILD relationships’ as cause
UNION
match(m)<-[:HAS_PARENT]-(n)
with n, count(m) as noOfParents
where noOfParents > 1
return id(n) as id , ‘multiple outgoing HAS_PARENT relationships’ as cause
}
return id, cause
05-16-2022 12:15 AM
Thanks. I could write conditions per case even without Neo4J. This is what I'm trying to avoid. There are much more "invalid" cases than "valid" cases.
In my head I looks like define some rules for valid case, and then just do A-B where A is a set of allgraphs and B is set of valid graphs.
All the sessions of the conference are now available online