optimize prim's