Optimized the collapseEdges() function and added a unit test for it.