Skip to content

Commit

Permalink
Merge pull request #122 from yo123abxd/pybind11
Browse files Browse the repository at this point in the history
bug fixing in cpp-ver CC
  • Loading branch information
yo123abxd authored Apr 26, 2024
2 parents f8931b6 + 9ad3dc6 commit cc0d389
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 685 deletions.
2 changes: 1 addition & 1 deletion cpp_easygraph/classes/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ void Graph::gen_CSR(const py::object& py_weight, const py::object& py_sources,
for (auto adj_it = n_adjs.begin(); adj_it != n_adjs.end(); ++adj_it) {
const edge_attr_dict_factory& edge_attr = adj_it->second;
auto edge_it = edge_attr.find(weight_key);
weight_t w = edge_it != edge_attr.end() ? edge_it->second : 1.9;
weight_t w = edge_it != edge_attr.end() ? edge_it->second : 1.0;

W.push_back(w);
E.push_back(node2idx[adj_it->first]);
Expand Down
41 changes: 26 additions & 15 deletions cpp_easygraph/functions/centrality/closeness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static py::object invoke_cpp_closeness_centrality(py::object G, py::object weigh
int N = G_.node.size();
bool is_directed = G.attr("is_directed")().cast<bool>();
std::string weight_key = weight_to_string(weight);
const Graph_L& G_l = graph_to_linkgraph(G_, is_directed, weight_key, false, true);
const Graph_L& G_l = graph_to_linkgraph(G_, is_directed, weight_key, false, false);
int cutoff_ = -1;
if (!cutoff.is_none()){
cutoff_ = cutoff.cast<int>();
Expand Down Expand Up @@ -82,20 +82,25 @@ static py::object invoke_cpp_closeness_centrality(py::object G, py::object weigh
}
}

py::list py_nodes_order;
std::vector<node_t> node_idx;
py::list ret;

for (auto it = G_.node.begin(); it != G_.node.end(); ++it) {
node_idx.push_back(it->first);
}
std::sort(node_idx.begin(), node_idx.end());
if (sources.is_none()) {
py::list py_nodes_order;
std::vector<node_t> node_idx;

for (int i = 0; i < node_idx.size(); ++i) {
py_nodes_order.append(G_.id_to_node[py::cast(node_idx[i])]);
}
for (auto it = G_.node.begin(); it != G_.node.end(); ++it) {
node_idx.push_back(it->first);
}
std::sort(node_idx.begin(), node_idx.end());

py::list ret;
ret.append(py_nodes_order);
for (int i = 0; i < node_idx.size(); ++i) {
py_nodes_order.append(G_.id_to_node[py::cast(node_idx[i])]);
}

ret.append(py_nodes_order);
} else {
ret.append(py::list(sources));
}
ret.append(total_res_lst);

return ret;
Expand All @@ -118,13 +123,19 @@ static py::object invoke_gpu_closeness_centrality(py::object G, py::object weigh
py::pybind11_fail(gpu_easygraph::err_code_detail(gpu_r));
}

py::list ret;

py::list ret_val;
for (int i = 0; i < CC.size(); ++i) {
for (int i = 0; i < sources.size(); ++i) {
ret_val.append(CC[i]);
}

py::list ret;
ret.append(py_nodes_order);
if (py_sources.is_none()) {
ret.append(py_nodes_order);
} else {
ret.append(py::list(py_sources));
}

ret.append(ret_val);

return ret;
Expand Down
18 changes: 12 additions & 6 deletions easygraph/functions/centrality/closeness.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def closeness_centrality_parallel(nodes, G, path_length):
dist = sum(x.values())
cnt = len(x)
if dist == 0:
ret.append([node, 0])
ret.append([node, 0.0])
else:
ret.append([node, (cnt - 1) * (cnt - 1) / (dist * (length - 1))])
return ret
Expand Down Expand Up @@ -68,6 +68,9 @@ def closeness_centrality(G, weight=None, sources=None, n_workers=None):
else:
path_length = functools.partial(single_source_bfs)

ret_nodes = []
ret_vals = []

if n_workers is not None:
# use parallel version for large graph
import random
Expand All @@ -87,16 +90,19 @@ def closeness_centrality(G, weight=None, sources=None, n_workers=None):
)
with Pool(n_workers) as p:
ret = p.imap(local_function, nodes)
res = [x for i in ret for x in i]
closeness = dict(res)
for i in ret:
for n, v in i:
ret_nodes.append(n)
ret_vals.append(v)
else:
# use np-parallel version for small graph
for node in nodes:
x = path_length(G, node)
dist = sum(x.values())
cnt = len(x)
ret_nodes.append(node)
if dist == 0:
closeness[node] = 0
ret_vals.append(0.0)
else:
closeness[node] = (cnt - 1) * (cnt - 1) / (dist * (length - 1))
return [list(G.nodes), list(closeness.values())]
ret_vals.append((cnt - 1) * (cnt - 1) / (dist * (length - 1)))
return [ret_nodes, ret_vals]
Loading

0 comments on commit cc0d389

Please sign in to comment.