1+ /*
2+ Copyright 2024 Huawei Technologies Co., Ltd.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+
16+ @author Toni Boehnlein, Pal Andras Papp, Raphael S. Steiner, Christos Konstantinos Matzoros
17+ */
18+
19+ #define BOOST_TEST_MODULE SubGraphs
20+ #include < boost/test/unit_test.hpp>
21+
22+ #include " osp/graph_algorithms/specialised_graph_algorithms/subgraph_algorithms.hpp"
23+ #include " osp/graph_implementations/adj_list_impl/cdag_vertex_impl.hpp"
24+ #include " osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp"
25+
26+ using namespace osp ;
27+
28+ BOOST_AUTO_TEST_CASE (SubGraphCompactSparseGraph) {
29+ const std::vector<std::pair<std::size_t , std::size_t >> edges ({{0 , 1 }, {2 , 3 }, {6 , 10 }, {7 , 9 }, {0 , 2 }, {4 , 6 }, {1 , 6 }, {6 , 7 }, {5 , 6 }, {3 , 7 }, {1 , 2 }});
30+ Compact_Sparse_Graph<true , true , true , true , true > graph (11 , edges);
31+ Compact_Sparse_Graph<true , true , true , true , true > subGraph;
32+
33+ unsigned cntr = 0 ;
34+ for (const auto &vert : graph.vertices ()) {
35+ graph.set_vertex_work_weight (vert, cntr++);
36+ graph.set_vertex_comm_weight (vert, cntr++);
37+ graph.set_vertex_mem_weight (vert, cntr++);
38+ graph.set_vertex_type (vert, cntr++);
39+ }
40+
41+ const std::vector<vertex_idx_t <Compact_Sparse_Graph<true , true , true , true , true >>> selectVert ({2 , 3 , 10 , 6 , 7 });
42+ const auto vertCorrespondence = create_induced_subgraph_map (graph, subGraph, selectVert);
43+ BOOST_CHECK_EQUAL (subGraph.num_vertices (), selectVert.size ());
44+ BOOST_CHECK_EQUAL (subGraph.num_edges (), 4 );
45+
46+ for (const auto &vert : selectVert) {
47+ BOOST_CHECK_LT (vertCorrespondence.at (vert), selectVert.size ());
48+
49+ for (const auto &otherVert : selectVert) {
50+ if (vertCorrespondence.at (vert) == vertCorrespondence.at (otherVert)) {
51+ BOOST_CHECK_EQUAL (vert, otherVert);
52+ }
53+ }
54+ }
55+
56+ for (const auto &vert : selectVert) {
57+ BOOST_CHECK_EQUAL (graph.vertex_work_weight (vert), subGraph.vertex_work_weight (vertCorrespondence.at (vert)));
58+ BOOST_CHECK_EQUAL (graph.vertex_comm_weight (vert), subGraph.vertex_comm_weight (vertCorrespondence.at (vert)));
59+ BOOST_CHECK_EQUAL (graph.vertex_mem_weight (vert), subGraph.vertex_mem_weight (vertCorrespondence.at (vert)));
60+ BOOST_CHECK_EQUAL (graph.vertex_type (vert), subGraph.vertex_type (vertCorrespondence.at (vert)));
61+ }
62+ }
63+
64+ BOOST_AUTO_TEST_CASE (SubGraphDagVectorImpl) {
65+ using v_impl = cdag_vertex_impl<std::size_t , unsigned , unsigned , unsigned , unsigned >;
66+
67+ computational_dag_vector_impl<v_impl> graph;
68+ computational_dag_vector_impl<v_impl> subGraph;
69+
70+ const std::size_t numVert = 11 ;
71+ const std::vector<std::pair<std::size_t , std::size_t >> edges ({{0 , 1 }, {2 , 3 }, {6 , 10 }, {7 , 9 }, {0 , 2 }, {4 , 6 }, {1 , 6 }, {6 , 7 }, {5 , 6 }, {3 , 7 }, {1 , 2 }});
72+
73+ unsigned cntr = 0 ;
74+ for (std::size_t i = 0U ; i < numVert; ++i) {
75+ graph.add_vertex (cntr, cntr + 1U , cntr + 2U , cntr + 3U );
76+ cntr += 4U ;
77+ }
78+ for (const auto &[src, tgt] : edges) {
79+ graph.add_edge (src, tgt);
80+ }
81+
82+ const std::vector<vertex_idx_t <computational_dag_vector_impl<v_impl>>> selectVert ({2 , 3 , 10 , 6 , 7 });
83+ const auto vertCorrespondence = create_induced_subgraph_map (graph, subGraph, selectVert);
84+ BOOST_CHECK_EQUAL (subGraph.num_vertices (), selectVert.size ());
85+ BOOST_CHECK_EQUAL (subGraph.num_edges (), 4 );
86+
87+ for (const auto &vert : selectVert) {
88+ BOOST_CHECK_LT (vertCorrespondence.at (vert), selectVert.size ());
89+
90+ for (const auto &otherVert : selectVert) {
91+ if (vertCorrespondence.at (vert) == vertCorrespondence.at (otherVert)) {
92+ BOOST_CHECK_EQUAL (vert, otherVert);
93+ }
94+ }
95+ }
96+
97+ for (const auto &vert : selectVert) {
98+ BOOST_CHECK_EQUAL (graph.vertex_work_weight (vert), subGraph.vertex_work_weight (vertCorrespondence.at (vert)));
99+ BOOST_CHECK_EQUAL (graph.vertex_comm_weight (vert), subGraph.vertex_comm_weight (vertCorrespondence.at (vert)));
100+ BOOST_CHECK_EQUAL (graph.vertex_mem_weight (vert), subGraph.vertex_mem_weight (vertCorrespondence.at (vert)));
101+ BOOST_CHECK_EQUAL (graph.vertex_type (vert), subGraph.vertex_type (vertCorrespondence.at (vert)));
102+ }
103+ }
0 commit comments