Skip to content

Commit

Permalink
feat:density
Browse files Browse the repository at this point in the history
feat:density
  • Loading branch information
yizhihenpidehou authored Aug 15, 2022
2 parents 616e8ff + b8b036a commit 007d1fd
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 99 deletions.
4 changes: 3 additions & 1 deletion cpp_easygraph/classes/__init__.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#define BOOST_PYTHON_STATIC_LIB

#include "graph.h"
#include "graph.h"
#include "directed_graph.h"
#include "operation.h"
100 changes: 100 additions & 0 deletions cpp_easygraph/classes/directed_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "directed_graph.h"
#include "../common/utils.h"

DiGraph::DiGraph(): Graph() {

}

py::object DiGraph__init__(py::tuple args, py::dict kwargs) {
py::object MappingProxyType = py::import("types").attr("MappingProxyType");
py::object self = args[0];
self.attr("__init__")();
DiGraph& self_ = py::extract<DiGraph&>(self);
py::dict graph_attr = kwargs;
self_.graph.update(graph_attr);
self_.nodes_cache = MappingProxyType(py::dict());
self_.adj_cache = MappingProxyType(py::dict());
return py::object();
}

py::object DiGraph_out_degree(py::object self, py::object weight) {
py::dict degree = py::dict();
py::list edges = py::extract<py::list>(self.attr("edges"));
py::object u, v;
py::dict d;
for (int i = 0;i < py::len(edges);i++) {
py::tuple edge = py::extract<py::tuple>(edges[i]);
u = edge[0];
v = edge[1];
d = py::extract<py::dict>(edge[2]);
if (degree.contains(u)) {
degree[u] += d.get(weight, 1);
}
else {
degree[u] = d.get(weight, 1);
}
}
py::list nodes = py::list(self.attr("nodes"));
for (int i = 0;i < py::len(nodes);i++) {
py::object node = nodes[i];
if (!degree.contains(node)) {
degree[node] = 0;
}
}
return degree;
}

py::object DiGraph_in_degree(py::object self, py::object weight) {
py::dict degree = py::dict();
py::list edges = py::extract<py::list>(self.attr("edges"));
py::object u, v;
py::dict d;
for (int i = 0;i < py::len(edges);i++) {
py::tuple edge = py::extract<py::tuple>(edges[i]);
u = edge[0];
v = edge[1];
d = py::extract<py::dict>(edge[2]);
if (degree.contains(v)) {
degree[v] += d.get(weight, 1);
}
else {
degree[v] = d.get(weight, 1);
}
}
py::list nodes = py::list(self.attr("nodes"));
for (int i = 0;i < py::len(nodes);i++) {
py::object node = nodes[i];
if (!degree.contains(node)) {
degree[node] = 0;
}
}
return degree;
}

py::object DiGraph_degree(py::object self, py::object weight) {
py::dict degree = py::dict();
py::dict out_degree = py::extract<py::dict>(self.attr("out_degree")(weight));
py::dict in_degree = py::extract<py::dict>(self.attr("in_degree")(weight));
py::list nodes = py::list(self.attr("nodes"));
for (int i = 0;i < py::len(nodes);i++) {
py::object u = nodes[i];
degree[u] = out_degree[u] + in_degree[u];
}
return degree;
}

py::object DiGraph_size(py::object self, py::object weight) {
py::dict out_degree = py::extract<py::dict>(self.attr("out_degree")(weight));
py::object s = py_sum(out_degree.values());
return (weight == py::object()) ? py::object(py::extract<int>(s)) : s;
}

py::object DiGraph_number_of_edges(py::object self, py::object u, py::object v) {
if (u == py::object()) {
return self.attr("size")();
}
Graph& G = py::extract<Graph&>(self);
node_t u_id = py::extract<node_t>(G.node_to_id[u]);
node_t v_id = py::extract<node_t>(G.node_to_id.get(v, -1));
return py::object(int(v != -1 && G.adj[u_id].count(v_id)));
}
18 changes: 18 additions & 0 deletions cpp_easygraph/classes/directed_graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#define BOOST_PYTHON_STATIC_LIB

#include "graph.h"
#include "../common/common.h"

struct DiGraph: public Graph
{
DiGraph();
};

py::object DiGraph__init__(py::tuple args, py::dict kwargs);

py::object DiGraph_out_degree(py::object self, py::object weight);
py::object DiGraph_in_degree(py::object self, py::object weight);
py::object DiGraph_degree(py::object self, py::object weight);
py::object DiGraph_size(py::object self, py::object weight);
py::object DiGraph_number_of_edges(py::object self, py::object u, py::object v);
Loading

0 comments on commit 007d1fd

Please sign in to comment.