Profile minimization problem for matrices and graphs