| 2 | #include "graph.h" |
| 3 | |
| 4 | py::object density(py::object G) { |
| 5 | Graph& G_ = G.cast<Graph&>(); |
| 6 | int n = G_.node.size(); |
| 7 | int m = G.attr("number_of_edges")().cast<int>(); |
| 8 | if (m == 0 || n <= 1) { |
| 9 | return py::cast(0); |
| 10 | } |
| 11 | weight_t d = m * 1.0 / (n * (n - 1)); |
| 12 | if(G.attr("is_directed")().equal(py::cast(false))){ |
| 13 | d*=2; |
| 14 | } |
| 15 | return py::cast(d); |
| 16 | } |