# pgr_connectedComponents¶

pgr_connectedComponents — Componentes conectados de un grafo no dirigido mediante un enfoque basado en DFS.

• Versión 3.0.0

• Las columnas de retorno cambian:

• n_seq se elimina

• seq cambió el tipo a BIGINT

• Función oficial

• Versión 2.5.0

• Nueva función experimental

## Descripción¶

Un componente conectado de un gráfico no direccionado es un conjunto de vértices que son todos accesibles entre sí.

Las características principales son:

• Funciona para grafos no dirigidos.

• Los componentes se describen mediante vértices

• Los valores regresados se ordenan:

• component ascendente

• node ascendente

• Tiempo de ejecución: $$O(V + E)$$

## Firmas¶

pgr_connectedComponents(Edges SQL)

RETURNS SET OF (seq, component, node)
OR EMPTY SET
Ejemplo:

Los componentes conectados del grafo

SELECT * FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edges'
);
seq | component | node
-----+-----------+------
1 |         1 |    1
2 |         1 |    3
3 |         1 |    5
4 |         1 |    6
5 |         1 |    7
6 |         1 |    8
7 |         1 |    9
8 |         1 |   10
9 |         1 |   11
10 |         1 |   12
11 |         1 |   15
12 |         1 |   16
13 |         1 |   17
14 |         2 |    2
15 |         2 |    4
16 |        13 |   13
17 |        13 |   14
(17 rows)



## Parámetros¶

Parámetro

Tipo

Descripción

SQL Aristas

TEXT

SQL Aristas descritas más adelante.

## Consultas Internas¶

### SQL aristas¶

Columna

Tipo

x Defecto

Descripción

id

ENTEROS

Identificador de la arista.

source

ENTEROS

Identificador del primer vértice de la arista.

target

ENTEROS

Identificador del segundo vértice de la arista.

cost

FLOTANTES

Peso de la arista (source, target)

reverse_cost

FLOTANTES

-1

Peso de la arista (target, source)

• Cuando negativo: la arista (target, source) no existe, por lo tanto no es parte del grafo.

Donde:

ENTEROS:

SMALLINT, INTEGER, BIGINT

FLOTANTES:

SMALLINT, INTEGER, BIGINT, REAL, FLOAT

## Columnas de Resultados¶

Devuelve el conjunto de(seq, component, node)

Columna

Tipo

Descripción

seq

BIGINT

Valor secuencial a partir de 1.

component

BIGINT

• Continene le valor mínimo de identificador de nodo en el componente.

node

BIGINT

Identificador del vértice que pertenece al component.

### Conectando componentes desconectados¶

Para obtener la conectividad del grafo:

SELECT * FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edges'
);
seq | component | node
-----+-----------+------
1 |         1 |    1
2 |         1 |    3
3 |         1 |    5
4 |         1 |    6
5 |         1 |    7
6 |         1 |    8
7 |         1 |    9
8 |         1 |   10
9 |         1 |   11
10 |         1 |   12
11 |         1 |   13
12 |         1 |   14
13 |         1 |   15
14 |         1 |   16
15 |         1 |   17
16 |         1 |   18
17 |         2 |    2
18 |         2 |    4
(18 rows)



In this example, the component $$2$$ consists of vertices $$\{2, 4\}$$ and both vertices are also part of the dead end result set.

This graph needs to be connected.

Nota

With the original graph of this documentation, there would be 3 components as the crossing edge in this graph is a different component.

#### Prepare storage for connection information¶

ALTER TABLE vertices ADD COLUMN component BIGINT;
ALTER TABLE
ALTER TABLE edges ADD COLUMN component BIGINT;
ALTER TABLE


#### Save the vertices connection information¶

UPDATE vertices SET component = c.component
FROM (SELECT * FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edges'
)) AS c
WHERE id = node;
UPDATE 18


#### Save the edges connection information¶

UPDATE edges SET component = v.component
FROM (SELECT id, component FROM vertices) AS v
WHERE source = v.id;
UPDATE 20


#### Get the closest vertex¶

The closest vertex to component $$1$$ is vertex $$4$$. And the closest edge to vertex $$4$$ is edge $$14$$.

