我使用的是Metis for Python
,Metis的Python包装器(一个图形分区软件)。我已经安装了所有的东西,它似乎工作正常,但是我不明白如何构造一个要输入的图形。
下面有一个在线示例:http://metis.readthedocs.org/en/latest/#example
>>> import networkx as nx
>>> import metis
>>> G = metis.example_networkx()
>>> (edgecuts, parts) = metis.part_graph(G, 3)
>>> colors = ['red','blue','green']
>>> for i, p in enumerate(parts):
... G.node[i]['color'] = colors[p]
...
>>> nx.write_dot(G, 'example.dot') # Requires pydot or pygraphviz
我运行了这个例子,它运行得很好。但是,在本例中,它们从未指定如何构造图“example_networkx()”。我尝试用networkx:metis构造图
我的代码是:
>>> A=nx.Graph()
>>> A.add_edges_from([(3,1),(2,3),(1,2),(3,4),(4,5),(5,6),(5,7),(7,6),(4,10),(10,8),(10,9),(8,9)])
>>> G = metis.networkx_to_metis(A)
>>> (edgecuts, parts) = metis.part_graph(G, 3)
我在最后一行出现了一个错误。错误可追溯到Metis内置代码中的以下几行:
in part_graph(graph, nparts, tpwgts, ubvec, recursive, **opts)
graph = adjlist_to_metis(graph, nodew, nodesz)
in adjlist_to_metis(adjlist, nodew, nodesz)
m2 = sum(map(len, adjlist))
TypeError: object of type 'c_long' has no len()
我还试着用邻接列表:metis来构造图,但是这给出了和以前一样的错误。
我想知道是否有人有这个问题,或者知道我做错了什么。
我在CentOS6.5上使用python2.7
发布于 2015-02-22 18:05:15
metis.part_graph既接受网络表示,又接受图的邻接列表表示。当你构建一个网络图形的时候,你几乎是对的。但是,应该将此图直接传递给part_graph函数,而不是首先将其转换为metis对象,因为part_graph函数不直接接受metis类型图。给定numpy中的附加矩阵A
,一个例子可以是:
# since weights should all be integers
G = networkx.from_numpy_matrix(np.int32(A))
# otherwise metis will not recognize you have a weighted graph
G.graph['edge_weight_attr']='weight'
[cost, parts] = metis.part_graph(G, nparts=30, recursive=True)
https://stackoverflow.com/questions/26060423
复制相似问题