implemented Prim's algorithm