skip to Main Content

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


  1. 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) and WHERE end IS NULL as the end token will be interpreted as the END 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:

    SELECT *
    FROM cypher('graph_name', $$                                                                                                
        MATCH path = (start:vertex)-[:distance*]->(finish:vertex)                                                                             
        WITH path                                                                                                                           
        RETURN CASE WHEN path IS NULL THEN 'Graph is not connected' ELSE 'Graph is connected' END AS result
    $$) AS (v agtype);
    

    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.

    Login or Signup to reply.
  2. 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:

    SELECT * FROM cypher('graph_name', $$
        MATCH (v:vertex)
        RETURN COUNT(v)
    $$) AS (total_vertices agtype);
    

    Now, you use this information to build a query that checks connectivity, let’s say we’ve 10 vertices:

    SELECT * FROM cypher('graph_name', $$                                                                                                
        MATCH path = (start:vertex)-[*9..]->(finish:vertex)                                                                             
        WITH path                                                                                                                           
        RETURN CASE WHEN path IS NULL THEN 'Graph is not connected' ELSE 'Graph is connected' END
    $$) AS (result agtype);
    

    This way, we know that every vertex has an edge with its neighbor. You can try this dynamically too:

    SELECT * FROM cypher('graph_name', $$
        MATCH (v:vertex)
        WITH count(v) AS total_vertices
        MATCH path = (start:vertex)-[*(total_vertices-1)..]->(finish:vertex)                                                                             
        WITH path                                                                                                                           
        RETURN CASE WHEN path IS NULL THEN 'Graph is not connected' ELSE 'Graph is connected' END
    $$) AS (result agtype);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search