Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all edges linked to a given node in a networkx graph

Just wondering if there is convenient networkx function that returns a list of edges connected to a given node (or nodes) (e.g. my_node_name) in a graph (e.g. G).

I can do it this way:

edlist=[] for ed in G.edges():      if 'my_node_name' in ed:             edlist.append(ed) 

but expect there might be a better way?

like image 512
atomh33ls Avatar asked Oct 12 '15 10:10

atomh33ls


People also ask

Which functions is used to remove all edges and nodes in a graph in NetworkX?

clear. Remove all nodes and edges from the graph. This also removes the name, and all graph, node, and edge attributes.

How do you count edges on a directed graph?

In a directed graph having N vertices, each vertex can connect to N-1 other vertices in the graph(Assuming, no self loop). Hence, the total number of edges can be are N(N-1).

What is Nbunch in NetworkX?

nbunch. An nbunch is a single node, container of nodes or None (representing all nodes). It can be a list, set, graph, etc.. To filter an nbunch so that only nodes actually in G appear, use G.


1 Answers

If the graph is undirected, you can use

G.edges(node) 

In networkx 2.x this is an EdgeDataView object. In networkx 1.x this is a list - if you want a generator in 1.x rather than getting the whole list, G.edges_iter(node) works (this no longer exists in 2.x).

If the graph is directed the command above will not give the in-edges. Use

G.in_edges(node) G.out_edges(node)  

These are views in 2.x. In 1.x these are lists and there are generator options: G.in_edges_iter(node) and G.out_edges_iter(node)

like image 200
Joel Avatar answered Oct 20 '22 14:10

Joel