Counting number of bidirectional edges of a Graph

1,002 views
Skip to first unread message

Jose Guzman

unread,
Mar 7, 2014, 3:02:45 PM3/7/14
to networkx...@googlegroups.com
Hi everybody,

I've just started to use networkx and when checked the documentation I could not find whether there is an algorithm to count the number bidirectional edges between nodes. For example:

>>> import networkx as nx
>>> G = nx.DiGraph()
>>> G.add_edges_from([(0,1),(1,0),(1,2)])
>>> G.size() # returns 3

it would expect to have 1 bidirectional node (0,1), (1,0).

Thanks in advance.

Jose






Dan Schult

unread,
Mar 7, 2014, 3:46:25 PM3/7/14
to networkx...@googlegroups.com
There is no built-in method to count bidirectional edges in a DiGraph.
But you could build one quickly with something like:

0.5 * len( [ 1 for (u,v) in G.edges() if u in G[v] ] )

Dan
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
> To post to this group, send email to networkx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/networkx-discuss.
> For more options, visit https://groups.google.com/d/optout.

Moritz Beber

unread,
Mar 8, 2014, 9:54:02 AM3/8/14
to networkx...@googlegroups.com

I'd include a check to avoid counting self-loops.

Reply all
Reply to author
Forward
0 new messages