hoot
hoot

Reputation: 75

Why does laplacian computed by Networkx and by incidence matrix is different

import networkx as bx
import numpy as np
G1 = nx.erdos_renyi_graph(20, .3)
L1 = nx.linalg.laplacian_matrix(G1)
A1=nx.incidence_matrix(G1)
L1_inc = A1*np.transpose(A1)
L1_inc == L1

But the answer is not True for all elements. Since laplacian is not oriented, what is wrong?

If you need more info, please let me know.

Upvotes: 2

Views: 682

Answers (1)

Aric
Aric

Reputation: 25299

The function nx.incidence_matrix() gives an unoriented incidence matrix by default. You can pass oriented=True to return the oriented version. For example:

In [1]: import networkx as nx

In [2]: G = nx.path_graph(4)

In [3]: I = nx.incidence_matrix(G,oriented=True)

In [4]: I.todense()
Out[4]: 
matrix([[-1.,  0.,  0.],
        [ 1., -1.,  0.],
        [ 0.,  1., -1.],
        [ 0.,  0.,  1.]])

In [5]: L = nx.laplacian_matrix(G)

In [6]: L.todense()
Out[6]: 
matrix([[ 1, -1,  0,  0],
        [-1,  2, -1,  0],
        [ 0, -1,  2, -1],
        [ 0,  0, -1,  1]])

In [7]: (I*I.T).todense()
Out[7]: 
matrix([[ 1., -1.,  0.,  0.],
        [-1.,  2., -1.,  0.],
        [ 0., -1.,  2., -1.],
        [ 0.,  0., -1.,  1.]])

Upvotes: 2

Related Questions