Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 38 additions & 37 deletions include/dijkstra/drivingDist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,18 @@ void remove_details(const G &graph,
}
}

/** @brief gets results in form of a container of paths with depth
/** @brief gets the depth of the nodes reached from the root
*
* The depth of a node is the number of edges from the root on the predecessors tree.
* Runs in O(V): the children lists are built in one pass over the predecessors,
* then the tree is traversed breadth first from the root.
*
* @param [in] graph The graph that is being worked
* @param [in] root The starting node
* @param [in] distances An array of vertices @b id
* @param [in] predecessors an array of predecessors
* @param [in] distances aggregate cost from the root to each vertex
* @param [in,out] predecessors an array of predecessors (modified when @b details is false)
* @param [in] distance the max distance
* @param [in] details the max distance
* @param [in] details when false, points are removed from the predecessors before getting the depth
*/
template <typename G, typename V>
std::map<int64_t, int64_t> get_depth(
Expand All @@ -229,48 +233,45 @@ std::map<int64_t, int64_t> get_depth(
if (predecessors.size() != distances.size()) return depth;
depth[graph[root].id] = 0;

std::set<V> vertices;
vertices.insert(root);

if (!details) {
remove_details(graph, distances, predecessors);
}

/* cycle depth max depth can be number of nodes*/
for (int64_t d = 1; static_cast<size_t>(d) < graph.num_vertices() ; ++d) {
/*
* there is no more to search for
*/
if (vertices.empty()) break;
/*
* A node belongs to the tree when it has a predecessor and it is within the distance.
* Children of u are stored in children[first_child[u] .. first_child[u + 1])
*/
const auto n = predecessors.size();
auto in_tree = [&](V v) {return predecessors[v] != v && distances[v] <= distance;};

/*
* One next cycle these vertices have the next depth
*/
std::set<V> vertices_next;
std::set<V> point_vertices;
std::vector<size_t> first_child(n + 1, 0);
for (V v = 0; v < n; ++v) {
if (in_tree(v)) ++first_child[predecessors[v] + 1];
}
std::partial_sum(first_child.begin(), first_child.end(), first_child.begin());

std::vector<V> children(first_child[n]);
std::vector<size_t> position(first_child.begin(), first_child.end() - 1);
for (V v = 0; v < n; ++v) {
if (in_tree(v)) children[position[predecessors[v]]++] = v;
}

for (const auto v : vertices) {
/*
* Cycle predecessors looking for vertices on the depth d
* v -> p
*/
for (V p = 0; p < graph.num_vertices() ; ++p) {
/*
* Sikiping unassigned predecessors
* Sikiping distances greater than the one asked for
*/
if (predecessors[p] == p) continue;
if (!(distances[p] <= distance)) continue;
if (!(predecessors[p] == v)) continue;


/* found */
depth[graph[p].id] = d;
vertices_next.insert(p);
/*
* Breadth first traversal from the root: nodes on the next level have depth d
*/
CHECK_FOR_INTERRUPTS();
std::vector<V> level{root};
std::vector<V> next_level;
for (int64_t d = 1; !level.empty(); ++d) {
next_level.clear();
for (const auto u : level) {
for (auto i = first_child[u]; i < first_child[u + 1]; ++i) {
const auto v = children[i];
depth[graph[v].id] = d;
next_level.push_back(v);
}
}
vertices = vertices_next;
level.swap(next_level);
}
return depth;
}
Expand Down
122 changes: 122 additions & 0 deletions pgtap/dijkstra/driving_distance/edge_cases/depth.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* :file: This file is part of the pgRouting project.
:copyright: Copyright (c) 2026-2026 pgRouting developers
:license: Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 */

BEGIN;

SELECT CASE WHEN min_version('3.6.0') THEN plan(13) ELSE plan(1) END;

-- chain of 2000 vertices: 1 -> 2 -> ... -> 2000
CREATE TEMP TABLE chain AS
SELECT i AS id, i AS source, i + 1 AS target, 1.0::FLOAT AS cost, -1.0::FLOAT AS reverse_cost
FROM generate_series(1, 1999) AS i;

/*
* depth of a node is the depth of its predecessor plus one
*/
CREATE OR REPLACE FUNCTION depth_is_consistent(q TEXT, msg TEXT)
RETURNS SETOF TEXT AS
$BODY$
BEGIN
RETURN QUERY SELECT is_empty(format($$
WITH r AS (%s)
SELECT c.* FROM r AS c
LEFT JOIN r AS p ON (p.start_vid = c.start_vid AND p.node = c.pred)
WHERE (c.node = c.start_vid AND c.depth <> 0)
OR (c.node <> c.start_vid AND (p.node IS NULL OR c.depth <> p.depth + 1))$$, q),
msg);
END;
$BODY$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION test_function() RETURNS SETOF TEXT AS
$BODY$
BEGIN
IF NOT min_version('3.6.0') THEN
RETURN QUERY SELECT skip(1, 'depth column added on 3.6.0');
RETURN;
END IF;

PREPARE expected_11 AS
SELECT * FROM (VALUES
(0::BIGINT, 11::BIGINT), (1, 7), (1, 12), (1, 16), (2, 3), (2, 6), (2, 8), (2, 15), (2, 17),
(3, 1), (3, 5), (3, 9), (3, 10)) AS t(depth, node);

PREPARE dd_11 AS
SELECT depth, node FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges', 11, 3.0);

RETURN QUERY SELECT set_eq('dd_11', 'expected_11', '1: depth from 11');

PREPARE dd_11_many AS
SELECT depth, node FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[11, 5], 3.0) WHERE start_vid = 11;

RETURN QUERY SELECT set_eq('dd_11_many', 'expected_11', '2: depth from 11 when other roots are given');

RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges', 11, 3.0)$$,
'3: one root');
RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[5, 11, 16], 2.5)$$,
'4: many roots');
RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[5, 11, 16], 2.5, directed => false)$$,
'5: many roots undirected');
RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[11, 16], 3.0, equicost => true)$$,
'6: many roots equicost');
RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM edges', 6, 100)$$,
'7: whole graph');
RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_withPointsDD('SELECT id, source, target, cost, reverse_cost FROM edges',
'SELECT pid, edge_id, fraction, side FROM pointsOfInterest', ARRAY[-1, 16], 3.3, 'r', details => true)$$,
'8: withPointsDD with details');

