@require('numpy')
@not_implemented_for('directed')
def laplacian_matrix(G, nodelist=None, weight='weight'):
    """Return the Laplacian matrix of G.

    The graph Laplacian is the matrix L = D - A, where
    A is the adjacency matrix and D is the diagonal matrix of node degrees.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list, optional
       The rows and columns are ordered according to the nodes in nodelist.
       If nodelist is None, then the ordering is produced by G.nodes().

    weight : string or None, optional (default='weight')
       The edge data key used to compute each value in the matrix.
       If None, then each edge has weight 1.

    Returns
    -------
    L : NumPy matrix
      The Laplacian matrix of G.

    Notes
    -----
    For MultiGraph/MultiDiGraph, the edges weights are summed.
    See to_numpy_matrix for other options.

    See Also
    --------
    to_numpy_matrix
    normalized_laplacian_matrix
    """
    import numpy as np
    if nodelist is None:
        nodelist = G.nodes()
    if G.is_multigraph():
        # this isn't the fastest way to do this...
        A = np.asarray(nx.to_numpy_matrix(G,nodelist=nodelist,weight=weight))
        I = np.identity(A.shape[0])
        D = I*np.sum(A,axis=1)
        L = D - A
    else:
        # Graph or DiGraph, this is faster than above
        n = len(nodelist)
        index = dict( (n,i) for i,n in enumerate(nodelist) )
        L = np.zeros((n,n))
        for ui,u in enumerate(nodelist):
            totalwt = 0.0
            for v,d in G[u].items():
                try:
                    vi = index[v]
                except __HOLE__:
                    continue
                wt = d.get(weight,1)
                L[ui,vi] = -wt
                totalwt += wt
            L[ui,ui] = totalwt
    return np.asmatrix(L)
dataset/ETHPy150Open gkno/gkno_launcher/src/networkx/linalg/laplacianmatrix.py/laplacian_matrix