CMPS 144L
Activity: Outdegree-1 Graphs

Background

This lab activity pertains to outdegree-1 graphs, which are directed graphs in which every node has outdegree one. In such a graph, following the path from any node eventually leads you to a node that lies on a cycle. For a more detailed treatment, see the writeup of the current programming assignment. (This lab exercise is intended to give you some insight as to how to complete the current programming assignment.)

Given

Provided to you are the following:

PathsApp

The student's task is to complete the PathsApp application program. It expects to be run using a command line argument (or what jGrasp refers to as a "run argument") indicating the name of a file containing input data, specifically the description of an outdegree-1 graph.

The program reads the data in the specified file and uses it to create a corresponding instance of the OutDeg1Graph class. It then iterates over the nodes in the graph, for each node v displaying (the ID's of the nodes lying on) two paths:

  1. The cycle that v reaches
  2. The path to the cycle that v reaches

Displaying these paths is the responsibility of the stubbed methods cycleReachedBy() and pathToCycle(), respectively. To make the task a little easier, it is acceptable if the paths are displayed going in reverse order.

To illustrate, below is pictured the outdegree-1 graph described in the file graph0.txt.

The following shows the result of running PathsApp "from the command line", providing "graph0.txt" as the command line argument. (Liberty was taken to split the list of edges over two lines of output.)

$ java PathsApp graph0.txt
The graph:
# nodes: 13
edges: (0,6) (1,6) (2,5) (3,1) (4,8) (5,8) (6,10) 
       (7,0) (8,3) (9,1) (10,3) (11,11) (12,3)

Cycle reached by node 0: 6 1 3 10 6
Path to cycle from node 0: 6 0

Cycle reached by node 1: 1 3 10 6 1
Path to cycle from node 1: 1

Cycle reached by node 2: 3 10 6 1 3
Path to cycle from node 2: 3 8 5 2

Cycle reached by node 3: 3 10 6 1 3
Path to cycle from node 3: 3

Cycle reached by node 4: 3 10 6 1 3
Path to cycle from node 4: 3 8 4

Cycle reached by node 5: 3 10 6 1 3
Path to cycle from node 5: 3 8 5

Cycle reached by node 6: 6 1 3 10 6
Path to cycle from node 6: 6

Cycle reached by node 7: 6 1 3 10 6
Path to cycle from node 7: 6 0 7

Cycle reached by node 8: 3 10 6 1 3
Path to cycle from node 8: 3 8

Cycle reached by node 9: 1 3 10 6 1
Path to cycle from node 9: 1 9

Cycle reached by node 10: 10 6 1 3 10
Path to cycle from node 10: 10

Cycle reached by node 11: 11 11
Path to cycle from node 11: 11

Cycle reached by node 12: 3 10 6 1 3
Path to cycle from node 12: 3 12


How cycleReachedBy() and pathToCycle() Work

You can think of each of these methods as having two phases, the first of which places node IDs on a stack and the second of which pops them off that stack. The first phase is common to the two methods:

Phase 1:
Push onto a stack the nodes on the path starting at the specified node v. (The next() method in OutDeg1Graph is useful here.) Stop when the next node w on that path completes a cycle, which is to say that it is already on the stack. (How can you tell if a node ID is already on the stack? A boolean array could be employed to help.)

Phase 2 of cycleReachedBy():
Display w and then repeatedly pop the stack, displaying each popped node ID until such time as w has been popped. What will have been displayed is the cycle, in reverse order, from w to itself. Because that is the cycle reachable from node v, the method will have done its job.

Phase 2 of pathToCycle():
Repeatedly pop the stack until w has been popped, without displaying anything. Display w and then repeatedly pop the stack, displaying each popped node ID until such time as the stack is empty. What will have been displayed is, in reverse order, the nodes on the path from v to w. As w is the "closest" node to v that lies on a cycle, the method will have done its job.