Prim algorithm for finding minimum spanning tree