Abstract

In this assignment, you will determine whether an arbitrary directed graph contains a topopath - an ordering of its vertices that simultaneously corresponds to a valid path in the graph and a valid topological sort. More than anything, this program should serve as a relatively short critical thinking exercise.

You will gain experience reading graphs from an input file, representing them computationally, and writing graph theory algorithms. You will also solidify your understanding of topological sorts, sharpen your problem solving skills, and get some practice at being clever, because your solution to this problem must be O(n2). In coming up with a solution, I recommend focusing first on developing a working algorithm, and then analyzing its runtime. (Focusing too much on the runtime restriction might cloud your thinking as you cook up a solution to this problem.)

If you use any code that I have given you so far in class, you should probably include a comment to give me credit. The intellectually curious student will, of course, try to write the whole program from scratch.

1. Problem Statement

Given a directed graph, determine whether it has a topopath - an ordering of its vertices that simultaneously corresponds to a valid path in the graph and a valid topological sort. For example: see image.

In G1, the vertex ordering 1, 2, 3, 4 is a valid topological sort, but does not correspond to a valid path in the graph (i.e., 1 -> 2 -> 3 -> 4 is not a path in G1). In fact, none of the topological sorts for G1 correspond to actual paths through that graph.

In contrast, the vertex ordering 1, 2, 3, 4 corresponds to a valid path in G2 (i.e., 1 -> 2 -> 3 -> 4 is an actual path in G2), but is not a valid topological sort for the graph. None of the paths in G2 orrespond to a valid topological sort of that graphs vertices.

In G3, the ordering 1, 2, 3 corresponds to both a valid path (1 -> 2 -> 3) and a valid topological sort.

2. Input File Format

Each input file contains a single digraph. The first line contains a single integer, n >= 2, indicating the number of vertices in the graph. (Vertices in these graphs are numbered 1 through n.) The following n lines are the adjacency lists for each successive vertex in the graph, with a small twist: each adjacency list begins with a single non-negative integer, k, indicating the number of vertices that follow. The list of vertices that follows will contain k distinct integers (i.e., no repeats) on the range 1 through n. For example, the following text files correspond to the graphs G1, G2, and G3 that are pictured above:

g1.txt
4
1 3
2 3 4
1 4
0
g2.txt
4
1 2
1 3
1 4
1 2
g3.txt
3
1 2
1 3
0

3. Special Restriction: Runtime Requirement

Please note that you must implement a solution that is O(n2) where n = |V|. Recall from our formal definition of big-oh that a faster solution is still considered O(n2).

4. Method and Class Requirements

Implement the following methods in a class named TopoPath . Notice that all these methods are both public and static.

public static boolean hasTopoPath(String filename)
Open filename and process the graph it contains. If the graph has a topopath (explained above), return true. Otherwise, return false. The string filename will refer to an existing file, and it will follow the format indicated above. You may have your method throw exceptions as necessary. Note that this function must execute in O(n2) time.

public static double difficultyRating()
Return a double on the range 1.0 (ridiculously easy) through 5.0 (insanely difficult).

public static double hoursSpent()
Return an estimate (greater than zero) of the number of hours you spent on this assignment.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.