/* * Small test program for Graph class based very heavily on * command-line input. The program first puts in one vertex named * isolated all by itself, and then prompts the user to add edges, * entering *** when done. * Very minimal comments, because I wrote this to test my own class * then intend to throw it away. * * CS 202, November 2002 */ #include #include #include "Graph.hh" using namespace std; int main() { const string SENTINEL = "***"; const string PROMPT = "Please enter two vertices and a weight; the sentinel is "; Graph myGraph; cout << "Graph was declared as empty graph. Initial size is " << myGraph.vertexCount() << endl; const string isolatedVertex ="isolated"; myGraph.insertVertex( isolatedVertex); myGraph.insertVertex( isolatedVertex); cout << "Just called insertVertex twice to add one vertex." << endl; cout << "Both calls inserted special vertex name" << isolatedVertex << endl; string v1, v2; Weight weight; cout << PROMPT << SENTINEL << endl; cin >> v1; while (v1 != SENTINEL) { cin >> v2 >> weight; myGraph.insertEdge (v1, v2, weight); cout << PROMPT << SENTINEL << endl; cin >> v1; } // getting edges cout << "Edge count = " << myGraph.edgeCount() << endl; cout << "vertex count = " << myGraph.vertexCount()<< endl; Graph::iterator itr; for (itr = myGraph.begin(); itr != myGraph.end(); itr++) cout << "vertex = " << *itr << endl; string start; cout << "enter start vertex" << endl; cin >> start; list< string > neighbor_list = myGraph.getNeighbors (start); list< string >::iterator list_itr; for (list_itr = neighbor_list.begin(); list_itr != neighbor_list.end(); list_itr++) cout << (*list_itr) << endl; cout << "end of neighbors of start" << endl; cout << "vertex count = " << myGraph.vertexCount()<< endl; cout << "edge count = " << myGraph.edgeCount() << endl; cout << "contains edge a t is " << myGraph.containsEdge ("a", "t") << endl; cout << "contains edge a e is " << myGraph.containsEdge ("a", "e") << endl; cout << "Depth-first_reach iterating from start:" << endl; Graph::dfs_reach_iterator d_itr = myGraph.dfs_reach_begin (start); cout << "Sucessfully initialized begin dfs_reach_iterator" << endl; while ( d_itr != myGraph.dfs_reach_end() ) { cout << *d_itr << " "; d_itr++; } cout << endl; cout << endl; cout << "Depth-first_all iterating from start:" << endl; Graph::dfs_all_iterator dfsAllItr = myGraph.dfs_all_begin (start); while ( dfsAllItr != myGraph.dfs_all_end() ) { cout << *dfsAllItr << " "; dfsAllItr++; } cout << endl; cout << endl; cout << "printing all edges and their weights: " << endl; for (itr = myGraph.begin(); itr != myGraph.end(); itr++) { v1 = *itr; list my_list = myGraph.getNeighbors (v1); cout << "Edges starting at vertex " << v1 << ":" << endl; for (list_itr = my_list.begin(); list_itr != my_list.end(); list_itr++) { v2 = *list_itr; cout << "(" << v1 << ", " << v2 << "): " << myGraph.edgeWeight(v1, v2) << endl; } } // printing all edges with weights return 0; }