pgr_aStarCostMatrix¶
pgr_aStarCostMatrix
 Calculates the a cost matrix using pgr_aStar.
Availability
Version 3.0.0
Official function
Version 2.4.0
New proposed function
Support
Description¶
The main characteristics are:
Using internaly the pgr_aStar algorithm
Returns a cost matrix.
No ordering is performed
let v and u are nodes on the graph:
when there is no path from v to u:
no corresponding row is returned
cost from v to u is \(\inf\)
when \(v = u\) then
no corresponding row is returned
cost from v to u is \(0\)
When the graph is undirected the cost matrix is symmetric
Signatures¶
Summary
pgr_aStarCostMatrix(edges_sql, vids [, directed] [, heuristic] [, factor] [, epsilon])
RETURNS SET OF (start_vid, end_vid, agg_cost)
Using defaults
pgr_aStarCostMatrix(edges_sql, vids)
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example
Cost matrix for vertices \(\{1, 2, 3, 4\}\) on a directed graph
SELECT * FROM pgr_aStarCostMatrix(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edge_table',
(SELECT array_agg(id) FROM edge_table_vertices_pgr WHERE id < 5)
);
start_vid  end_vid  agg_cost
++
1  2  1
1  3  6
1  4  5
2  1  1
2  3  5
2  4  4
3  1  2
3  2  1
3  4  3
4  1  3
4  2  2
4  3  1
(12 rows)
Complete Signature¶
pgr_aStarCostMatrix(edges_sql, vids, [, directed] [, heuristic] [, factor] [, epsilon])
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example
Symmetric cost matrix for vertices \(\{1, 2, 3, 4\}\) on an undirected graph using heuristic \(2\)
SELECT * FROM pgr_aStarCostMatrix(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edge_table',
(SELECT array_agg(id) FROM edge_table_vertices_pgr WHERE id < 5),
directed := false, heuristic := 2
);
start_vid  end_vid  agg_cost
++
1  2  1
1  3  2
1  4  3
2  1  1
2  3  1
2  4  2
3  1  2
3  2  1
3  4  1
4  1  3
4  2  2
4  3  1
(12 rows)
Parameters¶
Parameter 
Type 
Description 

edges_sql 

edges_sql inner query. 
vids 

Array of vertices identifiers. 
Optional Parameters¶
Parameter 
Type 
Default 
Description 

directed 



heuristic 


Heuristic number. Current valid values 0~5. Default

factor 


For units manipulation. \(factor > 0\). See Factor 
epsilon 


For less restricted results. \(epsilon >= 1\). 
Inner query¶
edges_sql¶
 edges_sql
an SQL query, which should return a set of rows with the following columns:
Column 
Type 
Default 
Description 

id 

Identifier of the edge. 

source 

Identifier of the first end point vertex of the edge. 

target 

Identifier of the second end point vertex of the edge. 

cost 

Weight of the edge (source, target)


reverse_cost 

1 
Weight of the edge (target, source),

x1 

X coordinate of source vertex. 

y1 

Y coordinate of source vertex. 

x2 

X coordinate of target vertex. 

y2 

Y coordinate of target vertex. 
Where:
 ANYINTEGER
SMALLINT, INTEGER, BIGINT
 ANYNUMERICAL
SMALLINT, INTEGER, BIGINT, REAL, FLOAT
Result Columns¶
Returns SET OF (start_vid, end_vid, agg_cost)
Column 
Type 
Description 

start_vid 

Identifier of the starting vertex. 
end_vid 

Identifier of the ending vertex. 
agg_cost 

Aggregate cost from 
Additional Examples¶
 Example
Use with pgr_TSP
SELECT * FROM pgr_TSP(
$$
SELECT * FROM pgr_aStarCostMatrix(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edge_table',
(SELECT array_agg(id) FROM edge_table_vertices_pgr WHERE id < 5),
directed:= false, heuristic := 2
)
$$,
randomize := false
);
seq  node  cost  agg_cost
+++
1  1  1  0
2  2  1  1
3  3  1  2
4  4  3  3
5  1  0  6
(5 rows)