CMPS 144 Spring 2020
Prog. Assg. #3
Shortest Cycles in a Directed Graph
Due: March 30

You are given the following files:

You should be familiar with the queue-related Java source code files, but you should spend some time examining each of the others so that you come to understand each one's purpose.

Your task is to complete the CycleFinder class, two of whose methods are unfinished: cycle() and findShortestCycle(). You can use the length() method for ideas on how to complete the former. As for the latter, you should use as a model the algorithm for the Uniform-Cost Single Source Shortest Paths Problem, as covered in lecture.

However, keep in mind that your findShortestCycle() method is solving a slightly different problem: It is attempting to find a shortest cycle beginning at a specified "target" vertex. (A cycle1 is a path of one or more edges that begins and ends at the same vertex.) Hence, the method should be written so that, once it "discovers" the target vertex, it quickly finishes, there being no need to discover any others.

For the purpose of iterating through all the outgoing edges of the vertex being "explored", you are encouraged to make use of either the neighborIterator() or neighbors() method in Digraph rather than the hasEdge() method. To illustrate, suppose that variable g refers to an instance of the Digraph class and that variable u has a vertex ID as its value and that you want to explore from that vertex:

Using neighborIterator() Using neighbors()
Iterator<Integer> iter = g.neighborIterator();
while (iter.hasNext()) {
   int v = iter.next();
   < process the vertex whose ID is v >
}
Integer[] neighborsOfU = g.neighbors(u);
for (int i = 0; i != neighborsOfU.length; i++) {
   int v = neighborsOfU[i];
   < process the vertex whose ID is v >
}


Submit your CycleFinder.java file to the Prog. Assg. #3 folder.

Example Tester Dialog

The following shows an example of running the given CycleFinderTester application and providing it with the graph described in the given graph2.txt file. It first prints a description of the graph and then (repeatedly) asks the user to enter a vertex ID. When the user enters a value less than -1, the program terminates. Entering -1 causes the description of the graph to be displayed again.

Enter name of input file: graph2.txt
Graph has 15 vertices.
Edges from vertex 0 go to [2, 3, 5]
Edges from vertex 1 go to [0, 4, 7, 11]
Edges from vertex 2 go to []
Edges from vertex 3 go to [0, 1, 4, 6]
Edges from vertex 4 go to [0, 2, 4, 5, 9]
Edges from vertex 5 go to [0, 4, 7, 9, 10]
Edges from vertex 6 go to [1, 3, 7, 12]
Edges from vertex 7 go to [1, 2, 8]
Edges from vertex 8 go to [0, 7, 10, 11]
Edges from vertex 9 go to [0, 1, 2, 3, 4, 5]
Edges from vertex 10 go to [3, 4, 9, 11]
Edges from vertex 11 go to [5, 8, 14]
Edges from vertex 12 go to [0, 13]
Edges from vertex 13 go to [10]
Edges from vertex 14 go to [12]

Enter ID of target vertex: 14
Shortest cycles involving vertex 14 have length 5
One shortest cycle: [14, 12, 13, 10, 11, 14]

Enter ID of target vertex: 8
Shortest cycles involving vertex 8 have length 2
One shortest cycle: [8, 7, 8]

Enter ID of target vertex: 4
Shortest cycles involving vertex 4 have length 1
One shortest cycle: [4, 4]

Enter ID of target vertex: 2
Vertex 2 does not lie on a cycle.

Enter ID of target vertex: -5
Goodbye.


Footnote

[1] Although a graph theorist might cringe, we allow a directed graph to include self-loops (i.e., edges going from vertices to themselves) and we consider a self-loop to constitute a cycle of length one.