Computer Science 1622
Computer Science II

Programming Assignment 2
Recursion and Backtracking (40 points)
Due Monday, January 11, 1998

Introduction

A graph is a method for representing a set of objects and whether those objects are related. In the simplest form of graph we have a set of objects (called vertices) and a set of relationships (called edges) that indicate that one object is related to another. For example, our set of objects could consist of several cities and towns (Duluth, Superior, TwinCities, EauClaire, Madison, Chicago) and the relationships could indicate whether there was a major highway connecting these cities directly. Therefore, for edges we might have Duluth-Superior, Duluth-TwinCities, TwinCities-EauClaire, Superior-EauClaire, EauClaire-Madison and Madison-Chicago. The corresponding graph might be shown like this:

   Duluth --- Superior
      |            \
      |             \
   TwinCities --- EauClaire
                      |
                      |
                    Madison --- Chicago

To represent a graph, generally we first give each of the vertices a number (e.g. Duluth=0, Superior=1, TwinCities=2,. EauClaire=3, Madison=4, and Chicago=5). Then we represent the relationship between the vertices using a 2D matrix that has a row and a column for each vertex. For each entry in the matrix we put a 1 if there is a relationship between the vertex corresponding to the row and the vertex corresponding to the column, and a 0 otherwise. For example, given the numbering shown above the matrix representing the relationships in this graph would be:

     0  1  2  3  4  5
   --------------------
 0 | 0  1  1  0  0  0 |
 1 | 1  0  0  1  0  0 |
 2 | 1  0  0  1  0  0 |
 3 | 0  1  1  0  1  0 |
 4 | 0  0  0  1  0  1 |
 5 | 0  0  0  0  1  0 |
   --------------------

The Problem

Given a graph such as the one above, an interesting question is what are the connected components of the graph? A connected component of a graph is a set of vertices such that for each pair of vertices in the set, there is a path connecting the two vertices. A path consists of a series of edges starting at one of the vertices in the pair and ending at the other vertex. For example, a path from Duluth to Madison in the graph above graph would be Duluth--Superior--EauClaire--Madison. For our city graph above, there is a path from each city to each other city, so all of the cities are in one connected component that includes all of the verticies (cities) in the graph.

To solve this problem we can use the following algorithm:

  Set the number of components seen so far (C) to 0.

  Mark all of the vertices in the graph as not visited yet.

  For each vertex V in the graph
    If V has not been visited yet
      Increment C
      * Mark V and all of the vertices that can be reached from V
	as having been visited and further that each is part of
	the component numbered C

  For each component from 1 to C
    Print all of the Vertices that are part of Component C

The one difficult part (*) of this algorithm is marking not only V as having been visited and as part of component C, but all the vertices that can be reached from V as part of that component. To solve this problem we use a method called Depth-First Search (DFS). In DFS, we ``visit'' each vertex starting with V. To visit a vertex we mark the vertex as having been visited and what its component number is, then we try to visit each of the vertices adjacent to that vertex that we haven't already visited. Following is the general DFS algorithm (which we call with the vertex V and the number of the new connected component C):

  Visit (W,CompNum)
    Mark vertex W as visited

    Set W's component number to CompNum

    For each vertex X where there is an edge from W to X
      If X has not already been visited
        Visit(X,CompNum)

Every vertex that is visited is a vertex that can be reached from the start vertex V.

In this program you will read in a graph from a file and print out the vertices that form each connected component of the graph. To do this you should implement the algorithms shown above.

Notes

Sample Input and Corresponding Output

Input:
6
0 1 1 0 0 0
1 0 0 1 0 0
1 0 0 1 0 0
0 1 1 0 1 0
0 0 0 1 0 1
0 0 0 0 1 0
5
0 0 0 0 0
0 0 1 0 0
0 1 0 1 1
0 0 1 0 1
0 0 1 1 0
8
0 0 1 0 0 0 0 0
0 0 0 1 0 1 0 1
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 1
0 0 1 0 0 0 1 0
0 1 0 1 0 0 0 1
0 0 0 0 1 0 0 0
0 1 0 1 0 1 0 0
0

Output:
Graph #1
  CC 1 vertices: 0 1 2 3 4 5

Graph #2
  CC 1 vertices: 0
  CC 2 vertices: 1 2 3 4

Graph #3
  CC 1 vertices: 0 2 4 6
  CC 2 vertices: 1 3 5 7

What To Hand In

Hand in a full lab report for your program (consult the Programming Assignment Report Guidelines on what to include in this). Also include a listing of the data you used to test your program (you should discuss in your lab report what test data you used and whether this fully tests your solution). Finally you should attach output for your data showing that your program performs correctly.