I have a graph in my PostgreSQL database, for the sake of example let's define it so:
CREATE TABLE nodes (node_id INTEGER);
CREATE TABLE roads (road_id INTEGER, nodes INTEGER[]);
INSERT INTO nodes VALUES (1), (2), (3), (4), (5);
INSERT INTO roads VALUES (1, {1, 2}), (2, {3, 4}));
I want to create SQL query that returns the number of connected components of the graph, in this example the number is 3, because nodes 1/2 are connected, 3/4 as well, while 5 is not connected to anything.
I tried searching for find&union implementations in SQL but to no avail, I then turned to CTEs but I can't do it on my own, I was thinking of something like this:
WITH RECURSIVE cc(iterator_id, node_id, rank, iterator) AS
(
SELECT row_number() OVER(), n.node_id, row_number() OVER (), 1 FROM nodes AS n
UNION ALL
# Something here that does the magic
)
SELECT
COUNT(DISTINCT rank) AS no_of_cc
FROM
cc,
(SELECT COUNT(*) FROM nodes) AS last_iterator_id
WHERE iterator = last_iterator_id;
where in each iteration we update the ranks of rows whose iterator_id <= iterator. We iterate until iterator
is equal to the biggest iterator_id
but I can't think of the recursive part.
Can you help me find the number of connected components?