Implementation of Prim algorithm