/** * Class: PositiveWeightedGraphDemo * Author: Peter Williams * LastEdit: Wed May 29 09:51:47 2002 * Purpose: To demonstrate shortest path algorithm */ import DataStructures.*; import java.io.*; import java.util.*; public class PositiveWeightedGraphDemo { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String filename; System.out.print("graph file name: "); filename = in.readLine(); PositiveWeightedGraph g = new PositiveWeightedGraph(); g.readGraphTxtFile(filename); if (g.nrArcs() <= 24) { Iterator i = g.arcs(); while (i.hasNext()) { WeightedGraph.Arc a = (WeightedGraph.Arc) i.next(); System.out.println (a.tail() + " -> " + a.head() + " " + a.weight()); } } System.out.println("graph has " + g.nrNodes() + " nodes" + " and " + g.nrArcs() + " arcs"); String reply; boolean running; do { String origin, destination; do { System.out.print("origin: "); origin = in.readLine(); } while (!g.contains(origin)); do { System.out.print("destination: "); destination = in.readLine(); } while (!g.contains(destination)); if (g.pathExists(origin, destination)) { Enumeration path = g.shortestPath(origin, destination); System.out.print(path.nextElement()); while (path.hasMoreElements()) { System.out.print(" -> " + path.nextElement()); } System.out.println (" (" + g.shortestPathLength(origin, destination) + ")"); } else { System.out.println ("no path exists between "+ origin + " and " + destination); } System.out.print("continue (y/n)? "); reply = in.readLine(); running = (reply.length() > 0 && (Character.toLowerCase(reply.charAt(0)) == 'y')); } while (running); } }