|
|
Some Java projects PROJECT 1: Forming graphs. Listing prerequisites. Detecting cycles in the graph. The following is a Java that I completed yesterday i.e. on January 20th (2002). I found it interesting & decided to put it on the web. This was an assignment for my Algorithms (CS 341) course. It was just submitted today so I'm not sure how correct this solution was, although I tried to test & make sure that it works well. For the next stage of this program, I'd like to actually make a Java applet that runs on the web. It would actually show as the vectors & hashtables are being filled. It would also show the Depth-first search taking place. I think it'd be exciting to see those details. But for now, I'm just putting the "raw" code. It runs from a Console window. 1) Use the Makefile to compile the program by typing in make,
or javac CycleFind.java
Brief Explanation: Two main things done by the program is that if we choose the "-prereq" option then it will list all the courses that require the given course to be taken. If "-find" option is chosen then the program checks for cycles in the graph. If there is a cycle then it displays the cycle else it displays "No cycle found" message. See bottom of the page for sample input files & their output. The program models relationships between courses that one might need to take. Also note that there is a directed graph, where a directed edge exists from node A to node B if course B has course A as a prerequisite (i.e. course A must be taken before course B). For example, if CS341 requires both CS240 and Math239 as prerequisites, this can be shown this way:
The program also tests for such cycles in any such graph. If we ever find an edge from an active node to another active node, the program stops, and the directed cycle is printed out. Input:
Output:
Java Code
Testing screen shot for the files above:
Page updated on: 19-Feb-2002 03:40 PM |