Stability and optimization in structured population models on graphs