#include "Graph.h" std::ostream & operator<<(std::ostream & out, const Graph * graph) { return graph->print(out); } std::ostream & operator<<(std::ostream & out, const Graph & graph) { return graph.print(out); } Graph& Graph:: operator=(const Graph& rhs ) { if ( this == &rhs ) return *this; adjList = rhs.adjList; return *this; } void Graph::mergeEdges( ) { for ( list< list >::iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::iterator this_class = ptr->begin(); if ( ptr->size() > 1 ) { string class_name = this_class->getName(); ++this_class; // skip the first one, 'cuz it's the header while ( this_class != ptr->end() ) { string this_name = this_class->getName(); list::iterator next = this_class; ++next; while ( next != ptr->end() ) { string next_name = next->getName(); if (this_name == next_name) { cout << "MERGING: <" << class_name << ", " << this_name << ">" << " and <" << class_name << ", " << next_name << ">" << endl; int this_wgt = this_class->getWeight(); int next_wgt = next->getWeight(); this_class->setWeight( this_wgt+next_wgt ); this_class->setEdgeType(merged); next = ptr->erase(next); } if ( next != ptr->end() ) ++next; } ++this_class; } } } } void Graph::printWeights(ostream & out) const { out << endl << "The WEIGHTS in the graph: " << endl; int edge_count = 0; for ( list< list >::const_iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::const_iterator this_class = ptr->begin(); string name = this_class->getName(); out << "Class: " << name << endl; if ( ptr->size() > 1 ) { ++this_class; // skip the first one, 'cuz it's the header out << "\tEdges: "; while ( this_class != ptr->end() ) { out << "(" << name << "," << this_class->getName() << "," << this_class->getEdgeType(); out << ", " << this_class->getEdgeCount(); int wgt = this_class->getWeight( ); out << "," << wgt; out << ") "; ++this_class; ++edge_count; if ( edge_count % 2 == 0 && this_class!=ptr->end() ) { out << endl << "\t"; } } out << endl; } } out << "Number of connections: " << edge_count << endl; } Edge Graph::removeLightestEdge() { bool first_time = true; int lightest; list::iterator light_ptr; list< list >::iterator list_ptr; string edge_source; for ( list< list >::iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::iterator this_class = ptr->begin(); if ( ptr->size() > 1 ) { ++this_class; // skip the first one, 'cuz it's the header while ( this_class != ptr->end() ) { int wgt = this_class->getWeight(); if (first_time) { lightest = wgt; light_ptr = this_class; edge_source = ptr->begin()->getName(); list_ptr = ptr; first_time = false; }// if if (this_class->getWeight() < lightest) { lightest = wgt; light_ptr = this_class; edge_source = ptr->begin()->getName(); list_ptr = ptr; }// if ++this_class; }// while }// if }// for if ( light_ptr != adjList.end() ) { Edge removedEdge(edge_source, light_ptr->getName(), light_ptr->getEdgeType(), 0, lightest); cout << "REMOVING LIGHT LINK: " << "<" << edge_source << ", " << (*light_ptr) << ">\t" << light_ptr->getEdgeType() << ", weight is: " << lightest << endl; list_ptr->erase(light_ptr); return removedEdge; }// if else { Edge removedEdge("", "", ""); return removedEdge; }// else } void Graph::setAllWeights(const CostModel& model) { for ( list< list >::iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::iterator this_class = ptr->begin(); if ( ptr->size() > 1 ) { ++this_class; // skip the first one, 'cuz it's the header while ( this_class != ptr->end() ) { EdgeTypes this_type = this_class->getEdgeType(0); int wgt = model(this_type)*this_class->getEdgeCount(); this_class->setWeight( wgt ); ++this_class; } } } } int Graph::addEdge (const Edge& edge) { bool found = false; int thisEdgeCount = 0; for ( list< list >::iterator ptr = adjList.begin(); ptr != adjList.end() && !found; ++ptr ) { list::iterator classes = ptr->begin(); if ( edge.getSrc() == classes->getName() ) { while ( !found && classes != ptr->end() ) { if ( classes->getName() == edge.getDest() && classes->getEdgeType() == edge.getType() ) { classes->incrEdgeCount(); thisEdgeCount = classes->getEdgeCount(); found = true; } ++classes; } if (!found) { Node new_node(edge.getDest(), edge.getType(), edge.getCount(), edge.getWeight()); new_node.setEdgeCount(edge.getCount()); ptr->push_back( new_node ); thisEdgeCount = edge.getCount(); found = true; } } } return (thisEdgeCount); } void Graph::remEdge (const Edge& edge) { list< list >::iterator ptr = adjList.begin(); list::iterator thisClass = ptr->begin(); while (thisClass->getName() != edge.getSrc() && ptr != adjList.end()) { ++ptr; thisClass=ptr->begin(); } if (ptr == adjList.end()) { throw string ("Source Node Not Found!"); } bool found = false; while ( (thisClass != ptr->end()) && !found) { if ( (thisClass->getName() == edge.getDest()) && (thisClass->getEdgeType() == edge.getType())) { ptr->erase(thisClass); found = true; }// if ++thisClass; }// while }// remEdge void Graph::insert(const Node& node) { string name = node.getName(); bool found = false; for ( list< list >::iterator ptr = adjList.begin(); ptr != adjList.end() && !found; ++ptr ) { list::const_iterator classes = ptr->begin(); if ( name == classes->getName() ) { found = true; } } // first param to constructor is number of items, // second param is the item to put in list if (not found) adjList.push_back( list(1, node) ); } std::ostream& Graph::print(std::ostream & out) const { for ( list< list >::const_iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::const_iterator this_class = ptr->begin(); string name = this_class->getName(); out << "Class: " << name << endl; if ( ptr->size() > 1 ) { ++this_class; // skip the first one, 'cuz it's the header out << "\tEdges: "; int edge_count = 0; while ( this_class != ptr->end() ) { out << "(" << name << "," << this_class->getName() << "," << this_class->getEdgeType(); if ( this_class->getEdgeCount() > 1 ) out << ", " << this_class->getEdgeCount(); out << ") "; ++this_class; ++edge_count; if ( edge_count % 2 == 0 && this_class!=ptr->end() ) { out << endl << "\t"; } } out << endl; } } return out; } void Graph::printEdgeCount() const { Node node; unsigned int sum = 0; int numberOfClasses = 0; int count[none]; for (int i = 0; i < none; ++i) { count[i] = 0; } computeEdgeCount (numberOfClasses, count); cout << "The number of nodes/classes in the graph are: " << numberOfClasses << endl; cout << "The edges in the graph are:" << endl; for (int i = inheritance; i < none; ++i) { cout << "\t" << EdgeType::convertToString( static_cast(i) ) << ":\t" << count[i] << endl; sum += count[i]; } cout << "\tTotal edges: " << sum << endl; } void Graph::computeEdgeCount (int& numberOfClasses, int count[none]) const { numberOfClasses = adjList.size(); for ( list< list >::const_iterator ptr = adjList.begin(); ptr != adjList.end(); ++ptr ) { list::const_iterator this_class = ptr->begin(); string name = this_class->getName(); if ( ptr->size() > 1 ) { ++this_class; // skip the first one, 'cuz it's the header while ( this_class != ptr->end() ) { count[this_class->getEdgeType(0)] += this_class->getEdgeCount(); ++this_class; } } } }