-


> > > > Programming Assignments > Assignment 6 - Graph Search

Objectives

Assignment

Write a program called Paths that reads a directed graph and then finds and displays paths in that graph by various methods. The name of the file containing the graph data will be the first command-line argument. The file will contain a positive integer on the first line giving the number of vertices in the graph and then zero or more lines where each line contains the index of the from vertex and then the to vertex for an edge. Each edge will be listed at most once and the indices will be separated by any amount of whitespace. There may also be leading and trailing whitespace on each line.

Subsequent command-line arguments specify which vertices to find paths between and the method to use to find the paths. Command-line arguments "-breadth", "-depth", or "-degree" indicate that paths should be found using

Those method specifiers may appear in any order and may be repeated. Following each method specifier will be the index of a vertex in the graph read to use as the from vertex and then zero or more indices of to vertices, which may include the from vertex and may be repeated.

The output of your program must be written to standard output with one line per path requested. That line should give the method used, the index of the from and to vertices, and the path in the format used in the example below (with "no path" in place of the path if there is no path from the from vertex to the to vertex). The output for the requested paths should appear in the order they were given on the command line.

Your program should run in time $O(a(n+m) + b(n^2 \log n) + p)$ where $a$ is the total number of times -breadth and -depth appear on the command line, $b$ is the number of times -degree appears, and $p$ is the total length of all the paths output.

If the input file or command-line arguments are not as specified then your program must behave gracefully: it must not crash or go into an infinite loop.

Your program and any program must not produce any error messages when run with valgrind --tool=memcheck --leak-check=yes -q.

We reserve the right to deduct points from submissions for violations of the following guidelines.

Example

If the file graph_4.in contains
4
0 1
0 3
1 3
1 2
2 0
2 3
then running ./Paths graph_4.in -breadth 0 3 -depth 0 3 -degree 0 3 should result in the following output:
-breadth 0->3: [0, 3]
-depth 0->3: [0, 1, 3]
-degree 0->3: [0, 1, 2, 3]

Files and Submissions

A suggested directed graph implementation is available in /home/httpd/html/zoo/classes/cs223/f2017/Assignments/Paths:
Valid HTML 4.01 Transitional