A
A
aarifkhamdi2019-12-13 22:10:33
PostgreSQL
aarifkhamdi, 2019-12-13 22:10:33

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

2 answer(s)
I
Ivan Shumov, 2019-12-13
@inoise

There is a special type of database - graph, for example, Neo4j. You can see analogues. Gremlin or SPARQL Syntax

D
Dimonchik, 2019-12-14
@dimonchik2013

ArangoDB
or yes, Neo4j

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question