-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:density
- Loading branch information
Showing
12 changed files
with
307 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.