I am trying to detect cycles in a graph created on postgreSQl and Apache AGE.
The schema for the graph is as follows:
CREATE TABLE modules (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
version VARCHAR(255) NOT NULL
);
CREATE TABLE dependencies (
module_id INTEGER REFERENCES modules(id),
dependency_id INTEGER REFERENCES modules(id),
PRIMARY KEY (module_id, dependency_id)
);
and the sample data is given by
-- Modules
INSERT INTO modules (name, version) VALUES
('Module A', '1.0.0'),
('Module B', '1.1.0'),
('Module C', '1.2.0'),
('Module D', '2.0.0'),
('Module E', '2.1.0');
-- Dependencies
INSERT INTO dependencies (module_id, dependency_id) VALUES
(1, 2),
(1, 3),
(2, 3),
(3, 4),
(4, 5),
(5, 1);
Can someone guide me what the cypher query would be to detect cycles within the graph and also the nodes involved in the cycle.
3
Answers
The code for that would be something like:
You can try to experiment with the following query to detect cycles per your defined schema:
You can try working out with "WITH RECURSIVE" statement. Here is the official documentation of it.
Other possible way is to proceed with a custom function. Here is how you can create a custom function.