CMSC 341 Fall 1998 Project 4

1 Introduction

Your project will create a class for undirected graphs and provide several functions that are associated with a graph. Your program will read graph data from a file and create a Graph object containing the specified nodes and edges. Your program will then read commands from a command file and execute all commands found in the file.

Project Due Dates:

Section 101 -- Midnight, Wednesday 12/9/98

Section 201 -- Midnight, Tuesday 12/8/98

All source code and data files for this project are found in the directory

/afs/umbc.edu/users/d/e/dennis/pub/cs341/proj4

The header file for the graph class (graph.h) is as follows. Feel free to modify it as you see fit.

#include <iostream.h>

typedef char Node;
class Graph
{
    public:
	Graph ();
	~Graph ();

	// add an edge -- checks that verticies exist
	void addEdge (const Node& v1, const Node& v2, int weight);

	// add a Node and retrun it's id
	int addNode (const Node& v1);

	// perform depth-first search starting from
	// specified vertex
	void dfs (const Node& v1) const;

	// perform breadth-first search starting
	// from specified vertex
	void bfs (const Node& v1) const;

	// generate minimum spanning tree
	void mst () const;

	// single-source shortest path
	void shortpath (const Node& n1, const Node& n2) const;

	// output the graph
	friend ostream& operator<< (ostream& out, const Graph& g);

	// return the number of verticies -- if needed
	int getNumNodes() const;

    private:
	// your graph representation goes here


	// isAdjacent -- you probably need this
	// determines if 2 nodes in the graph are
	// adjacent... returns T/F (1/0)
	int isAdjacent (const Node& n1, const Node&n2) const;

	// other private data member go here
};



2 Details about Input File

The input file contains the data for an UNDIRECTED graph. An example graph:

The input file that represents this graph is:
5
A
B
C
D
E
8
A B 1
A C 2
B D 4
D E 3
C E 4
B E 1
D C 2
B C 2

The first line provides the number of nodes in the graph (let's call it N). This number is guaranteed to be less than 20.

The next N lines contain single character node designations. This data is stored in the node.

The next (N + 1st) line provides the number of edges, call it E. There is only the theoretical guarantee on the maximum size of E.

The next E lines contain the edge information. Each line consists of two node designators (single characters) and the edge's weight (an integer). Note that undirected edges are stored in both directions. That is, the line
A W 2
implies there is an edge W -- A as well as an edge A -- W, both with weight = 2.

When nodes designations are read from the file, a Node is creatd and added to the graph via the addNode() member function. When edges are read from the file, they are added to the graph via the addEdge() member function.

3 Details of Supported Functions

The description of the functions that will be supported by the graph datatype are as follows:

  1. Breadth First Search (Graph::bfs()):
    This function will take a Node as an argument. The function performs a breadth first search starting from that node. This function prints nodes in the order it encounters them. Eventually it should print all nodes in the graph if the graph is connected.
  2. Depth First Search (Graph::dfs()):
    This function will take a Node as an argument. The function performs a depth first search starting from that node. This function prints nodes in the order it encounters them. Eventually it should print all nodes in the graph, if the graph is connected.
  3. Minimum Spanning Tree (Graph::mst()):
    This function will print out the edges added to the minimum spanning tree and it will print the overall weight of the minimum spanning tree. This should be performed using Prim's algorithm
  4. Shortest Path (Graph::shortpath()):
    This function takes two Node arguments. The first argument is the source and the second argument is the destination. This function must print the total distance from the source to the destination and it must print the path from source to destination. This should be performed using Dijkstra's algorithm.
  5. Print the graph (operator<<):
    This function takes no arguments. It "prints" the graph by printing the verticies on a single line, followed by all the edges and their weights (one edge per line).

4 Details About Commands File

The commands file should have either B, D, M, P, S or E in the first column. If there is anything else, you should print an error and immediately exit out.

The explanations of the 6 characters are as follows:

5 Grading Details

Your program will be tested with two graph data files and two command files. Both graphs will be connected, undirected graphs. The usual 5-point bonus for projects submitted at least 24 hours early are available. The usual 5-point per penalties for the first 2 days late are assessed. No project will be accepted if more than 48 hours late. The stack and queue classes are provided for you. The files Stack.h, Stack.C, Q.h and Q.C are in the project directory. The breakdown of the grades for the different parts of the project are as follows:

  1. project4.C -- 15 points: This file will parse the input file and commands file and execute the appropriate member function of graph.
  2. Graph::Graph(), Graph::addEdge() and Graph::addNode() -- 15 points: The basic Graph class member functions.
  3. Graph::bfs() -- 10 points: This is the code for the breadth-first-search.
  4. Graph::dfs() -- 10 points: This file has the code for the depth-first-search.
  5. Graph::shortpath() -- 15 points: This is the code for the shortest path (Dijkstra's) algorithm.
  6. Graph::mst()-- 15 points: This is the code for the Minimum Spanning Tree (Prim's) algorithm.
  7. operator<< -- 5 points: This is the code that prints the graph. You'll want this for debugging too.
  8. Misc -- 15 points: This includes design, coding and readability
As usual, any project that does not "make" will be limited to a score of 50.

6 Submitting your project

You will submit your project in the usual way --
For section 101 --
      submit cs341-df1 proj4 <files>
For section 201 --
      submit cs341-df2 proj4 <files>

For this project, you are to submit ALL source files and and a makefile. In addition, you are to use the Unix script facility to run your command. After executing the script command, run your program twice.

  1. The first time, get your input from the file graph1.dat and your commands from the file cmds1.dat
  2. The second time, get your input from the file graph2.dat and your commands from the file cmds2.dat
Exit the script, then rename the file typescript to proj4.out. Be sure to submit this file as well

All files are located in
/afs/umbc.edu/users/d/e/dennis/pub/cs341/proj4