functions for non-bipartite optimal matching