Implemented Prim's algorithm.