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?
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.
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.
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With