Constrained graph partitioning via matrix differential equations