Reputation: 103
My objective is to build a code that, given an adjacency matrix, return me a digraph. I wrote this snippet:
>>> import networkx as nx
>>> import numpy as np
>>> G = nx.DiGraph()
>>> arr = np.array([[1,1],[1,0]])
>>> G = nx.from_numpy_array(arr)
>>> list(G.nodes)
[0, 1]
>>> list(G.edges)
[(0, 0), (0, 1)]
I was expecting that the last command would return me:
[(0, 0), (0, 1), (1, 0)]
What I'm missing?
Upvotes: 0
Views: 26
Reputation: 548
This produces what you want:
arr = np.array([[1,1],[1,0]])
G = nx.from_numpy_array(arr, create_using=nx.DiGraph)
list(G.nodes) # [0, 1]
list(G.edges) # [(0, 0), (0, 1), (1, 0)]
Upvotes: 1