From b0be1f70879c66613d9feabf3b469b0e0da0823f Mon Sep 17 00:00:00 2001 From: Peter Kerzum Date: Thu, 12 Dec 2024 03:33:26 +0100 Subject: [PATCH 1/2] Make adjacency_matrix implement BidirectionalGraph concept Adjacency matrix is by nature bidirectional and one can see that the concept was mostly implemented before. That implementation missed the degree function(s). These functions are implemented in this commit and the traversal_category is updated to meet the concept requirements. Tests are also added, though they are a bit shallow in the sense that they seem to omit intensive algorithms. --- include/boost/graph/adjacency_matrix.hpp | 23 ++++++++++++++++++++++- test/adj_matrix_cc.cpp | 4 ++++ test/reverse_graph_cc.cpp | 22 ++++++++++++++++++++++ test/test_graphs.cpp | 4 ++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/boost/graph/adjacency_matrix.hpp b/include/boost/graph/adjacency_matrix.hpp index a089e1cb9..2a6ae889d 100644 --- a/include/boost/graph/adjacency_matrix.hpp +++ b/include/boost/graph/adjacency_matrix.hpp @@ -433,7 +433,8 @@ struct adj_matrix_traversal_tag : public virtual adjacency_matrix_tag, public virtual vertex_list_graph_tag, public virtual incidence_graph_tag, public virtual adjacency_graph_tag, - public virtual edge_list_graph_tag + public virtual edge_list_graph_tag, + public virtual bidirectional_graph_tag { }; @@ -826,6 +827,26 @@ typename adjacency_matrix< D, VP, EP, GP, A >::degree_size_type in_degree( return n; } +// degree +template < typename VP, typename EP, typename GP, typename A > +typename adjacency_matrix< directedS, VP, EP, GP, A >::degree_size_type +degree( + typename adjacency_matrix< directedS, VP, EP, GP, A >::vertex_descriptor u, + const adjacency_matrix< directedS, VP, EP, GP, A >& g) +{ + return in_degree(u, g) + out_degree(u, g); +} + +template < typename VP, typename EP, typename GP, typename A > +typename adjacency_matrix< undirectedS, VP, EP, GP, A >::degree_size_type +degree( + typename adjacency_matrix< undirectedS, VP, EP, GP, A >::vertex_descriptor + u, + const adjacency_matrix< undirectedS, VP, EP, GP, A >& g) +{ + return out_degree(u, g); +} + //========================================================================= // Functions required by the AdjacencyGraph concept diff --git a/test/adj_matrix_cc.cpp b/test/adj_matrix_cc.cpp index 9457a70c8..68d024ca3 100644 --- a/test/adj_matrix_cc.cpp +++ b/test/adj_matrix_cc.cpp @@ -21,6 +21,7 @@ int main(int, char*[]) BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >)); + BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((MutableGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< Graph >)); } @@ -30,6 +31,7 @@ int main(int, char*[]) BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >)); + BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((MutableGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< Graph >)); } @@ -44,6 +46,7 @@ int main(int, char*[]) BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >)); + BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< Graph >)); BOOST_CONCEPT_ASSERT((VertexMutablePropertyGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((EdgeMutablePropertyGraphConcept< Graph >)); @@ -64,6 +67,7 @@ int main(int, char*[]) BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >)); + BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< Graph >)); BOOST_CONCEPT_ASSERT((VertexMutablePropertyGraphConcept< Graph >)); BOOST_CONCEPT_ASSERT((EdgeMutablePropertyGraphConcept< Graph >)); diff --git a/test/reverse_graph_cc.cpp b/test/reverse_graph_cc.cpp index 7965f25cc..a4e5c56cd 100644 --- a/test/reverse_graph_cc.cpp +++ b/test/reverse_graph_cc.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -59,5 +60,26 @@ int main(int, char*[]) get_property(gr, graph_name_t()); set_property(gr, graph_name_t(), "foo"); } + // Check matrix + { + typedef adjacency_matrix< directedS, + property< vertex_color_t, int >, property< edge_weight_t, int >, + property< graph_name_t, std::string > > + AdjMatrix; + typedef reverse_graph< AdjMatrix > Graph; + BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >)); + BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >)); + typedef graph_traits< Graph >::vertex_descriptor Vertex; + typedef graph_traits< Graph >::edge_descriptor Edge; + BOOST_CONCEPT_ASSERT( + (ReadablePropertyGraphConcept< Graph, Vertex, vertex_color_t >)); + BOOST_CONCEPT_ASSERT( + (ReadablePropertyGraphConcept< Graph, Edge, edge_weight_t >)); + BOOST_CONCEPT_ASSERT( + (ReadablePropertyGraphConcept< Graph, Edge, edge_underlying_t >)); + AdjMatrix g(42); + Graph gr(g); + get_property(gr, graph_name_t()); + } return 0; } diff --git a/test/test_graphs.cpp b/test/test_graphs.cpp index 81c489bb5..577d9fff4 100644 --- a/test/test_graphs.cpp +++ b/test/test_graphs.cpp @@ -132,6 +132,8 @@ int main() Graph; BOOST_META_ASSERT(is_directed_graph< Graph >); BOOST_META_ASSERT(!is_multigraph< Graph >); + BOOST_META_ASSERT(is_bidirectional_graph< Graph >); + BOOST_META_ASSERT(is_directed_bidirectional_graph< Graph >); BOOST_META_ASSERT(has_vertex_property< Graph >); BOOST_META_ASSERT(has_bundled_vertex_property< Graph >); BOOST_META_ASSERT(has_edge_property< Graph >); @@ -145,6 +147,8 @@ int main() typedef adjacency_matrix< directedS, VertexBundle, EdgeBundle > Graph; BOOST_META_ASSERT(is_directed_graph< Graph >); BOOST_META_ASSERT(!is_multigraph< Graph >); + BOOST_META_ASSERT(is_bidirectional_graph< Graph >); + BOOST_META_ASSERT(is_directed_bidirectional_graph< Graph >); BOOST_META_ASSERT(has_vertex_property< Graph >); BOOST_META_ASSERT(has_bundled_vertex_property< Graph >); BOOST_META_ASSERT(has_edge_property< Graph >); From 50d26818e03bfb8043cbdfdf86700253fdbcc9d5 Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Fri, 28 Feb 2025 14:56:13 +0900 Subject: [PATCH 2/2] Fix comments for degree functions --- include/boost/graph/adjacency_matrix.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/boost/graph/adjacency_matrix.hpp b/include/boost/graph/adjacency_matrix.hpp index 2a6ae889d..83c22de50 100644 --- a/include/boost/graph/adjacency_matrix.hpp +++ b/include/boost/graph/adjacency_matrix.hpp @@ -827,7 +827,7 @@ typename adjacency_matrix< D, VP, EP, GP, A >::degree_size_type in_degree( return n; } -// degree +// O(N) template < typename VP, typename EP, typename GP, typename A > typename adjacency_matrix< directedS, VP, EP, GP, A >::degree_size_type degree( @@ -837,6 +837,7 @@ degree( return in_degree(u, g) + out_degree(u, g); } +// O(N) template < typename VP, typename EP, typename GP, typename A > typename adjacency_matrix< undirectedS, VP, EP, GP, A >::degree_size_type degree(