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
9 changes: 6 additions & 3 deletions doc/queries/doc-pgr_breadthFirstSearch.queries
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ SELECT * FROM pgr_breadthFirstSearch(
);
seq | depth | start_vid | node | edge | cost | agg_cost
-----+-------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

--q2
SELECT * FROM pgr_breadthFirstSearch(
Expand All @@ -18,7 +19,8 @@ SELECT * FROM pgr_breadthFirstSearch(
);
seq | path_seq | start_vid | node | edge | cost | agg_cost
-----+----------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

--q3
SELECT * FROM pgr_breadthFirstSearch(
Expand All @@ -27,7 +29,8 @@ SELECT * FROM pgr_breadthFirstSearch(
);
seq | depth | start_vid | node | edge | cost | agg_cost
-----+-------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

ROLLBACK;
ROLLBACK
127 changes: 127 additions & 0 deletions include/breadthFirstSearch/pgr_breadthFirstSearch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*PGR-GNU*****************************************************************
File: pgr_breadthFirstSearch.hpp

Copyright (c) 2019 pgRouting developers
Mail: project@pgrouting.org

Copyright (c) 2019 Gudesa Venkata Sai AKhil
Mail: gvs.akhil1997@gmail.com

------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/

#ifndef INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
#define INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
#pragma once

#include <vector>

#include <visitors/edges_order_bfs_visitor.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>

#include "cpp_common/pgr_base_graph.hpp"
//******************************************

namespace pgrouting
{
namespace functions
{

template <class G>
class Pgr_breadthFirstSearch
{
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::B_G B_G;

public:


//many to depth
std::vector<pgr_mst_rt> breadthFirstSearch(
G &graph,
std::vector<int64_t> start_vertex,
int64_t depth){

// pgr_mst_rt dummy = {0, 0, 0, 0, 0.0, 0.0};
// std::vector<pgr_mst_rt> dummyResults = {dummy};
// return dummyResults;


std::vector<pgr_mst_rt> results;
using bfs_visitor = visitors::Edges_order_bfs_visitor<E>;
for (auto source : start_vertex)
{
std::vector<E> visited_order;
if (graph.has_vertex(source))
{
boost::breadth_first_search(graph.graph,
graph.get_V(source),
visitor(bfs_visitor(visited_order)));

auto single_source_results = get_results(visited_order, source, depth, graph);
results.insert(results.end(), single_source_results.begin(), single_source_results.end());
}
else
{
results.push_back({source, 0, source, -1, 0.0, 0.0});
}
}
return results;
}

//source, depth, node, edge

private:



template <typename T>
std::vector<pgr_mst_rt> get_results(
T order,
int64_t source,
int64_t max_depth,
const G &graph) {
std::vector<pgr_mst_rt> results;

std::vector<double> agg_cost(graph.num_vertices(), 0);
std::vector<int64_t> depth(graph.num_vertices(), 0);

for (const auto edge : order) {
auto u = graph.source(edge);
auto v = graph.target(edge);

agg_cost[v] = agg_cost[u] + graph[edge].cost;
depth[v] = depth[u] + 1;

if (max_depth >= depth[v]) {
results.push_back({
source,
depth[v],
graph[v].id,
graph[edge].id,
graph[edge].cost,
agg_cost[v]
});
}
}
return results;
}

};
} // namespace functions
} // namespace pgrouting

#endif // INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
2 changes: 1 addition & 1 deletion pgtap/breadthFirstSearch/breadthFirstSearch-innerQuery.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SELECT plan(45);

SELECT style_dijkstra('pgr_primBFS', ', 5)');
SELECT style_dijkstra('pgr_breadthFirstSearch', ', 5)');

SELECT finish();
ROLLBACK;
6 changes: 3 additions & 3 deletions sql/breadthFirstSearch/breadthFirstSearch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--ONE TO DEPTH
CREATE OR REPLACE FUNCTION pgr_breadthFirstSearch(
TEXT, -- edges_sql (required)
BIGINT, -- from_vids (required)
BIGINT, -- from_vid (required)

max_depth BIGINT DEFAULT 9223372036854775807,
directed BOOLEAN DEFAULT true,
Expand All @@ -50,7 +50,7 @@ CREATE OR REPLACE FUNCTION pgr_breadthFirstSearch(
RETURNS SETOF RECORD AS
$BODY$
SELECT *
FROM _pgr_breadthFirstSearch(_pgr_get_statement($1), ARRAY[$2]::BIGINT[], max_depth, directed) AS a;
FROM _pgr_breadthFirstSearch(_pgr_get_statement($1), ARRAY[$2]::BIGINT[], max_depth, directed) AS a;
$BODY$
LANGUAGE SQL VOLATILE STRICT;

Expand All @@ -64,7 +64,7 @@ CREATE OR REPLACE FUNCTION pgr_breadthFirstSearch(
directed BOOLEAN DEFAULT true,

OUT seq BIGINT,
OUT path_seq BIGINT,
OUT depth BIGINT,
OUT start_vid BIGINT,
OUT node BIGINT,
OUT edge BIGINT,
Expand Down
61 changes: 22 additions & 39 deletions src/breadthFirstSearch/breadthFirstSearch_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "cpp_common/pgr_assert.h"

//TODO(vicennial) : Remove below headers once pgr_breadthFirstSearch.hpp is implemented.
#include "cpp_common/pgr_messages.h"
#include "cpp_common/basePath_SSEC.hpp"
#include "cpp_common/pgr_base_graph.hpp"
#include "breadthFirstSearch/pgr_breadthFirstSearch.hpp"
//TODO(vicennial) : Complete file once pgr_breadthFirstSearch.hpp is implemented.
//TODO(vicennial) : Use parameter max_depth.

Expand All @@ -57,32 +55,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
directed BOOLEAN DEFAULT true,
***********************************************************/

#if 0
template < class G >
std::deque< Path >
std::vector<pgr_mst_rt>
pgr_breadthFirstSearch(
G &graph,
std::vector < int64_t > sources,
std::vector < int64_t > targets,
std::string &log,
bool only_cost = false) {
int64_t max_depth) {
std::sort(sources.begin(), sources.end());
sources.erase(
std::unique(sources.begin(), sources.end()),
sources.end());

std::sort(targets.begin(), targets.end());
targets.erase(
std::unique(targets.begin(), targets.end()),
targets.end());

Pgr_breadthFirstSearch< G > fn_breadthFirstSearch;
auto paths = fn_breadthFirstSearch.breadthFirstSearch(
graph, sources, targets, only_cost);
log += fn_breadthFirstSearch.get_log();
return paths;
pgrouting::functions::Pgr_breadthFirstSearch< G > fn_breadthFirstSearch;
auto results = fn_breadthFirstSearch.breadthFirstSearch(
graph, sources, max_depth);
return results;
}
#endif

void
do_pgr_breadthFirstSearch(
Expand Down Expand Up @@ -115,61 +104,55 @@ do_pgr_breadthFirstSearch(
std::vector<int64_t>
start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);

std::deque< Path >paths;
std::vector<pgr_mst_rt> results;
std::string logstr;
if (directed) {
log << "Working with directed Graph\n";
pgrouting::DirectedGraph digraph(gType);
digraph.insert_edges(data_edges, total_edges);

#if 0
paths = pgr_breadthFirstSearch(digraph,
results = pgr_breadthFirstSearch(
digraph,
start_vertices,
end_vertices,
logstr,
only_cost);
#endif
max_depth);

} else {
log << "Working with Undirected Graph\n";
pgrouting::UndirectedGraph undigraph(gType);
undigraph.insert_edges(data_edges, total_edges);

#if 0
paths = pgr_breadthFirstSearch(
results = pgr_breadthFirstSearch(
undigraph,
start_vertices,
end_vertices,
logstr,
only_cost);
#endif
max_depth);

}
#if 0 //TODO (vicennial): Figure out how to process return tuples
log<< logstr;
size_t count(0);
count = count_tuples(paths);

auto count = results.size();

if (count == 0) {
(*return_tuples) = NULL;
(*return_count) = 0;
notice <<
"No paths found";
"No traversal found";
*log_msg = pgr_msg(notice.str().c_str());
return;
}

(*return_tuples) = pgr_alloc(count, (*return_tuples));
log << "\nConverting a set of paths into the tuples";
(*return_count) = (collapse_paths(return_tuples, paths));
log << "\nConverting a set of traversals into the tuples";
for(size_t i = 0; i < count; i++){
*((*return_tuples) + i) = results[i];
}
(*return_count) = count;

pgassert(*err_msg == NULL);
*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*notice_msg = notice.str().empty()?
*notice_msg :
pgr_msg(notice.str().c_str());
#endif
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
Expand Down
15 changes: 9 additions & 6 deletions test/breadthFirstSearch/doc-pgr_breadthFirstSearch.result
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ SET client_min_messages TO NOTICE;
SET
--q1
SELECT * FROM pgr_breadthFirstSearch(
\'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id\',
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
2
);
seq | depth | start_vid | node | edge | cost | agg_cost
-----+-------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

--q2
SELECT * FROM pgr_breadthFirstSearch(
\'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id\',
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
ARRAY[13,2], max_depth := 3
);
seq | path_seq | start_vid | node | edge | cost | agg_cost
-----+----------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

--q3
SELECT * FROM pgr_breadthFirstSearch(
\'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id\',
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
0
);
seq | depth | start_vid | node | edge | cost | agg_cost
-----+-------+-----------+------+------+------+----------
(0 rows)
1 | 0 | 0 | 0 | 0 | 0 | 0
(1 row)

ROLLBACK;
ROLLBACK