The self-similarity of complex networks is typically investigated through computational algorithms the primary task of which is to cover the structure with a minimal number of boxes. Here we introduce a box-covering algorithm that not only outperforms previous ones, but also finds optimal solutions. For the two benchmark cases tested, namely, the E. Coli and the WWW networks, our results show that the improvement can be rather substantial, reaching up to 15% in the case of the WWW network.