// Java /****************************************** use variable graph to access Graph methods (e.g. graph.getVertexCount()) Goto http://www.inf.ethz.ch/~braendle/graphbench for Java API ******************************************/ import ch.ethz.graphbench.toolbox.*; // import Iterator class import java.util.Iterator; public class ShortestEdge extends GraphAlgorithm{ public void executeAlgorithm(){ // make sure graph has edges at all if(graph.getEdgeCount() == 0){ return; } // reset all edges states graph.resetStates(); // get an iterator over all edges Iterator edgeIterator = graph.getEdgeIterator(); // get the first edge and save it as the shortest so far Edge shortestEdge = (Edge)edgeIterator.next(); // mark the edge as valid, to visualize that this is the shortest edge so far shortestEdge.setEdgeState(Edge.VALID_STATE); // Reference to the edge that is currently being proccessed Edge currentEdge; // go through all other edges while(edgeIterator.hasNext()){ // get the next edge and cast it currentEdge = (Edge)edgeIterator.next(); // mark it as active currentEdge.setEdgeState(Edge.ACTIVE_STATE); // test if it is shorter then the shortest one so far if(currentEdge.getLength() < shortestEdge.getLength()){ // if it is shorter // mark the previously shortest one as processed shortestEdge.setEdgeState(Edge.PROCESSED_STATE); // save current edge as the shortest one shortestEdge = currentEdge; // mark it as valid currentEdge.setEdgeState(Edge.VALID_STATE); }else{ // current edge has been processed currentEdge.setEdgeState(Edge.PROCESSED_STATE); } } // reset all edges states graph.resetStates(); // and mark only the shortest one shortestEdge.setEdgeState(Edge.VALID_STATE); } }