Assignment 6: Breadth-First Search

Objectives

Introduction

Recall the Travelling Salesperson assignment. We wish to add the following heuristic to the TSP program: We can implement this heuristic with an undirected graph. The partial tour is represented by a graph, which initially has no edges in it. Adding a segment is adding an edge to the graph. Checking if two cities are already connected is checking if there is already a path between them in the graph. Checking how many pairs each city has already been included in is checking the degree of the corresponding vertices. Finally, to construct the final tour, you can find a path between the two vertices with degree 1.

Assignment

Part 1

Complete the implementation of a adjacency-list-based undirected graph in /c/cs223/hw6/Optional/lugraph.c to implement all of the functions declared in /c/cs223/hw6/Required/lugraph.h. In particular, complete the following functions:

The lugraph_connected and lugraph_bfs functions should run in $O(n + m)$ time, where $n$ is the number of vertices in the graph and $m$ is the number of edges. The lug_search_distance functions should run in $O(1)$ time and the lug_search_path function should run in $O(k)$ time where $k$ is the number of edges in the returned path.

Your implementations should check those preconditions that it is possible to check in $O(1)$ time and return something appropriate so that programs do not crash or hang when they are not met.

There is an incomplete set of unit tests in /c/cs223/hw6/Required/lugraph_unit.c. Your makefile must create an executable called Unit that starts with the unit tests' main.

Part 2

Modify your TSP program to implement the above heuristic algorithm when -greedy is given as a method name. The tour output should start with the first city in the input file and should be ordered in the same way as for -insert. The -greedy method should execute in $O(n^3)$ time. All input and output formats remain the same. You will only be tested on the output of the -greedy method.

Example

[jrg94@cicada code]$ for i in `seq 1 17`; do ./Unit $i; done
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
[jrg94@cicada code]$ ./TSP /c/cs223/hw2/Tests/8_cities.in -greedy
-greedy         :   9711.84 SEA YVR BWI DCA STL MCI DAL IAH SEA

Submissions

Submit a makefile with a default target that builds both the TSP and Unit executables, your log file, and your source code necessary to build the executables. Do not submit the provided location module or the lugraph.h header file. Your makefile should assume that those files have been copied into the current directory, but not that they are still in their original locations.