def graph_to_laplacian(G, normalized=True):
try:<EOL><INDENT>import networkx as nx<EOL>if isinstance(G, nx.Graph):<EOL><INDENT>if normalized:<EOL><INDENT>return nx.normalized_laplacian_matrix(G)<EOL><DEDENT>else:<EOL><INDENT>return nx.laplacian_matrix(G)<EOL><DEDENT><DEDENT><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>import graph_tool.all as gt<EOL>if isinstance(G, gt.Graph):<EOL><INDENT>if normalized:<EOL><INDENT>return gt.laplacian_type(G, normalized=True)<EOL><DEDENT>else:<EOL><INDENT>return gt.laplacian(G)<EOL><DEDENT><DEDENT><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>import igraph as ig<EOL>if isinstance(G, ig.Graph):<EOL><INDENT>if normalized:<EOL><INDENT>return np.array(G.laplacian(normalized=True))<EOL><DEDENT>else:<EOL><INDENT>return np.array(G.laplacian())<EOL><DEDENT><DEDENT><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>
Converts a graph from popular Python packages to Laplacian representation.

Currently support NetworkX, graph_tool and igraph.

Parameters
----------
G : obj
    Input graph
normalized : bool
    Whether to use normalized Laplacian.
    Normalized and unnormalized Laplacians capture different properties of graphs, e.g. normalized Laplacian spectrum can determine whether a graph is bipartite, but not the number of its edges. We recommend using normalized Laplacian.

Returns
-------
scipy.sparse
    Laplacian matrix of the input graph

Examples
--------
>>> graph_to_laplacian(nx.complete_graph(3), 'unnormalized').todense()
[[ 2, -1, -1], [-1,  2, -1], [-1, -1,  2]]

>>> graph_to_laplacian('test')
None