'Using cosine distance of Laplacian spectrums as similarity between graphs

The current similarity functions on networkx didn't work fastenough for me and I couldn't make graphsim to work either.

So, I was naively thinking to use inner product distance of the two graphs’ laplacian_spectrum.

Is it reasonable? How should I accommodate for cases where graphs have different sizes? At the moment, I simply take part of spectrum based on min(len(g1), len(g2))

import numpy as np
import networkx as nx
from scipy.spatial import distance

g1=nx.generators.sudoku.sudoku_graph(5)
g2=nx.generators.sudoku.sudoku_graph(6)
k = min(len(g1), len(g2))

laplacian1 = nx.spectrum.laplacian_spectrum(g1)
laplacian2 = nx.spectrum.laplacian_spectrum(g2)
self_similarity = 1-distance.cosine(np.sort(laplacian1)[:k],np.sort(laplacian1)[:k])
g1_g2_similarity = 1-distance.cosine(np.sort(laplacian1)[:k],np.sort(laplacian2)[:k])


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source