// Copyright (c) 1996, 1997, 1998 Bruno R. Preiss, P.Eng. // $Author: brpreiss $ // $Date: 1998/12/14 02:52:01 $ // $RCSfile: Demo10.java,v $ // $Revision: 3.5 $ package Opus5; import java.io.*; /** * Demonstration program that tests the following classes: * GraphAsMatrix, * GraphAsLists, * DigraphAsMatrix, and * DigraphAsLists. * @see GraphAsMatrix * @see GraphAsLists * @see DigraphAsMatrix * @see DigraphAsLists * @author Bruno R. Preiss, P.Eng. * @version $Id: Demo10.java,v 3.5 1998/12/14 02:52:01 brpreiss Exp $ **/ public class Demo10 { //[ final String copyright = "@(#) Copyright (c) 1998 by Bruno R. Preiss, P.Eng."; //] /** * Performs a number of tests on the specified undirected graph. * @param g The graph to be tested. **/ private static void testGraph (Graph g) { g.addVertex (0); g.addVertex (1); g.addVertex (2); g.addEdge (0, 1); g.addEdge (0, 2); g.addEdge (1, 2); Terminal.out.println (g); Terminal.out.println ("IsDirected returns " + g.isDirected ()); Terminal.out.println ("Using Vertex Enumeration"); Enumeration p = g.getVertices (); while (p.hasMoreElements ()) Terminal.out.println (p.nextElement ()); Terminal.out.println ("Using Edge Enumeration"); Enumeration q = g.getEdges (); while (q.hasMoreElements ()) Terminal.out.println (q.nextElement ()); PrintingVisitor visitor = new PrintingVisitor (); Terminal.out.println ("DepthFirstTraversal"); g.depthFirstTraversal (new PreOrder (visitor), 0); visitor.finish (); Terminal.out.println ("BreadthFirstTraversal"); g.breadthFirstTraversal (visitor, 0); visitor.finish (); Terminal.out.println ("IsConnected returns " + g.isConnected ()); } /** * Performs a number of tests on the specified directed graph. * @param g The digraph to be tested. **/ private static void testDigraph (Digraph g) { testGraph (g); PrintingVisitor visitor = new PrintingVisitor (); Terminal.out.println ("TopologicalOrderTraversal"); g.topologicalOrderTraversal (visitor); visitor.finish (); Terminal.out.println ("IsCyclic returns " + g.isCyclic ()); Terminal.out.println ("IsStronglyConnected returns " + g.isStronglyConnected ()); } /** * Performs a number of tests on the specified undirected, weighted graph. * @param g The weighted graph to be tested. **/ private static void testWeightedGraph (Graph g) { g.addVertex (0); g.addVertex (1); g.addVertex (2); g.addEdge (0, 1, new Int (3)); g.addEdge (0, 2, new Int (1)); g.addEdge (1, 2, new Int (4)); Terminal.out.println (g); Terminal.out.println ("Using Vertex Enumeration"); Enumeration p = g.getVertices (); while (p.hasMoreElements ()) Terminal.out.println (p.nextElement ()); Terminal.out.println ("Using Edge Enumeration"); Enumeration q = g.getEdges (); while (q.hasMoreElements ()) Terminal.out.println (q.nextElement ()); Terminal.out.println ("Prim's Algorithm"); Graph g2 = Algorithms.PrimsAlgorithm (g, 0); Terminal.out.println (g2); Terminal.out.println ("Kruskal's Algorithm"); g2 = Algorithms.KruskalsAlgorithm (g); Terminal.out.println (g2); } /** * Performs a number of tests on the specified weighted, directed graph. * @param g The weighted, directed graph to be tested. **/ private static void testWeightedDigraph (Digraph g) { testWeightedGraph (g); Graph g2 = Algorithms.DijkstrasAlgorithm (g, 0); Terminal.out.println (g2); Terminal.out.println ("Floyd's Algorithm"); g2 = Algorithms.FloydsAlgorithm (g); Terminal.out.println (g2); } /** * The (static) main program. * Invokes various test routines. * @param args The command-line arguments. Ignored. **/ public static void main (String[] args) { Graph g = new GraphAsMatrix (32); testGraph (g); g.purge (); testWeightedGraph (g); Digraph dg = new DigraphAsMatrix (32); testDigraph (dg); dg.purge (); testWeightedDigraph (dg); g = new GraphAsLists (32); testGraph (g); g.purge (); testWeightedGraph (g); dg = new DigraphAsLists (32); testDigraph (dg); dg.purge (); testWeightedDigraph (dg); } /** * A visitor that prints each object it visits. * @author Bruno R. Preiss, P.Eng. * @version $Id: Demo10.java,v 3.5 1998/12/14 02:52:01 brpreiss Exp $ **/ private static class PrintingVisitor extends AbstractVisitor { /** * Indicates whether a comma should be printed. **/ private boolean comma; /** * Prints the specified object on Terminal.out. **/ public void visit (Object object) { if (comma) Terminal.out.print (", "); Terminal.out.print (object); comma = true; } /** * Finishes a line of output on Terminal.out and * resets the comma flag. **/ public void finish () { Terminal.out.println (); comma = false; } } }