【发布时间】:2010-06-30 03:25:09
【问题描述】:
我正在尝试使用 scipy.weave 在 Python 中构建一个快速的最小生成树程序。不幸的是,将 scipy.weave 与我发现的 C++ 库 STANN 一起使用比我想象的要困难得多。这是 STANN 库的链接:http://sites.google.com/a/compgeom.com/stann/
下面是我编写的带有 scipy.weave 脚本的 Python。
import scipy.weave as weave
from scipy.weave import inline
import numpy
def gmst(points):
# computing GMST with STANN headers
assert(type(points) == type(numpy.array([])))
# now the c++ code
code = """
using namespace std;
typedef reviver::dpoint<double,2> Point;
typedef vector<Point>::size_type stype;
vector< std::pair<stype,stype> > outputmst;
PyArrayObject *py_val
gmst(points,outputmst);
return_val = outputmst;
"""
return inline(code,['points'],
headers = ["<iostream>","<gmst.hpp>","<dpoint.hpp>","<test.hpp>"],
include_dirs=["/home/tree/usr/STANN/include"])
到目前为止,编织工作还没有运气。任何想法为什么我遇到问题?谢谢您的帮助。
干杯
【问题讨论】:
-
不是您的问题,而是您的图表有多大/有多密集? (我使用一个简单的纯 python MST,sort + Kruskal + unionfind;大部分时间都在排序中。)
-
我通常处理 300 到 30,000 个节点。有时更大。但是 MST 需要计算很多次才能进行投影分析(3D 数据)。刚刚找到了一种在 Python 中使用 networkx 和 matplotlib.delaunay 编写 MST 程序的方法,可以在大约 5 秒内计算 50,000 个源的 MST。现在已经足够快了。