-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubgraph_algorithms.hpp
More file actions
272 lines (204 loc) · 11.3 KB
/
subgraph_algorithms.hpp
File metadata and controls
272 lines (204 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2024 Huawei Technologies Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@author Toni Boehnlein, Benjamin Lozes, Pal Andras Papp, Raphael S. Steiner
*/
#pragma once
#include "osp/concepts/constructable_computational_dag_concept.hpp"
#include "osp/concepts/directed_graph_concept.hpp"
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
namespace osp {
template<typename Graph_t_in, typename Graph_t_out>
void create_induced_subgraph(const Graph_t_in &dag, Graph_t_out &dag_out,
const std::set<vertex_idx_t<Graph_t_in>> &selected_nodes,
const std::set<vertex_idx_t<Graph_t_in>> &extra_sources = {}) {
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
"Graph_t_in and out must have the same vertex_idx types");
static_assert(is_constructable_cdag_vertex_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_vertex concept");
static_assert(is_constructable_cdag_edge_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_edge concept");
assert(dag_out.num_vertices() == 0);
std::map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> local_idx;
for (const auto &node : extra_sources) {
local_idx[node] = dag_out.num_vertices();
if constexpr (is_constructable_cdag_typed_vertex_v<Graph_t_out> and has_typed_vertices_v<Graph_t_in>) {
// add extra source with type
dag_out.add_vertex(0, dag.vertex_comm_weight(node), dag.vertex_mem_weight(node), dag.vertex_type(node));
} else {
// add extra source without type
dag_out.add_vertex(0, dag.vertex_comm_weight(node), dag.vertex_mem_weight(node));
}
}
for (const auto &node : selected_nodes) {
local_idx[node] = dag_out.num_vertices();
if constexpr (is_constructable_cdag_typed_vertex_v<Graph_t_out> and has_typed_vertices_v<Graph_t_in>) {
// add vertex with type
dag_out.add_vertex(dag.vertex_work_weight(node), dag.vertex_comm_weight(node), dag.vertex_mem_weight(node),
dag.vertex_type(node));
} else {
// add vertex without type
dag_out.add_vertex(dag.vertex_work_weight(node), dag.vertex_comm_weight(node), dag.vertex_mem_weight(node));
}
}
if constexpr (has_edge_weights_v<Graph_t_in> and has_edge_weights_v<Graph_t_out>) {
// add edges with edge comm weights
for (const auto &node : selected_nodes)
for (const auto &in_edge : in_edges(node, dag)) {
const auto &pred = source(in_edge, dag);
if (selected_nodes.find(pred) != selected_nodes.end() || extra_sources.find(pred) != extra_sources.end())
dag_out.add_edge(local_idx[pred], local_idx[node], dag.edge_comm_weight(in_edge));
}
} else {
// add edges without edge comm weights
for (const auto &node : selected_nodes)
for (const auto &pred : dag.parents(node)) {
if (selected_nodes.find(pred) != selected_nodes.end() ||
extra_sources.find(pred) != extra_sources.end())
dag_out.add_edge(local_idx[pred], local_idx[node]);
}
}
}
template<typename Graph_t_in, typename Graph_t_out>
void create_induced_subgraph(const Graph_t_in &dag, Graph_t_out &dag_out,
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
return create_induced_subgraph(dag, dag_out, std::set<vertex_idx_t<Graph_t_in>>(selected_nodes.begin(), selected_nodes.end()));
}
template<typename Graph_t>
bool checkOrderedIsomorphism(const Graph_t &first, const Graph_t &second) {
static_assert(is_directed_graph_v<Graph_t>, "Graph_t must satisfy the directed_graph concept");
if (first.num_vertices() != second.num_vertices() || first.num_edges() != second.num_edges())
return false;
for (const auto &node : first.vertices()) {
if (first.vertex_work_weight(node) != second.vertex_work_weight(node) ||
first.vertex_mem_weight(node) != second.vertex_mem_weight(node) ||
first.vertex_comm_weight(node) != second.vertex_comm_weight(node) ||
first.vertex_type(node) != second.vertex_type(node))
return false;
if (first.in_degree(node) != second.in_degree(node) || first.out_degree(node) != second.out_degree(node))
return false;
if constexpr (has_edge_weights_v<Graph_t>) {
std::set<std::pair<vertex_idx_t<Graph_t>, e_commw_t<Graph_t>>> first_children, second_children;
for (const auto &out_edge : out_edges(node, first))
first_children.emplace(target(out_edge, first), first.edge_comm_weight(out_edge));
for (const auto &out_edge : out_edges(node, second))
second_children.emplace(target(out_edge, second), second.edge_comm_weight(out_edge));
auto itr = first_children.begin(), second_itr = second_children.begin();
for (; itr != first_children.end() && second_itr != second_children.end(); ++itr) {
if (*itr != *second_itr)
return false;
++second_itr;
}
} else {
std::set<vertex_idx_t<Graph_t>> first_children, second_children;
for (const auto &child : first.children(node))
first_children.emplace(child);
for (const auto &child : second.children(node))
second_children.emplace(child);
auto itr = first_children.begin(), second_itr = second_children.begin();
for (; itr != first_children.end() && second_itr != second_children.end(); ++itr) {
if (*itr != *second_itr)
return false;
++second_itr;
}
}
}
return true;
}
template<typename Graph_t_in, typename Graph_t_out>
std::vector<Graph_t_out> create_induced_subgraphs(const Graph_t_in &dag_in,
const std::vector<unsigned> &partition_IDs) {
// assumes that input partition IDs are consecutive and starting from 0
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
"Graph_t_in and out must have the same vertex_idx types");
static_assert(is_constructable_cdag_vertex_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_vertex concept");
static_assert(is_constructable_cdag_edge_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_edge concept");
unsigned number_of_parts = 0;
for (const auto id : partition_IDs)
number_of_parts = std::max(number_of_parts, id + 1);
std::vector<Graph_t_out> split_dags(number_of_parts);
std::vector<vertex_idx_t<Graph_t_out>> local_idx(dag_in.num_vertices());
for (const auto node : dag_in.vertices()) {
local_idx[node] = split_dags[partition_IDs[node]].num_vertices();
if constexpr (is_constructable_cdag_typed_vertex_v<Graph_t_out> and has_typed_vertices_v<Graph_t_in>) {
split_dags[partition_IDs[node]].add_vertex(dag_in.vertex_work_weight(node), dag_in.vertex_comm_weight(node),
dag_in.vertex_mem_weight(node), dag_in.vertex_type(node));
} else {
split_dags[partition_IDs[node]].add_vertex(dag_in.vertex_work_weight(node), dag_in.vertex_comm_weight(node),
dag_in.vertex_mem_weight(node));
}
}
if constexpr (has_edge_weights_v<Graph_t_in> and has_edge_weights_v<Graph_t_out>) {
for (const auto node : dag_in.vertices()) {
for (const auto &out_edge : out_edges(node, dag_in)) {
auto succ = target(out_edge, dag_in);
if (partition_IDs[node] == partition_IDs[succ])
split_dags[partition_IDs[node]].add_edge(local_idx[node], local_idx[succ],
dag_in.edge_comm_weight(out_edge));
}
}
} else {
for (const auto node : dag_in.vertices()) {
for (const auto &child : dag_in.children(node)) {
if (partition_IDs[node] == partition_IDs[child])
split_dags[partition_IDs[node]].add_edge(local_idx[node], local_idx[child]);
}
}
}
return split_dags;
}
template<typename Graph_t_in, typename Graph_t_out>
std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> create_induced_subgraph_map(const Graph_t_in &dag, Graph_t_out &dag_out,
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
"Graph_t_in and out must have the same vertex_idx types");
static_assert(is_constructable_cdag_vertex_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_vertex concept");
static_assert(is_constructable_cdag_edge_v<Graph_t_out>,
"Graph_t_out must satisfy the constructable_cdag_edge concept");
assert(dag_out.num_vertices() == 0);
std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> local_idx;
local_idx.reserve(selected_nodes.size());
for (const auto &node : selected_nodes) {
local_idx[node] = dag_out.num_vertices();
if constexpr (is_constructable_cdag_typed_vertex_v<Graph_t_out> and has_typed_vertices_v<Graph_t_in>) {
// add vertex with type
dag_out.add_vertex(dag.vertex_work_weight(node), dag.vertex_comm_weight(node), dag.vertex_mem_weight(node),
dag.vertex_type(node));
} else {
// add vertex without type
dag_out.add_vertex(dag.vertex_work_weight(node), dag.vertex_comm_weight(node), dag.vertex_mem_weight(node));
}
}
if constexpr (has_edge_weights_v<Graph_t_in> and has_edge_weights_v<Graph_t_out>) {
// add edges with edge comm weights
for (const auto &node : selected_nodes)
for (const auto &in_edge : in_edges(node, dag)) {
const auto &pred = source(in_edge, dag);
if (local_idx.count(pred))
dag_out.add_edge(local_idx[pred], local_idx[node], dag.edge_comm_weight(in_edge));
}
} else {
// add edges without edge comm weights
for (const auto &node : selected_nodes)
for (const auto &pred : dag.parents(node)) {
if (local_idx.count(pred))
dag_out.add_edge(local_idx[pred], local_idx[node]);
}
}
return local_idx;
}
} // end namespace osp