简体   繁体   中英

Find edges of multiplicity larger than 2 in a NetworkX MultiGraph

I have a MultiGraph which can have multiple edges between any two nodes:

g = MultiGraph()
g.add_nodes_from([1,2,3,4,5,6])
g.add_edges_from([(1,2), (2,1), (1,2), (3,4), (5,6), (6,5), (3,5), (3, 5), (3, 5), (3, 5)])

How can I find all edges with multiplicity larger than 2 in this graph? I can do something like this:

for s in itertools.combinations(g.nodes(), 2):
    e = g[s[0]].get(s[1], {})
    if len(e) > 2:
        print(s[0], s[1])

but it is too inefficient, so I'm looking for a better solution

for u in G.nodes():
    for neighbor in G.neighbors(u):
        if G.number_of_edges(u,neighbor)>2:
            print (u,neighbor)

Note - each such edge will be printed twice.

[x for x in tmp.edges if tmp.number_of_edges(x[0], x[1]) > 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM