Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void mst(){
- ArrayList<T> visited = new ArrayList<T>();
- ArrayList<T> unvisited = (ArrayList<T>)this.vertices.clone();
- visited.add(unvisited.remove(0));
- while(unvisited.size() > 0){
- T nextVertex = null;
- T startVertex = null;
- int minimumWeight = Integer.MAX_VALUE;
- int visitedSize = visited.size();
- int unvisitedSize = unvisited.size();
- for( int i = 0; i < visitedSize; i++ ){
- T fromVertex = visited.get(i);
- for( int j = 0; j < unvisitedSize; j++ ){
- T currentVertex = unvisited.get(j);
- int currentWeight = this.getWeight(fromVertex, currentVertex);
- if( minimumWeight > currentWeight ){
- minimumWeight = currentWeight;
- nextVertex = currentVertex;
- startVertex = fromVertex;
- }
- }
- }
- System.out.printf("(%s) - (%s)\n", startVertex, nextVertex);
- unvisited.remove(nextVertex);
- visited.add(nextVertex);
- }
- }
Add Comment
Please, Sign In to add comment