Source of 28.28.java


  1: GraphInterface<String> roadMap = new UndirectedGraph<String>();
  2: roadMap.addVertex("Provincetown");
  3: roadMap.addVertex("Truro");
  4: // . . .
  5: 
  6: roadMap.addVertex("Falmouth");
  7: roadMap.addEdge("Provincetown", "Truro", 10);
  8: // . . .
  9: 
 10: roadMap.addEdge("Hyannis", "Falmouth", 20);
 11: 
 12: StackInterface<String> bestRoute = new LinkedStack<String>();
 13: double distance = roadMap.getCheapestPath("Truro", "Falmouth", bestRoute);
 14: System.out.println("The shortest route from Truro to Falmouth is " + 
 15:                    distance + " miles long and " +
 16:                    "passes through the following towns:");
 17: while (!bestRoute.isEmpty())
 18:    System.out.println(bestRoute.pop());
 19: // Version 4.0