implements prim's algorithm