a version of the Hungarian/Munkres matching algorithm