如何在Metis中为Python构造图形

时间:2023-02-02 11:04:11

I am using Metis for Python, a Python wrapper for Metis (a graphs partitioning software). I have everything installed and it seems to work correctly, however I do not understand how can I construct a graph to input.

我正在为Python使用Metis,为Metis使用Python包装器(图分区软件)。我已经安装好了所有的东西,而且它看起来工作正常,但是我不知道如何构造一个图形来输入。

There is an online example in: http://metis.readthedocs.org/en/latest/#example

在http://metis.readthedocs.org/en/latest/#示例中有一个在线示例

>>> 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

I ran this example and it works fine. However in this example they never specify how to construct the graph “example_networkx()”. I have tried to construct graphs by networkx : http://metis.readthedocs.org/en/latest/#metis.networkx_to_metis

我运行了这个例子,它运行得很好。但是在本例中,他们从未指定如何构造图“example_networkx()”。我尝试过用networkx: http://metis.readthedocs.org/en/latest/#metis.networkx_to_metis构造图形

my code is:

我的代码是:

>>> 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)

I get an error in the last line. The error is traced back to these lines in the Metis built-in code:

最后一行有错误。错误可以追溯到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()

I have also tried to construct graphs by adjacency list: http://metis.readthedocs.org/en/latest/#metis.adjlist_to_metis but this gives the same error as before.

我还尝试通过邻接列表来构造图:http://metis.readthedocs.org/en/latest/#metis.adjlist_to_metis,但这与以前的错误相同。

I was wondering if anyone has had this problem, or has any idea what I'm doing wrong.

我想知道是否有人遇到过这个问题,或者知道我做错了什么。

I'm using python 2.7 on Centos 6.5

我在Centos 6。5中使用了python 2.7

1 个解决方案

#1


3  

The metis.part_graph accepts both networkx and adjacency list representation of graphs. you were almost right when you constructed a networkx graph. However, you should directly pass this graph to part_graph function rather than first converting it to a metis object since part_graph function does not directly accept a metis type graph. Given an adjajancy matrix A in numpy, an example can be:

梅蒂斯人。part_graph接受networkx和邻接表的图形表示。构建networkx图形时,您几乎是对的。但是,您应该直接将这个图传递给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)

#1


3  

The metis.part_graph accepts both networkx and adjacency list representation of graphs. you were almost right when you constructed a networkx graph. However, you should directly pass this graph to part_graph function rather than first converting it to a metis object since part_graph function does not directly accept a metis type graph. Given an adjajancy matrix A in numpy, an example can be:

梅蒂斯人。part_graph接受networkx和邻接表的图形表示。构建networkx图形时,您几乎是对的。但是,您应该直接将这个图传递给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)