networkx / networkx-metis

NetworkX Addon to allow graph partitioning with METIS
Other
79 stars 21 forks source link

How can I create METIS graph #65

Open zhiyuan8 opened 4 years ago

zhiyuan8 commented 4 years ago

Hello, I read your source codes for partition and to use your nxmetis.partition for a graph with both edge & node weights, I need to let me graph support: vwgt = [G.node[u].get(node_weight, 1) for u in G] vsize = [G.node[u].get(node_size, 1) for u in G] adjwgt = [G[u][v].get(edge_weight, 1) for u in G for v in G[u]] However, networkx graph does not have memeber function 'get'. It seems I can only use METIS graph for input. However, I cannot find a convert function in network-metis which transfer networkx graph to METIS graph. I have trouble installing METIS package by pip but the good news is that network-metis installation works well for me. So I hope to know how to get a METIS graph by your package. Thanks!

zhiyuan8 commented 4 years ago

Maybe there is another solution. I could overwrite your nxmetis.partition by mine and I put vwgt, vsize, adjwgt as function arguments. I am not sure if I can overwrite your partition function. Besides, in your nxmetis/tests/test_metis.py, you only have: partition = nxmetis.partition(self.G, 4) Can you please try to use METIS graph G, not networkx graph G for nxmetis.partition?

dschult commented 4 years ago
vwgt = nx.get_node_attributes(G, 'node_weight')
adjwgt = nx.get_edge_attributes(G, 'edge_weight')
vwgt2 = [G.nodes[u].get('node_weight', 1) for u in G]  # notice it is G.nodes, not G.node
zhiyuan8 commented 4 years ago

Thank you so much! I finally solved this problem with your help and self-experiments.

create graph to feed into your partition function

graph=nx.Graph()
print_info = True
for key,value in node_property.items():
    graph.add_node( node_for_adding=key ) # do not add attributes in for-loop
for key,value in edge_property.items():
    graph.add_edge( u_of_edge=key[0], v_of_edge=key[1]) # do not add attributes in for-loop
nx.set_node_attributes(graph, node_property) # add attributes here
nx.set_edge_attributes(graph, edge_property) # add attributes here
if print_info:
     print("vwgt, graph node weights in compressed format")
    print([graph.nodes[u].get('weight', 1) for u in graph])
    print("adjwgt, graph node weights in compressed format")
    print([graph[u][v].get('weight', 1) for u in graph for v in graph[u]] )
zhiyuan8 commented 4 years ago

And one exemplary node_property and edge_property is defined as a dictionary in networkx way: node_property = { # nested dict, use index to search, each dict has pos, weight 0:{'pos':(0,3),'weight':1}, 1:{'pos':(1,3),'weight':2}, 2:{'pos':(0,2),'weight':3}, 3:{'pos':(1,2),'weight':4}, } # 'weight' is for partition, 'size' is for communication volumn, do not care about 'size' right now

edge_property = { # nested dict, use index to search, each dict has pair, weight (0,1):{'weight':2}, (0,2):{'weight':1}, (1,3):{'weight':20}, (2,3):{'weight':15}, }