Implementation of Prims algorithm in Java