First version of Prim's algorithm.