import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application that reads a description of an outdegree-1 graph from ** a file (whose name is provided via command line argument) and then, for ** each node v in the graph, displays (the IDs of) the nodes ** ** (1) that lie on the cycle that is reached by v, and ** (2) that lie on the path from v to the closest node on that cycle. ** ** Authors: R. McCloskey and < STUDENTS' NAMES > ** Known Defects: ... */ public class PathsApp { private static OutDeg1Graph graph; // the graph being examined public static void main(String[] args) { // args[0] is assumed to be name of a file containing the description // of an outdegree-1 graph. graph = readGraph(args[0]); System.out.println("The graph:"); System.out.println(graph); final int N = graph.numNodes(); for (int v = 0; v != N; v++) { System.out.printf("\nCycle reached by node %d: ", v); cycleReachedBy(v); System.out.printf("Path to cycle from node %d: ", v); pathToCycle(v); } } /* Displays the (ID's of the) nodes that lie on the cycle ** reached by the specified node. ** Note: The order in which the nodes are displayed is going ** backwards around the cycle. */ public static void cycleReachedBy(int v) { // STUB! } /* Displays the (ID's of the) nodes that lie on the path from the ** specified node to the "first" node lying on the cycle that it reaches. ** (If the node itself lies on a cycle, it should be the lone node whose ** ID is displayed.) ** Note: The order in which the nodes are displayed goes backwards to v. */ public static void pathToCycle(int v) { // STUB! } /* Returns an OutDeg1Graph object that corresponds to the outdegree-1 ** graph described in the file having the given name. ** The expected format for a plain text description of an outdegree-1 ** graph is exemplified by the following, which describes a graph of ** six nodes (as indicated by the first numeral) in which the single ** outgoing edge from nodes 0, 1, ..., 5 go to nodes 2, 0, 4, 2, 5, ** and 1, respectively. ** +--------------+ ** |6 2 0 4 2 5 1 | ** +--------------+ */ private static OutDeg1Graph readGraph(String fileName) { try { Scanner input = new Scanner(new File(fileName)); int numNodes = input.nextInt(); int[] arcs = new int[numNodes]; for (int i=0; i != numNodes; i++) { arcs[i] = input.nextInt(); } return new OutDeg1Graph(arcs); } catch (Exception e) { e.printStackTrace(System.out); System.out.println("\nProgram aborting"); System.exit(0); } return null; // compiler requires a return statement here, even } // though it cannot be reached }