Skip to content

Commit

Permalink
Merge pull request #130 from stdgraph/bfs
Browse files Browse the repository at this point in the history
Move to graph namespace
  • Loading branch information
pratzl authored Sep 2, 2024
2 parents 0e75c30 + 3f0ac1b commit 300bd9b
Show file tree
Hide file tree
Showing 80 changed files with 2,512 additions and 1,884 deletions.
4 changes: 2 additions & 2 deletions ToDo.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
- [x] cancel() CPO? bfs, dfs: no, member only
- [x] replace is_undirected_edge_v<G> & undirected_incidence_graph<G> with is_unordered_edge_v<G>
- [ ] Views
- [x] view classes should be in std::graph
- [x] view functions should be in std::graph::view
- [x] view classes should be in graph
- [x] view functions should be in graph::view
- [ ] add range overloads to appropriate views (DFS, BFS, topo_sort, etc.)
- [ ] vertexlist
- [ ] Use VVF&& instead of const VVF&
Expand Down
6 changes: 3 additions & 3 deletions benchmark/mm_bench_dijkstra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ using std::endl;
using fmt::print;
using fmt::println;

using namespace std::graph;
using namespace std::graph::experimental;
using namespace graph;
using namespace graph::experimental;

struct bench_results {
size_t vertices_discovered = 0;
Expand All @@ -63,7 +63,7 @@ std::string current_timestamp() {
template <typename Distance>
size_t vertices_visited(const std::vector<Distance>& distances) {
size_t visited = std::accumulate(distances.begin(), distances.end(), 0ULL, [](size_t count, Distance dist) {
return (dist != shortest_path_invalid_distance<Distance>()) ? count + 1 : count;
return (dist != shortest_path_infinite_distance<Distance>()) ? count + 1 : count;
});
//fmt::println("{:L} vertices were actually visited", visited);
return visited;
Expand Down
18 changes: 9 additions & 9 deletions benchmark/mm_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ using std::convertible_to;
using std::ranges::forward_range;
using std::ranges::random_access_range;
using std::ranges::range_value_t;
using std::graph::adjacency_list;
using std::graph::container::compressed_graph;
using graph::adjacency_list;
using graph::container::compressed_graph;
namespace fmm = fast_matrix_market;

template <typename T>
Expand Down Expand Up @@ -287,14 +287,14 @@ struct graph_stats {
size_t max_degree = 0;
size_t self_loops_removed = 0;

template <std::graph::adjacency_list G>
template <graph::adjacency_list G>
graph_stats(const G& g, size_t self_loops = 0)
: vertex_count(std::graph::num_vertices(g))
, edge_count(static_cast<size_t>(std::graph::num_edges(g)))
: vertex_count(graph::num_vertices(g))
, edge_count(static_cast<size_t>(graph::num_edges(g)))
, self_loops_removed(self_loops) {
for (auto&& u : std::graph::vertices(g)) {
min_degree = std::min(min_degree, size(std::graph::edges(g, u)));
max_degree = std::max(max_degree, size(std::graph::edges(g, u)));
for (auto&& u : graph::vertices(g)) {
min_degree = std::min(min_degree, size(graph::edges(g, u)));
max_degree = std::max(max_degree, size(graph::edges(g, u)));
}
}
};
Expand Down Expand Up @@ -353,7 +353,7 @@ template <typename EV, typename VV, typename GV, integral VId, integral EIndex =
auto zip_view = std::views::zip(triplet.rows, triplet.cols, triplet.vals);
using zip_value = std::ranges::range_value_t<decltype(zip_view)>;

using edge_desc = std::graph::copyable_edge_t<VId, EV>;
using edge_desc = graph::copyable_edge_t<VId, EV>;
auto edge_proj = [](const zip_value& val) -> edge_desc {
return edge_desc{std::get<0>(val), std::get<1>(val), std::get<2>(val)};
};
Expand Down
2 changes: 1 addition & 1 deletion benchmark/mm_load_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using std::tuple;
using std::vector;

using namespace std::graph::container;
using namespace graph::container;

// Dataset: gap_twitter, symmetry_type::general, 1,468,364,884 rows
// Deb/Rel parallel_ok num_threads Read Rows/Sec LoadSimple Edges/Sec LoadCompressed Edges/Sec
Expand Down
2 changes: 1 addition & 1 deletion benchmark/nwgraph_dijkstra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
template <adjacency_list Graph, class Weight, class Sources>
static auto nwgraph_dijkstra(
Graph&& graph, const Sources& sources, Weight weight = [](auto& e) -> auto& { return std::get<1>(e); }) {
using namespace std::graph;
using namespace graph;
using vertex_id_type = vertex_id_t<Graph>;

using distance_t = int64_t;
Expand Down
4 changes: 2 additions & 2 deletions docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
#html_theme = 'sphinx13'
html_theme = 'sphinx_rtd_theme'
# html_theme = 'sphinx_book_theme'
html_title = "std::graph Documentation"
html_title = "graph Documentation"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand All @@ -114,7 +114,7 @@

# -- Options for the C++ Domain ----------------------------------------------

cpp_index_common_prefix = ['std::', 'std::graph::']
cpp_index_common_prefix = ['std::', 'graph::']


# -- Options for Breathe -----------------------------------------------------
Expand Down
28 changes: 14 additions & 14 deletions docs/sphinx/graph-v2-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ Graph-V2 API Reference
Concepts
--------

.. doxygenconcept:: std::graph::vertex_range
.. doxygenconcept:: graph::vertex_range

.. doxygenconcept:: std::graph::targeted_edge
.. doxygenconcept:: graph::targeted_edge

.. doxygenconcept:: std::graph::sourced_edge
.. doxygenconcept:: graph::sourced_edge

.. doxygenconcept:: std::graph::adjacency_list
.. doxygenconcept:: graph::adjacency_list

.. doxygenconcept:: std::graph::sourced_adjacency_list
.. doxygenconcept:: graph::sourced_adjacency_list

.. doxygenconcept:: std::graph::has_degree
.. doxygenconcept:: graph::has_degree

.. doxygenconcept:: std::graph::has_find_vertex
.. doxygenconcept:: graph::has_find_vertex

.. doxygenconcept:: std::graph::has_contains_edge
.. doxygenconcept:: graph::has_contains_edge

.. doxygenconcept:: std::graph::ordered_edge
.. doxygenconcept:: graph::ordered_edge

.. doxygenconcept:: std::graph::unordered_edge
.. doxygenconcept:: graph::unordered_edge


--------------------------------
Expand All @@ -37,13 +37,13 @@ Concepts
Algorithms
----------

.. doxygenfunction:: std::graph::dijkstra_shortest_distances
.. doxygenfunction:: graph::dijkstra_shortest_distances

.. doxygenfunction:: std::graph::dijkstra_shortest_paths
.. doxygenfunction:: graph::dijkstra_shortest_paths

.. doxygenfunction:: std::graph::bellman_ford_shortest_distances
.. doxygenfunction:: graph::bellman_ford_shortest_distances

.. doxygenfunction:: std::graph::bellman_ford_shortest_paths
.. doxygenfunction:: graph::bellman_ford_shortest_paths

--------------------------------

Expand Down
2 changes: 1 addition & 1 deletion example/CppCon2021/examples/bacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main() {
std::vector<size_t> bacon_number(size(actors));

// for (auto&& [u, v] : bfs_edge_range(costars, 1)) {
for (auto&& [u, v, uv] : std::graph::views::sourced_edges_breadth_first_search(costars, 1)) {
for (auto&& [u, v, uv] : graph::views::sourced_edges_breadth_first_search(costars, 1)) {
bacon_number[v] = bacon_number[u] + 1;
}

Expand Down
10 changes: 5 additions & 5 deletions example/CppCon2021/examples/graphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main() {
*/
std::vector<std::vector<size_t>> G(34);
push_back_plain_fill(karate_index_edge_list, G, false, 0);
static_assert(std::graph::adjacency_list<decltype(G)>);
static_assert(graph::adjacency_list<decltype(G)>);

std::vector<std::list<std::tuple<size_t>>> H(34);
push_back_plain_fill(karate_index_edge_list, H, false, 0);
Expand Down Expand Up @@ -79,16 +79,16 @@ int main() {
auto s = make_property_graph(spice_vertices, spice_edges_values);

//bfs_edge_range(n, 1);
std::graph::views::edges_breadth_first_search(n, 1);
graph::views::edges_breadth_first_search(n, 1);

//bfs_edge_range(o, 1);
std::graph::views::edges_breadth_first_search(o, 1);
graph::views::edges_breadth_first_search(o, 1);

//bfs_edge_range(p, 1);
std::graph::views::edges_breadth_first_search(p, 1);
graph::views::edges_breadth_first_search(p, 1);

//bfs_edge_range(q, 0);
std::graph::views::edges_breadth_first_search(q, 0);
graph::views::edges_breadth_first_search(q, 0);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions example/CppCon2021/examples/imdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ int main() {
std::vector<size_t> together_in(L.size());

//for (auto&& [u, v, k] : bfs_edge_range(L, kevin_bacon)) {
auto kprop = [&L](auto&& e) { return std::get<1>(std::graph::edge_value(L, e)); };
for (auto&& [u, v, uv, k] : std::graph::views::sourced_edges_breadth_first_search(L, kevin_bacon, kprop)) {
auto kprop = [&L](auto&& e) { return std::get<1>(graph::edge_value(L, e)); };
for (auto&& [u, v, uv, k] : graph::views::sourced_edges_breadth_first_search(L, kevin_bacon, kprop)) {
distance[v] = distance[u] + 1;
parents[v] = u;
together_in[v] = k;
Expand Down
26 changes: 13 additions & 13 deletions example/CppCon2021/examples/ospf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

int main() {

static_assert(std::graph::adjacency_list<decltype(ospf_index_adjacency_list)>);
static_assert(graph::adjacency_list<decltype(ospf_index_adjacency_list)>);

//auto d = dijkstra(ospf_index_adjacency_list, 5UL);
std::vector<size_t> d(size(ospf_index_adjacency_list));
std::vector<size_t> p(size(ospf_index_adjacency_list));
std::graph::init_shortest_paths(d, p);
std::graph::dijkstra_shortest_paths(ospf_index_adjacency_list, 5UL, d, p, [](auto&& ee) { return std::get<1>(ee); });
graph::init_shortest_paths(d, p);
graph::dijkstra_shortest_paths(ospf_index_adjacency_list, 5UL, d, p, [](auto&& ee) { return std::get<1>(ee); });

std::cout << "----------------" << std::endl;
std::cout << "Contents of ospf_index_adjacency_list (the correct answer)" << std::endl;
Expand All @@ -42,13 +42,13 @@ int main() {
auto I = make_property_graph<decltype(ospf_vertices), decltype(ospf_edges),
std::vector<std::vector<std::tuple<size_t, size_t>>>>(ospf_vertices, ospf_edges, true);

static_assert(std::graph::adjacency_list<decltype(G)>);
static_assert(graph::adjacency_list<decltype(G)>);

//auto e = dijkstra(G, 5UL, [](auto&& ee) { return std::get<1>(ee); });
p.resize(std::graph::num_vertices(G));
std::vector<size_t> e(std::graph::num_vertices(G));
std::graph::init_shortest_paths(e, p);
std::graph::dijkstra_shortest_paths(G, 5UL, e, p, [](auto&& ee) { return std::get<1>(ee); });
p.resize(graph::num_vertices(G));
std::vector<size_t> e(graph::num_vertices(G));
graph::init_shortest_paths(e, p);
graph::dijkstra_shortest_paths(G, 5UL, e, p, [](auto&& ee) { return std::get<1>(ee); });

for (size_t i = 0; i < size(ospf_vertices); ++i) {
std::cout << std::setw(6) << ospf_vertices[i] << std::setw(6) << e[i] << std::endl;
Expand All @@ -59,13 +59,13 @@ int main() {

auto J = make_index_graph(ospf_vertices, ospf_edges, true);

//std::graph::dijkstra_shortest_paths(G, 5UL, e, p, w);
//graph::dijkstra_shortest_paths(G, 5UL, e, p, w);

//auto f = dijkstra(J, 5, [](auto&& ee) { return std::get<2>(ospf_edges[std::get<1>(ee)]); });
p.resize(std::graph::num_vertices(G));
std::vector<size_t> f(std::graph::num_vertices(G));
std::graph::init_shortest_paths(f, p);
std::graph::dijkstra_shortest_paths(J, 5UL, f, p, [](auto&& ee) { return std::get<2>(ospf_edges[std::get<1>(ee)]); });
p.resize(graph::num_vertices(G));
std::vector<size_t> f(graph::num_vertices(G));
graph::init_shortest_paths(f, p);
graph::dijkstra_shortest_paths(J, 5UL, f, p, [](auto&& ee) { return std::get<2>(ospf_edges[std::get<1>(ee)]); });

for (size_t i = 0; i < size(ospf_vertices); ++i) {
std::cout << std::setw(6) << ospf_vertices[i] << std::setw(6) << e[i] << std::endl;
Expand Down
10 changes: 5 additions & 5 deletions example/CppCon2021/include/bfs_edge_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

enum three_colors { black, white, grey };

template <typename Graph, typename Queue = std::queue<std::graph::vertex_id_t<Graph>>>
requires std::graph::adjacency_list<Graph>
template <typename Graph, typename Queue = std::queue<graph::vertex_id_t<Graph>>>
requires graph::adjacency_list<Graph>
class bfs_edge_range {
private:
using vertex_id_type = std::graph::vertex_id_t<Graph>;
using vertex_id_type = graph::vertex_id_t<Graph>;

public:
explicit bfs_edge_range(Graph& graph, vertex_id_type seed = 0) : the_graph_(graph), visited_(graph.size(), false) {
Expand All @@ -39,7 +39,7 @@ class bfs_edge_range {
typename Graph::iterator G;
vertex_id_type v_;
//typename inner_range_t<Graph>::iterator u_begin, u_end;
std::graph::vertex_iterator_t<Graph> u_begin, u_end;
graph::vertex_iterator_t<Graph> u_begin, u_end;

public:
bfs_edge_range_iterator(bfs_edge_range<Graph, Queue>& range)
Expand Down Expand Up @@ -106,7 +106,7 @@ class bfs_edge_range {
};

template <typename Graph, typename PriorityQueue>
requires std::graph::adjacency_list<Graph>
requires graph::adjacency_list<Graph>
class bfs_edge_range_2 {
private:
using vertex_id_type = typename Graph::vertex_id_type;
Expand Down
2 changes: 1 addition & 1 deletion example/CppCon2021/include/dijkstra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <vector>

template <adjacency_list Graph,
std::invocable<inner_value_t<Graph>> WeightFunction =
invocable<inner_value_t<Graph>> WeightFunction =
std::function<std::tuple_element_t<1, inner_value_t<Graph>>(const inner_value_t<Graph>&)>>
auto dijkstra(
const Graph& graph, vertex_id_t<Graph> source, WeightFunction weights = [](const inner_value_t<Graph>& e) {
Expand Down
16 changes: 8 additions & 8 deletions example/CppCon2021/include/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ auto make_index_edges(M& map, const E& edges) {
*/
template <std::ranges::random_access_range V,
std::ranges::random_access_range E,
std::graph::basic_adjacency_list Graph = std::vector<std::vector<size_t>>>
graph::basic_adjacency_list Graph = std::vector<std::vector<size_t>>>
auto make_plain_graph(const V& vertices, const E& edges, bool directed = true, size_t idx = 0) {
auto vertex_map = make_index_map(vertices);
auto index_edges = make_plain_edges(vertex_map, edges);
Expand All @@ -167,7 +167,7 @@ auto make_plain_graph(const V& vertices, const E& edges, bool directed = true, s
*/
template <std::ranges::random_access_range V,
std::ranges::random_access_range E,
std::graph::basic_adjacency_list Graph = std::vector<std::vector<std::tuple<size_t, size_t>>>>
graph::basic_adjacency_list Graph = std::vector<std::vector<std::tuple<size_t, size_t>>>>
auto make_index_graph(const V& vertices, const E& edges, bool directed = true, size_t idx = 0) {

auto vertex_map = make_index_map(vertices);
Expand All @@ -185,7 +185,7 @@ auto make_index_graph(const V& vertices, const E& edges, bool directed = true, s
*/
template <std::ranges::random_access_range V,
std::ranges::forward_range E,
std::graph::basic_adjacency_list Graph =
graph::basic_adjacency_list Graph =
std::vector<std::vector<decltype(std::tuple_cat(std::make_tuple(size_t{}), props(*(begin(E{})))))>>>
auto make_property_graph(const V& vertices, const E& edges, bool directed = true, size_t idx = 0) {

Expand Down Expand Up @@ -226,7 +226,7 @@ auto data_to_graph_edge_list(const V& left_vertices, const V& right_vertices, co
template <std::ranges::random_access_range V1,
std::ranges::random_access_range V2,
std::ranges::random_access_range E,
std::graph::basic_adjacency_list Graph =
graph::basic_adjacency_list Graph =
std::vector<std::vector<decltype(std::tuple_cat(std::make_tuple(size_t{}), props(*(begin(E{})))))>>>
auto make_plain_bipartite_graph(const V1& left_vertices, const V2& right_vertices, const E& edges, size_t idx = 0) {

Expand Down Expand Up @@ -273,7 +273,7 @@ auto make_bipartite_graph(const V& left_vertices, const V& right_vertices, const

template <std::ranges::random_access_range V,
std::ranges::random_access_range E,
std::graph::basic_adjacency_list Graph =
graph::basic_adjacency_list Graph =
std::vector<std::vector<decltype(std::tuple_cat(std::make_tuple(size_t{}), props(*(begin(E{})))))>>>
auto make_bipartite_graphs(const V& left_vertices, const V& right_vertices, const E& edges) {

Expand All @@ -295,9 +295,9 @@ auto join(const Graph1& G, const Graph2& H) {

for (size_t i = 0; i < H.size(); ++i) {
for (auto&& k : H[i]) {
for (auto&& j : G[std::graph::target_id(H, k)]) {
if (std::graph::target_id(G, j) != i) {
s_overlap.push_back({i, std::graph::target_id(G, j), std::graph::target_id(H, k)});
for (auto&& j : G[graph::target_id(H, k)]) {
if (graph::target_id(G, j) != i) {
s_overlap.push_back({i, graph::target_id(G, j), graph::target_id(H, k)});
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions example/CppCon2022/germany_routes_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <cassert>

using namespace std::ranges;
using namespace std::graph;
using namespace std::graph::container;
using namespace std::graph::views;
using namespace graph;
using namespace graph::container;
using namespace graph::views;
using namespace std::literals;

using std::cout;
Expand Down Expand Up @@ -105,7 +105,7 @@ TEST_CASE("Germany Routes Presentation", "[presentation][germany][routes][shorte
cout << "Traverse the vertices & outgoing edges" << endl;
for (auto&& [uid, u] : vertexlist(g)) { // [id,vertex&]
cout << city_id(g, uid) << endl; // city name [id]
for (auto&& [vid, uv] : std::graph::views::incidence(g, uid)) { // [target_id,edge&]
for (auto&& [vid, uv] : graph::views::incidence(g, uid)) { // [target_id,edge&]
cout << " --> " << city_id(g, vid) << endl;
// "--> "target city" [target_id]
}
Expand Down
Loading

0 comments on commit 300bd9b

Please sign in to comment.