0

I am trying to detect cycles in a graph created on postgreSQl and Apache AGE using WITH RECURSIVE method. Can someone guide me why my cypher query would not be able to detect cycles? I have using this reference. Below is my trying code:

WITH RECURSIVE dependency_path AS (
  SELECT module_id, dependency_id, ARRAY[module_id] AS path
  FROM dependencies
  UNION ALL
  SELECT dp.module_id, d.dependency_id, path || dp.module_id
  FROM dependency_path dp, dependencies d
  WHERE dp.dependency_id = d.module_id
  AND NOT (dp.path @> ARRAY[d.dependency_id])
)
SELECT path || dependency_id AS cycle
FROM dependency_path
WHERE module_id = dependency_id
AND path @> ARRAY[dependency_id];
MAHMUDUL
  • 1
  • 2

7 Answers7

1

You can use a specific query designed for this purpose to make it easier to identify cycles inside the graph:

Start by matching "Person" nodes that are related by "relationship" edges. Give one of these nodes the context label "a" and another "b." The query then determines whether node "a" and node "b" are the same. This is a crucial criterion. If this is the case, the query will return pertinent data that represent this equivalence.

It's crucial to remember that the aforementioned query is intended to include both instances when a node directly connects to its own entity as well as direct edges leading from a node to itself.

A specific query setting is helpful if your goal is to identify cycles that include at least two nodes:

Start the search by locating nodes with the label "Person." The "relationship" edge then defines a variable-length path that connects these nodes. The stated path length in this case is between 2 and any greater value. The query again evaluates the equivalency between nodes "a" and "b," and in cases when this equivalence is confirmed, it indicates the existence of a cycle.

Raja Rakshak
  • 168
  • 2
0

The point in code from should be changed to join

FROM dependency_path dp, dependencies d

should be changed to

JOIN dependencies d ON dp.dependency_id = d.module_id

To ensure that the join is properly performed. and the next line should be changed from

AND NOT (dp.path @> ARRAY[d.dependency_id])

to

WHERE NOT (dp.path @> ARRAY[d.dependency_id])

This thing will make sure the condition is applied correctly. With these changes the recursive function to detect cycles in graph should work.

Talha Munir
  • 121
  • 5
0

In order to detect the cycle, use this code

MATCH path=(n)-[*]->(n)
RETURN path
0

For cycles detection, this customized query will work:

Match (a:Person)-[:relationship]-(b:Person)
where a=b

This query first takes the a as some person vertex and then b as person vertex and then check whether the a and b are same and if its same then it will return the values if its same.

The above query will also return the nodes in which there is a direct edge from a node to its own.

For having a cycle that has at least two nodes, we can use the following query.

Match (a:Person)-[:relationship*2..]-(b:Person)
where a=b
0

You can use WITH RECURSIVE method to detect cycles, you can modify your query to ensure that the conditions and joins are applied correctly.

WITH RECURSIVE dependency_path AS (
  SELECT module_id, dependency_id, ARRAY[module_id] AS path
  FROM dependencies
  UNION ALL
  SELECT dp.module_id, d.dependency_id, path || dp.module_id
  FROM dependency_path dp
  JOIN dependencies d ON dp.dependency_id = d.module_id
  WHERE NOT (dp.path @> ARRAY[d.dependency_id])
)
SELECT path || dependency_id AS cycle
FROM dependency_path
WHERE module_id = dependency_id
AND path @> ARRAY[dependency_id];
Abdul Manan
  • 117
  • 5
0

Your query is almost correct you just need to use JOIN on dp.dependency_id and d.module_id and then filter then the results using WHERE clause

JOIN dependencies d ON dp.dependency_id = d.module_id 
WHERE NOT (dp.path @> ARRAY[d.dependency_id])
adil shahid
  • 125
  • 4
0

Try this piece of code, might work for you.

WITH RECURSIVE dependency_path AS (
  SELECT module_id, dependency_id, ARRAY[module_id, dependency_id] AS path
  FROM dependencies
  UNION ALL
  SELECT dp.module_id, d.dependency_id, path || d.dependency_id
  FROM dependency_path dp
  JOIN dependencies d ON dp.dependency_id = d.module_id
  WHERE NOT dp.path @> ARRAY[d.dependency_id]
)
SELECT path AS cycle
FROM dependency_path
WHERE module_id = dependency_id;
helvete
  • 2,455
  • 13
  • 33
  • 37