Implementing Prim's algorithm.