Diameter partitioning algorithm in C and python