simple implementation of Prim's algorithm