C++ implementation copied from https://github.com/vtraag/leidenalg (GPL3)
