1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/* :file: This file is part of the pgRouting project.
:copyright: Copyright (c) 2018-2026 pgRouting developers
:license: Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 */
BEGIN;
SELECT plan(13);
UPDATE edges SET cost = sign(cost) + 0.001 * id * id, reverse_cost = sign(reverse_cost) + 0.001 * id * id;
--
PREPARE prim1 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges WHERE id > 18 ORDER BY id',
21, 3
);
SELECT set_eq('prim1',
$$VALUES (1, 0, 21, 21, -1, 0, 0) $$,
'1: Empty Graph -> Only root vertex is returned');
PREPARE prim2 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost
FROM edges WHERE id > 18 ORDER BY id',
ARRAY[21, 45],
3
);
SELECT set_eq('prim2',
$$VALUES
(1, 0, 21, 21, -1, 0, 0),
(2, 0, 45, 45, -1, 0, 0)
$$,
'2: Empty graph -> Only root vertices are returned');
--
PREPARE prim3 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
21, 3
);
SELECT set_eq('prim3',
$$VALUES (1, 0, 21, 21, -1, 0, 0) $$,
'3: Root not in Graph -> Only root vertex is returned');
--
PREPARE prim4 AS
SELECT seq, start_vid, depth, edge, agg_cost <= 3.5
FROM pgr_primBFS(
'SELECT id, source, target, cost
FROM edges ORDER BY id',
ARRAY[21, 15],
3
);
SELECT set_eq('prim4',
$$VALUES
(1, 15, 0, -1, true),
(2, 15, 1, 16, true),
(3, 15, 2, 9, true),
(4, 15, 3, 5, true),
(5, 15, 3, 8, true),
(6, 15, 3, 11, true),
(7, 21, 0, -1, true)
$$,
'4: Root not in Graph -> Only root vertex is returned, Root in graph -> spanning tree is returned');
--
PREPARE prim5 AS
SELECT seq, start_vid, depth, edge, depth <= 3
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
0, 3
);
SELECT set_eq('prim5',
$$VALUES
(1, 1, 0, -1, true),
(2, 1, 1, 6, true),
(3, 1, 2, 7, true),
(4, 1, 3, 4, true),
(5, 1, 3, 10, true),
(6, 2, 0, -1, true),
(7, 2, 1, 17, true),
(8, 13, 0, -1, true),
(9, 13, 1, 18, true)
$$,
'5: root = 0 -> forest (with random root vertices)');
--
PREPARE prim6 AS
SELECT *
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
4, -3
);
SELECT throws_ok('prim6',
CASE WHEN min_version('3.7.0') THEN 'XX000' ELSE 'P0001' END,
'Negative value found on ''max_depth''',
'6: Negative max_depth throws');
PREPARE prim7 AS
SELECT *
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
ARRAY[15, 8], -3
);
SELECT throws_ok('prim7',
CASE WHEN min_version('3.7.0') THEN 'XX000' ELSE 'P0001' END,
'Negative value found on ''max_depth''',
'7: Negative max_depth throws');
--
PREPARE prim8 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
15, 0
);
SELECT set_eq('prim8',
$$VALUES (1, 0, 15, 15, -1, 0, 0) $$,
'8: 0 max_depth -> Only root vertex is returned');
--
PREPARE prim9 AS
SELECT seq, depth, edge, depth <= 9223372036854775807
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
15
);
SELECT set_eq('prim9',
$$VALUES
(1, 0, -1, true),
(2, 1, 3, true),
(3, 2, 2, true),
(4, 2, 5, true),
(5, 3, 1, true),
(6, 3, 4, true),
(7, 3, 9, true),
(8, 3, 11, true),
(9, 4, 7, true),
(10, 4, 10, true),
(11, 4, 13, true),
(12, 5, 6, true),
(13, 5, 14, true)
$$,
'9: default max_depth -> complete tree from root');
--
PREPARE prim10 AS
SELECT seq, start_vid, depth, edge, depth <= 9223372036854775807
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id',
0
);
SELECT set_eq('prim10',
$$VALUES
(1, 1, 0, -1, true),
(2, 1, 1, 6, true),
(3, 1, 2, 7, true),
(4, 1, 3, 4, true),
(5, 1, 3,10, true),
(6, 1, 4, 1, true),
(7, 1, 4, 2, true),
(8, 1, 4,14, true),
(9, 1, 5, 3, true),
(10, 1, 5, 5, true),
(11, 1, 6, 9, true),
(12, 1, 6,11, true),
(13, 1, 7,13, true),
(14, 2, 0,-1, true),
(15, 2, 1,17, true),
(16, 13, 0,-1, true),
(17, 13, 1,18, true)
$$,
'10: root = 0 -> forest (with random root vertices)');
--
PREPARE prim11 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges', 18
);
SELECT set_eq('prim11', 'VALUES (1, 0, 18, 18, -1, 0, 0)', 'Vertex 18 does not exist');
--
PREPARE prim12 AS
SELECT *
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ORDER BY id', 4)
WHERE cost < 0;
SELECT is_empty('prim12', 'No cost can be negative');
--
PREPARE prim13 AS
SELECT seq, depth, start_vid, node, edge, cost, agg_cost
FROM pgr_primBFS(
'SELECT id, source, target, cost, reverse_cost
FROM edges ', 13
);
SELECT set_eq('prim13',
'VALUES
(1, 0, 13, 13, -1, 0, 0),
(2, 1, 13, 14, 18, 1.324, 1.324)' , 'Compare when node is 16 with expected result');
SELECT * FROM finish();
ROLLBACK;
|