Loyola College in Maryland

CS 462 - Algorithm Analysis
Spring 2004


Loyola College > Department of Computer Science > Dr. James Glenn > CS 462 > Programming Projects > Project 3 - Graph

Due

Tuesday, April 13th at 11:59pm. Late projects will be assessed a 20% penalty for each day past the due date. Projects will not be accepted more than four days past the due date.

Objectives

Assignment

There are two parts to this assignment:

Your Graph class should model directed, weighted graphs in which the vertices are natural numbers. Your class should have the following constructors and methods.

In addition, your class must have a nested class Edge that models the directed, weighted edges. Edge should have methods Finally, your class must provide an iterator class called const_edge_iterator that can be used to iterate over the edges that leave any chosen vertex. The Graph methods associated with the iterator class are const_edge_iterator must support operators !=, *, and ++, so that the following code would display the indices of all the vertices that are adjacent to vertex number u.
for (Graph::const_edge_iterator v = g.begin(u); v != g.end(u); ++v)
  std::cout << (*v).getDestination() << std::endl;

Input and Output

For the second part of the project, you will read input from standard input and build a graph based on what you read. The input will consist of team names, one name per line. Each pair of lines represents the result of one game. The first team is the winner and the second team is the loser. You must build a graph that contains edges from the winners to the losers. The number of times one team beat another should be recorded as the weight of the corresponding edge.

After all the data has been read, you should write to standard output a summary of the data read. This summary should, for each team, list the team that first team beat, along with the number of times if greater than one. For example, if the input is

Virginia
Maryland
Virginia
Duke
Maryland
Duke
Virginia
Duke
then the output should be
Virginia
  Duke [2x]
  Maryland
Maryland
  Duke
Duke
(the order may be chosen arbitrarily as long as all the teams defeated by one team are grouped together).

Files

Hints

To create the nested Edge class, simply nest the definition of Edge in the definition of Graph. The skeleton of your header file would then be

class Graph
{
  public:
    class Edge
    {
      ...
    };

  ...
};
and the method definitions in the implementation file would look something like
void Graph::Edge::foo()
{
  ...
}

If you use an STL data structure to hold your edge lists then creating the iterators is easy. For example, if you have an array of vectors outgoing to hold the edges leaving each vertex, then your const_edge_iterator is the same as std::vector< Edge >::const_iterator and you can include the line

typedef std::vector< Edge >::const_iterator const_edge_iterator;
in the public section of your class definition. Your begin method could then simply return outgoing[src].begin().

Since your graph uses numbers for vertices and the input file uses strings, you will have to have some additional structures, separate from the graph, that keep track of which vertex numbers correspond to which team names and vice versa.

Grading

For full credit, your code must:

A special bonus may be awarded for implementations that are particularly fast.

Submissions

Submit all source code files necessary to build your code in a tar file (preferably compressed).