WITH
edges_sql AS (SELECT id, geom FROM edges WHERE component = 1),
point_sql AS (SELECT geom AS point FROM vertices WHERE component = 2),
results AS (
SELECT
id::BIGINT AS edge_id,
ST_LineLocatePoint(geom, point) AS fraction,
CASE WHEN ST_Intersects(ST_Buffer(geom, 2, 'side=right endcap=flat'), point)
THEN 'r'
ELSE 'l' END::CHAR AS side,
geom <-> point AS distance,
point,
ST_MakeLine(point, ST_ClosestPoint(geom, point)) AS new_line
FROM  edges_sql, point_sql
WHERE ST_DWithin(geom, point, 2)
ORDER BY geom <-> point),
prepare_cap AS (
SELECT row_number() OVER (PARTITION BY point ORDER BY point, distance) AS rn, *
FROM results),
cap AS (
SELECT edge_id, fraction, side, distance, point, new_line
FROM prepare_cap
WHERE rn <= 1
)
SELECT edge_id, fraction, side, distance, point AS geom, new_line AS edge, id AS closest_vertex
INTO closest
FROM cap JOIN vertices ON (point = geom) ORDER BY distance LIMIT 1;
SELECT 1


The edge can be used to connect the components, using the fraction information about the edge $$14$$ to split the connecting edge.

#### Conectando componentes¶

There are three basic ways to connect the components

• From the vertex to the starting point of the edge

• From the vertex to the ending point of the edge

• From the vertex to the closest vertex on the edge

• This solution requires the edge to be split.

The following query shows the three ways to connect the components:

WITH
info AS (
SELECT
edge_id, fraction, side, distance, ce.geom, edge, v.id AS closest,
source, target, capacity, reverse_capacity, e.geom AS e_geom
FROM closest AS ce
JOIN vertices AS v USING (geom)
JOIN edges AS e ON (edge_id = e.id)
ORDER BY distance LIMIT 1),
three_options AS (
SELECT
closest AS source, target, 0 AS cost, 0 AS reverse_cost,
capacity, reverse_capacity,
ST_X(geom) AS x1, ST_Y(geom) AS y1,
ST_X(ST_EndPoint(e_geom)) AS x2, ST_Y(ST_EndPoint(e_geom)) AS y2,
ST_MakeLine(geom, ST_EndPoint(e_geom)) AS geom
FROM info

UNION

SELECT closest, source, 0, 0, capacity, reverse_capacity,
ST_X(geom) AS x1, ST_Y(geom) AS y1,
ST_X(ST_StartPoint(e_geom)) AS x2, ST_Y(ST_StartPoint(e_geom)) AS y2,
ST_MakeLine(info.geom, ST_StartPoint(e_geom))
FROM info
/*
UNION
-- This option requires splitting the edge
SELECT closest, NULL, 0, 0, capacity, reverse_capacity,
ST_X(geom) AS x1, ST_Y(geom) AS y1,
ST_X(ST_EndPoint(edge)) AS x2, ST_Y(ST_EndPoint(edge)) AS y2,
edge
FROM info */
)

INSERT INTO edges
(source, target,
cost, reverse_cost,
capacity, reverse_capacity,
x1, y1, x2, y2,
geom)
(SELECT
source, target, cost, reverse_cost, capacity, reverse_capacity,
x1, y1, x2, y2, geom
FROM three_options);
INSERT 0 2


#### Checking components¶

Ignoring the edge that requires further work. The graph is now fully connected as there is only one component.

SELECT * FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edges'
);
seq | component | node
-----+-----------+------
1 |         1 |    1
2 |         1 |    2
3 |         1 |    3
4 |         1 |    4
5 |         1 |    5
6 |         1 |    6
7 |         1 |    7
8 |         1 |    8
9 |         1 |    9
10 |         1 |   10
11 |         1 |   11
12 |         1 |   12
13 |         1 |   13
14 |         1 |   14
15 |         1 |   15
16 |         1 |   16
17 |         1 |   17
18 |         1 |   18
(18 rows)



Índices y tablas