We present a general method to optimize the evaluation of Feynman diagrammatic expansions, which requires the automated symbolic assignment of momentum/energy conserving variables to each diagram. With this symbolic representation, we utilize the pole structure of each diagram to automatically sort the Feynman diagrams into groups that are likely to contain nearly equal or nearly cancelling diagrams, and we show that for some systems this cancellation is exact. This allows for a potentially massive cancellation during the numerical integration of internal momenta variables, leading to an optimal suppression of the 'sign problem' and hence reducing the computational cost. Although we define these groups using a frequency space representation, the equality or cancellation of diagrams within the group remains valid in other representations such as imaginary time used in standard diagrammatic Monte Carlo. As an application of the approach we apply this method, combined with algorithmic Matsubara integration (AMI) [Phys. Rev. B 99, 035120 (2019)] and Monte Carlo methods, to the Hubbard model self-energy expansion on a 2D square lattice up to sixth order which we evaluate and compare with existing benchmarks. arXiv:1911.11129v1 [cond-mat.str-el]