Off by 1 error in MinSpanningTree