Implement a programs to walk through a graph in depth first or breadth first order. YOU MUST USE AN ABSTRACT DATA STRUCTURE which supports arbitrary numbers of edges per vertex. DO NOT use a matrix. Some of the graphs will be too large for a matrix.

The input will consist of the # of vertices, the # of edges, and then a series of edges. Each edge will connect vertex i to vertex j, and will have a length (ignore this for this assignment). As an example, we might have a 5 vertex graph, with 4 edges. I'll name the vertices A, B, C, D, E, for explanation, but they'll actually be numbered 0 through 4. A connects to B with a length of 10, B connects to C with length 3, A connects to D with length 5, A connects to E with length 2. (Again, the lengths don't matter for this assignment). The input file looks like this:

5 4
0 1 10
1 2 3
0 3 5
0 4 2

The objective is to determine the number of connected components. You could do this easily by having a "visited" flag on each vertex. With a for loop, go through each vertex in order -- if it is not visited, mark it as visited, then run either DFS or BFS to mark all the vertices that are connected -- you have found one connected component. Continue on in your for loop to the next unmarked vertex, and do the same thing.

Your program should print out the total number of connected components in the input file (just a single number to print).

There are 6 data files to try:

There's a simple C program to read in and print out a graph (with luck, there will be less weirdness in the graphs people use.).