Implement Prim’s algorithm