/* without details the points are not counted on the depth */
PREPARE wp_no_details AS
SELECT depth, start_vid, node FROM pgr_withPointsDD('SELECT id, source, target, cost, reverse_cost FROM edges',
'SELECT pid, edge_id, fraction, side FROM pointsOfInterest', ARRAY[-1, 16], 3.3, 'r', details => false);
PREPARE wp_no_details_expected AS
SELECT * FROM (VALUES
(0::BIGINT, -1::BIGINT, -1::BIGINT), (1, -1, 5), (2, -1, 6), (3, -1, 7),
(0, 16, 16), (1, 16, 11), (1, 16, 15), (1, 16, 17), (2, 16, 7), (2, 16, 10), (2, 16, 12),
(3, 16, 3), (3, 16, 6), (3, 16, 8)) AS t(depth, start_vid, node);

RETURN QUERY SELECT set_eq('wp_no_details', 'wp_no_details_expected', '9: withPointsDD without details');

/* deep tree: depth is the number of edges from the root */
PREPARE chain_dd AS
SELECT depth, node FROM pgr_drivingDistance('SELECT * FROM chain', 1, 1e9);
PREPARE chain_expected AS
SELECT (i - 1)::BIGINT, i::BIGINT FROM generate_series(1, 2000) AS i;

RETURN QUERY SELECT set_eq('chain_dd', 'chain_expected', '10: depth on a chain of 2000 vertices');

PREPARE chain_dd_limited AS
SELECT depth, node FROM pgr_drivingDistance('SELECT * FROM chain', 1001, 500.5);
PREPARE chain_expected_limited AS
SELECT (i - 1001)::BIGINT, i::BIGINT FROM generate_series(1001, 1501) AS i;

RETURN QUERY SELECT set_eq('chain_dd_limited', 'chain_expected_limited', '11: depth on a chain, limited distance');

PREPARE chain_dd_undirected AS
SELECT depth, node FROM pgr_drivingDistance('SELECT * FROM chain', 1000, 2.5, directed => false);
PREPARE chain_expected_undirected AS
SELECT abs(i - 1000)::BIGINT, i::BIGINT FROM generate_series(998, 1002) AS i;

RETURN QUERY SELECT set_eq('chain_dd_undirected', 'chain_expected_undirected', '12: depth on a chain, undirected');

RETURN QUERY SELECT depth_is_consistent(
$$SELECT * FROM pgr_drivingDistance('SELECT * FROM chain', ARRAY[1, 1500], 700, equicost => true)$$,
'13: chain, equicost');
END;
$BODY$
LANGUAGE plpgsql;

SELECT * FROM test_function();
SELECT * FROM finish();
ROLLBACK;
Loading