pygmtools.utils.from_networkx

pygmtools.utils.from_networkx(G: Graph)[source]

Convert networkx object to adjacency matrix

Parameters

G – networkx object, whose type must be networkx.Graph

Returns

the adjacency matrix corresponding to the networkx object

Example
>>> import networkx as nx
>>> import pygmtools as pygm
>>> pygm.set_backend('numpy')

# Generate networkx graphs
>>> G1 = nx.DiGraph()
>>> G1.add_weighted_edges_from([(1, 2, 0.5), (2, 3, 0.8), (3, 4, 0.7)])
>>> G2 = nx.DiGraph()
>>> G2.add_weighted_edges_from([(1, 2, 0.3), (2, 3, 0.6), (3, 4, 0.9), (4, 5, 0.4)])

# Obtain Adjacency matrix
>>> pygm.utils.from_networkx(G1)
matrix([[0. , 0.5, 0. , 0. ],
        [0. , 0. , 0.8, 0. ],
        [0. , 0. , 0. , 0.7],
        [0. , 0. , 0. , 0. ]])

>>> pygm.utils.from_networkx(G2)
matrix([[0. , 0.3, 0. , 0. , 0. ],
        [0. , 0. , 0.6, 0. , 0. ],
        [0. , 0. , 0. , 0.9, 0. ],
        [0. , 0. , 0. , 0. , 0.4],
        [0. , 0. , 0. , 0. , 0. ]])