Implementing prim's algorithm.