Answer the question
In order to leave comments, you need to log in
What are efficient ways to find a connected graph?
We need to find a connected graph. Interested in implementations better than this one. And how are these issues generally handled?
What confuses: the speed of work with a large table and more or less large graphs (200-300).
Special databases, a different storage method, any possible tricks are also considered.
CREATE TABLE "UserMachine" (
"userId" int,
"machineId" int
);
INSERT INTO "UserMachine"
("userId", "machineId")
VALUES(1, 1), (2,2), (2,3), (1,2), (3,3), (1,4), (3,4), (4,5), (5,6);
WITH RECURSIVE find_machines AS (
SELECT "machineId", "userId"
FROM "UserMachine"
WHERE "userId" = 1
UNION
SELECT new."machineId", new."userId"
FROM find_machines AS old
JOIN "UserMachine" AS NEW
ON (
(OLD."machineId" = NEW."machineId")
OR (OLD."userId" = NEW."userId")
)
)
SELECT * FROM find_machines
Answer the question
In order to leave comments, you need to log in
There is a special type of database - graph, for example, Neo4j. You can see analogues. Gremlin or SPARQL Syntax
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question