Reputation: 833
Suppose that an Undirected graph neighboring nodes are given in a txt file as below
1 2
1 3
1 4
1 5
2 5
2 6
3 4
can I generate an adjacency matrix by a single function in networkx as below
[0 1 1 1 1 0 ]
[1 0 0 0 1 0 ]
[1 0 0 1 0 0 ]
[1 0 1 0 0 0 ]
[1 1 0 0 0 0 ]
[0 0 0 0 0 1 ]
Upvotes: 2
Views: 1597
Reputation: 1027
It is not really one function, but you can read edges from file and then create adjacency matrix like this:
import networkx as nx
G = nx.read_edgelist('list.txt')
A = nx.adjacency_matrix(G)
which gives
matrix([[ 0., 1., 1., 1., 1., 0.],
[ 1., 0., 0., 0., 1., 0.],
[ 1., 0., 0., 1., 0., 1.],
[ 1., 0., 1., 0., 0., 0.],
[ 1., 1., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0.]])
Upvotes: 4