The Bayan Algorithm: Detecting Communities in Networks Through Exact and Approximate Optimization of Modularity