softProcess
softProcess

Reputation: 91

Networkx- finding the parallel edges of multiDigraph

I have a multidigraph like this-

Import Networkx as nx

G=nx.MultiDiGraph()
G.add_edge(0,1)
G.add_edge(1,0)
G.add_edge(1,3)

Any networkx way to find edge 0-1 and 1-0 are parallel?

Upvotes: 3

Views: 1664

Answers (1)

vurmux
vurmux

Reputation: 10020

There is no build-in networkx functions for this problem, as I know. But networkx stores graph nodes and edges in iterable structures, so you can process them one by one like this:

# For every node in graph
for node in G.nodes(): 
    # We look for adjacent nodes
    for adj_node in G[node]: 
        # If adjacent node has an edge to the first node
        # Or our graph have several edges from the first to the adjacent node
        if node in G[adj_node] or len(G[node][adj_node]) > 1: 
            # DO MAGIC!!
            print(node, adj_node)

I think it is the most networkx-ish code that can solve your problem. Note, that the more sparse graph is, the faster will it work. In the worst case - the complete graph - the complexity is O(n^2). In the best case - very sparse graph - O(n).

Upvotes: 3

Related Questions