-1

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?

MAHMUDUL
  • 1
  • 2

4 Answers4

0

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.

Ken W.
  • 397
  • 1
  • 13
0

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);
Wendel
  • 763
  • 1
  • 12
0

You can use the following guideline for the implementation.

  1. Firstly create the graph and include the vertex and edge labels.

     SELECT * FROM create_graph('my_graph');
     SELECT create_vlabel('my_graph', 'vertex');
     SELECT create_elabel('my_graph', 'distance');
    
  2. To check validity and correctness, try this 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;

0
MATCH (start:vertex)

OPTIONAL MATCH path = (start)-[:distance*]-(end:vertex)
WHERE end IS NOT NULL
LIMIT 1

RETURN CASE WHEN path IS NOT NULL THEN 'Graph is connected' ELSE 'Graph is not connected' END AS result;


MATCH (start:vertex)
OPTIONAL MATCH path = (start)-[:distance*]-(end:vertex)
WHERE end IS NOT NULL
LIMIT 1
RETURN CASE WHEN path IS NOT NULL THEN 'Graph is connected' ELSE 'Graph is not connected' END AS result;
desertnaut
  • 57,590
  • 26
  • 140
  • 166