pgRouting Manual (2.2)

pgr_vidsToDMatrix

«  pgr_vidsToDMatrix   ::   Contents   ::   Contraction  »

pgr_vidsToDMatrix

Name

pgr_vidsToDMatrix - Creates a distances matrix from an array of vertex_id.

Warning

This is a proposed function

  • Is not officially in the release.
  • Name could change.
  • Signature could change.
  • Needs testing.
  • Functionality could change.

Synopsis

This function takes an array of vertex_id, a sql statement to select the edges, and some boolean arguments to control the behavior. It then computes kdijkstra() distances for each vertex to all the other vertices and creates a distance matrix suitable for TSP.

The function returns:

  • dmatrix:float8[] - the distance matrix suitable to pass to pgr_TSP() function.
pgr_vidsToDMatrix(IN sql text, IN vids integer[], IN directed boolean, IN has_reverse_cost boolean, IN want_symmetric boolean, OUT dmatrix double precision[])

Description

Paramters

sql:text - A SQL statement to select the edges needed for the solution.
vids:integer[] - An array of vertex_id.
directed:boolean - A flag to indicate if the graph is directed.
has_reverse_cost:
 boolean - A flag to indicate if the SQL has a column reverse_cost.
want_symmetric:boolean - A flag to indicate if you want a symmetric or asymmetric matrix. You will need a symmetric matrix for pgr_TSP(). If the matriix is asymmetric, the then the cell(i,j) and cell(j,i) will be set to the average of those two cells except if one or the other are -1.0 then it will take the value of the other cell. If both are negative they will be left alone.

Warning

  • kdijkstra() can fail to find a path between some of the vertex ids. We to not detect this other than the cost might get set to -1.0, so the dmatrix shoule be checked for this as it makes it invalid for TSP

History

  • Proposed in version 2.1.0

Examples

SELECT * FROM pgr_vidsToDMatrix(
    'SELECT id::INTEGER, source::INTEGER, target::INTEGER, cost, reverse_cost FROM edge_table',
    array[1,2,3,5],
    true, true, false);
             pgr_vidstodmatrix             
-------------------------------------------
 {{0,1,6,2},{1,0,5,1},{2,1,0,2},{2,1,4,0}}
(1 row)

SELECT * FROM pgr_vidsToDMatrix(
    'SELECT id::INTEGER, source::INTEGER, target::INTEGER, cost, reverse_cost FROM edge_table',
    array[1,2,3,5],
    true, true, true);
             pgr_vidstodmatrix             
-------------------------------------------
 {{0,1,4,2},{1,0,3,1},{4,3,0,3},{2,1,3,0}}
(1 row)

This example shows how this can be used in the context of feeding the results into pgr_tsp() function.

SELECT * FROM pgr_tsp(
    (SELECT pgr_vidsToDMatrix(
        'SELECT id::INTEGER, source::INTEGER, target::INTEGER, cost, reverse_cost FROM edge_table',
        array[1,2,3,5],
        true, true, true)
    ),
    1
);
 seq | id 
-----+----
   0 |  1
   1 |  2
   2 |  3
   3 |  0
(4 rows)

This example uses the Sample Data network.

See Also

Indices and tables

«  pgr_vidsToDMatrix   ::   Contents   ::   Contraction  »