Directed graph from adjacency matrix?

5,836 views
Skip to first unread message

Mars

unread,
Jul 25, 2011, 6:16:43 PM7/25/11
to networkx-discuss
Hello,

I am new to python, numpy and networkx.

I would like to make a graph out of an asymmetric adjacency matrix. A
MultiDiGraph seems appropriate, but it looks like from_numpy_matrix
can only return a Graph.

Can this be done? How?

Also: I can't figure out how to list all nodes/edges and their
respective values/weights of a graph (just for verification). How is
this done?

Thanks!

Aric Hagberg

unread,
Jul 25, 2011, 6:55:41 PM7/25/11
to networkx...@googlegroups.com

You can use the create_using= keyword to specify what kind of graph
you want like this:

In [1]: import numpy as np

In [2]: import networkx as nx

In [3]: A=np.matrix([[1,2],[3,0]])

In [4]: G=nx.from_numpy_matrix(A,create_using=nx.MultiDiGraph())

In [5]: G.edges(data=True)
Out[5]: [(0, 0, {'weight': 1}), (0, 1, {'weight': 2}), (1, 0, {'weight': 3})]

In [6]: A
Out[6]:
matrix([[1, 2],
[3, 0]])


Aric

Mars

unread,
Jul 27, 2011, 10:17:06 AM7/27/11
to networkx-discuss
Perfect - thanks a lot, Aric.

soumya sarkar

unread,
Mar 21, 2015, 9:29:40 AM3/21/15
to networkx...@googlegroups.com
Thanx dats exactly wat i was looking for.........Plz add this functionality to the official networkx documentation

Mridul Seth

unread,
Mar 21, 2015, 2:22:25 PM3/21/15
to networkx...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages