Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping from a node's name to its index and vice versa in networkx

Given a networkx graph, is there a way to map from a node's name to its index in the adjacency matrix and vice versa?

I know that G.nodes() returns a list where the index of the node in the list corresponds to its index in the adjacency matrix.

So to map from a node's name to a node's index, I'm doing a very stupid way of storing the node's index in a dictionary and map it by the node's name.

To map from a node index to its name, then I create another dictionary similar to the one before (with the keys and values switched).

Is there a better way to do this?

like image 474
Jack Twain Avatar asked Aug 06 '14 12:08

Jack Twain


People also ask

How do I find the path between two nodes in NetworkX?

Networkx has a method named has_path () which can be used to check whether there exists as a path between two nodes or not. We can even use depth-first search and breadth-first search modules available in networkx to create graphs from the output of that algorithms and then plot it.

How to create a directed graph in NetworkX?

We can create a directed graph by using DiGraph () method of networkx. We can then loop through rows of our dataset and add edges to the graph. Directed graph object has method named add_edge () and add_node () which can be used to add edge and node respectively to graph.

How to find the importance of a node in a network?

The first approach to finding out the importance of any node in a network is by counting its number of nodes. For the directed graph, it'll be a number of nodes pointing to that node whereas, for undirected graph, it'll be a number of edges connecting to it. Let's count the number of neighbors of an above undirected graph.

What are nodes and edges in a graph?

Well, graphs are built using nodes and edges. A node represents some object, perhaps a person or organization, and an edge represents the actual connection from one node to another node. So in the example below, “A”, “B”, “C”, and “D” are nodes and the lines between them are the edges. We can also change the color of all the nodes quite easily.


2 Answers

Short answer: not as far as I know. I have a bunch of code similar to yours, though with a few improvements.

The first thing you can do is remember that each node has a dictionary hanging on it. G.node[node_name] is a dictionary of whatever you want. You can thus do something like:

G.node[node_name]['index'] = G.nodes().index(node_name)

Which at least hangs the information on the graph, but in the same fragile way as your method: if you modify your graph the behaviour of G.nodes() isn't documented.

A probably-better solution is to use the nodelist kwarg for G.adjacency_matrix. This takes a list of node names and outputs the adjacency matrix in that order. You can thus set up the list in a sensible-for-your-application way, rather than the pseudorandom behaviour of G.nodes(). For instance, you can pass it a sorted list of your nodes (sorted(G.nodes())) if the nodes are named in a useful way.

like image 99
Excalabur Avatar answered Sep 20 '22 16:09

Excalabur


For small / moderate graphs, one simple option is to store the node names as a list:

nodes_list = np.array(list(g.nodes()))

To recover the name from the index, e.g:

node_name = nodes_list[4]

To recover the index from the name:

node_id = np.where(nodes_list == "n4")[0][0]
like image 31
JARS Avatar answered Sep 18 '22 16:09

JARS