/** executes the algorithm */ public String execute() { // have we found the solution? boolean solution = false; // start the timer long startTime = System.currentTimeMillis(); // initial town toExpand.add(new Town(sourceCity, 0, 0, 0)); while (!toExpand.isEmpty() && !solution) { // gets the city with lower g value Town currentTown = toExpand.poll(); nodes++; // rebuild the followed route for the selected town Town aux = currentTown; followedRoute = new ArrayList(); followedRoute.add(aux.number); while (aux.level != 0) { aux = aux.parent; followedRoute.add(0, aux.number); } if (currentTown.level == distances.getCitiesCount()) { solution = true; optimumRoute = followedRoute; optimumCost = currentTown.g; } else { for (int i = 0; i < distances.getCitiesCount(); i++) { // have we visited this city in the current followed route? boolean visited = followedRoute.contains(i); boolean isSolution = (followedRoute.size() == distances.getCitiesCount()) && (i == sourceCity); if (!visited || isSolution) { Town childTown = new Town( i, currentTown.g + distances.getCost(currentTown.number, i), 0, currentTown.level + 1); childTown.parent = currentTown; toExpand.add(childTown); } } } } long endTime = System.currentTimeMillis(); result = "-------------------------------------\n"; result += "BÚSQUEDA DE COSTO UNIFORME:\n"; result += "-------------------------------------\n"; result += "MEJOR SOLUCIÓN: \t" + optimumRoute.toString() + "\nCOSTE: \t\t" + optimumCost + "\n"; result += "NODOS VISITADOS: \t" + nodes + "\n"; result += "TIEMPO TRANSCURRIDO: \t" + (endTime - startTime) + " ms\n"; result += "-------------------------------------\n"; return result; }