How can we check connectedness of a graph in Apache AGE?
If nodes are labeled as "vertex" and edges labeled as "distance," Then, Here’s my Cypher query:
MATCH (start:vertex)
OPTIONAL MATCH path = (start)-[:distance*]-(end:vertex)
WHERE end IS NULL
RETURN CASE WHEN path IS NULL THEN 'Graph is not connected' ELSE 'Graph is connected' END AS result;
In this query, the MATCH clause matches the starting node labeled as "vertex" ((start:vertex)). The OPTIONAL MATCH clause then attempts to find a path (path = (start)-[:distance*]-(end:vertex)) from the starting vertex to any other vertex in the graph using the "distance" relationship.
Could anyone give me complete guidelines on implementation?
2
Answers
A graph’s correctness is challenging to check through automation, so I suggest just doing it based on intuition. If you understand the data enough, you should be able to know what to expect from the output. Using AGE Viewer can also be useful to visualize your data to see the relationships.
As for your code, an error will occur at
(end:vertex)
andWHERE end IS NULL
as theend
token will be interpreted as theEND
command, so you will have to rename the alias into something else. Although I doubt it will be able to pass what you are looking for, judging by the clause choices. Something like this might be what you are looking for:There are several guides you can find online, including the AGE master documentation or even the Neo4j cypher documentation for more specific and complicated queries (although note that AGE has only limited functionalities compared to Neo4j’s cypher language). If you are specifically trying to find paths from your data, there have been questions asked that are closely related, such as this one.
If the graph is connected you can use the N as the total number of vertices, then from the start vertex to the end vertex you need N-1 edges between them. So, basically you first get the number of vertices of the graph:
Now, you use this information to build a query that checks connectivity, let’s say we’ve 10 vertices:
This way, we know that every vertex has an edge with its neighbor. You can try this dynamically too: