MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / Kruskal

Function Kruskal

cpp_easygraph/functions/path/path.cpp:190–247  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188 return a.second < b.second;
189}
190py::object Kruskal(py::object G, py::object weight) {
191 std::unordered_map<node_t, std::unordered_map<node_t, weight_t>> res_dict;
192 py::dict result_dict = py::dict();
193 std::vector<std::vector<node_t>> group;
194 Graph& G_ = G.cast<Graph&>();
195 adj_dict_factory& adj = G_.adj;
196 node_dict_factory& node_list = G_.node;
197 std::vector<std::pair<std::pair<node_t, node_t>, weight_t>> edge_list;
198 std::string weight_key = weight_to_string(weight);
199 for (node_dict_factory::iterator i = node_list.begin(); i != node_list.end(); i++) {
200 node_t i_id = i->first;
201 result_dict[G_.id_to_node[py::cast(i_id)]] = py::dict();
202 std::vector<node_t> temp_vector;
203 temp_vector.emplace_back(i_id);
204 group.emplace_back(temp_vector);
205 adj_attr_dict_factory i_adj = adj[i_id];
206 for (adj_attr_dict_factory::iterator j = i_adj.begin(); j != i_adj.end(); j++) {
207 node_t j_id = j->first;
208 weight_t edge_weight = adj[i_id][j_id].find(weight_key) != adj[i_id][j_id].end() ? adj[i_id][j_id][weight_key] : 1;
209 edge_list.emplace_back(std::make_pair(std::make_pair(i_id, j_id), edge_weight));
210 }
211 }
212 std::sort(edge_list.begin(), edge_list.end(), comp);
213 node_t m, n;
214 int group_size = group.size();
215 for (auto edge : edge_list) {
216 for (int i = 0; i < group_size; i++) {
217 int group_i_size = group[i].size();
218 for (int j = 0; j < group_i_size; j++) {
219 if (group[i][j] == edge.first.first) {
220 m = i;
221 break;
222 }
223 }
224 for (int j = 0; j < group_i_size; j++) {
225 if (group[i][j] == edge.first.second) {
226 n = i;
227 break;
228 }
229 }
230 }
231 if (m != n) {
232 res_dict[edge.first.first][edge.first.second] = edge.second;
233 std::vector<node_t> temp_vector;
234 group[m].insert(group[m].end(), group[n].begin(), group[n].end());
235 group[n].clear();
236 }
237 }
238 for (std::unordered_map<node_t, std::unordered_map<node_t, weight_t>>::iterator k = res_dict.begin();
239 k != res_dict.end(); k++) {
240 py::object res_node = G_.id_to_node[py::cast(k->first)];
241 for (std::unordered_map<node_t, weight_t>::iterator z = k->second.begin(); z != k->second.end(); z++) {
242 py::object res_adj_node = G_.id_to_node[py::cast(z->first)];
243 result_dict[res_node][res_adj_node] = z->second;
244 }
245 }
246 return result_dict;
247}

Callers

nothing calls this directly

Calls 4

weight_to_stringFunction · 0.85
sortFunction · 0.85
sizeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected