有向图变无向图并存储

有向图变无向图并存储 Transform directed graph into undirected graph.

'''


'''

import networkx as nx

edgelist_path = 'fq_following.number'
edgelist=[]
with open(edgelist_path, 'r') as edgelistX_reader:  # input anchor for bind
    for line in edgelistX_reader.readlines():
        temp_array = line.strip().split(' ')  #
        edgelist.append(list(map(int, temp_array))) # for netX, directly append
edgelistX_reader.close()

nodeID_set = set()   # set for store the nodes
for i in edgelist:
    nodeID_set.add(i[0])  #
    nodeID_set.add(i[1])  #

edges = [tuple(e) for e in edgelist]
node_names = [n for n in nodeID_set]
G = nx.DiGraph()
G.add_nodes_from(node_names)
G.add_edges_from(edges)
print(nx.info(G))

udrtG = G.to_undirected()
print(nx.info(udrtG))

nx.write_weighted_edgelist(udrtG, 'fq_following_udrt.number')
Published At
comments powered by Disqus