void Graph::Check() { ResetVerticesMarks(); queue<Vertex*> bfs_queue; Vertex* root=source_vertices_[0]; bfs_queue.push(root); root->SetIsVisted(true); while (!bfs_queue.empty()){ Vertex* front = bfs_queue.front(); bfs_queue.pop(); for (int i=0; i<front->GetIncidentEdgesNum();++i){ Vertex* child = front->GetIncidentEdge(i)->GetNeighbor(front); if (child->GetType()==Vertex::Type::PSEUDO){ child = dynamic_cast<PseudoVertex*>(child)->GetBoundaryVertex(front->GetIncidentEdge(i)); } if (!child->GetIsVisited()){ child->SetIsVisted(true); bfs_queue.push(child); } } } for (auto pair:vertices_) { if (!pair.second->GetIsVisited()) cout <<"no connect!"<<endl; } }
Graph* Graph::Shrink() { root_ = source_vertices_[0]; MarkArticulationPoints(); // Clean all the articulation points on the paths between every pair of source // vertices to ensure that all the source vertices will not be merged into // pseudo vertices. for (auto source_vertex : source_vertices_) { if (source_vertex != root_) { Vertex* vertex = source_vertex; while (vertex != root_) { assert(vertex != nullptr); vertex->SetIsArticulate(false); vertex = vertex->GetParent(); } } } // Clean all the articulation points connecting to a resident directly to // avoid making useless pseudo vertices each containing just a single // resident. for (auto pair : vertices_) { Vertex* vertex = pair.second; for (int i = 0; i < vertex->GetIncidentEdgesNum(); ++i) { Vertex* neighbor = vertex->GetIncidentEdge(i)->GetNeighbor(vertex); if (neighbor->GetType() == Vertex::Type::RESIDENT) { vertex->SetIsArticulate(false); break; } } } Graph* tmp = ShrinkByArticulationPoints(); Graph* shrinked_graph = tmp->ShrinkBySwitches(); delete tmp; //shrinked_graph->Print(); return shrinked_graph; }
void Graph::CountCost (){ ResetVerticesMarks(); for (auto source:source_vertices_) { //bfs for each source; ResetVerticesMarks(); queue<Vertex*> bfs_queue; bfs_queue.push(source); source->SetIsVisted(true); string source_name = source ->GetRaw()->GetName(); vector <Vertex*> vector_leafs; //cout <<"Start:"<<source_name<<endl; while (!bfs_queue.empty()){ Vertex* front = bfs_queue.front(); bfs_queue.pop(); for (int i=0; i<front->GetIncidentEdgesNum();++i){ Vertex* child = front->GetIncidentEdge(i)->GetNeighbor(front); Edge* connect_edge = front->GetIncidentEdge(i); if (child->GetType()==Vertex::Type::PSEUDO){ child = dynamic_cast<PseudoVertex*>(child)->GetBoundaryVertex(front->GetIncidentEdge(i)); connect_edge = edges_[front->GetIncidentEdge(i)->GetRaw()->GetName()]; } if (child->GetIsVisited() || child->GetAssignSource()!=source_name){ continue; } child->SetIsVisted(true); child->SetParent(front); front->AddChild(child); bfs_queue.push(child); child->SetParentEdge(connect_edge); } if (front->GetChildrenNum() == 0){ vector_leafs.push_back(front); } else if (front->GetType() == Vertex::Type::RESIDENT ){ cout <<"error!!! :resident has more one edge"<<endl; } } /* for (auto pair: vertices_) { if (!pair.second->GetIsVisited() && pair.second->GetAssignSource()==source_name){ cout <<"someting wrong!!"<<endl; } } */ cout <<"process:"<<source_name<<" leafnum:"<<vector_leafs.size()<<endl; for (auto child : vector_leafs){ //Vertex* child = pair.second; if (child->GetType() == Vertex::Type::RESIDENT && child->GetAssignSource() == source_name) { //cout <<"IN!! "<<endl; double load_current = dynamic_cast<ResidentVertex*>(child)->GetConsumingPower()/child->GetVoltage(); //cout <<"load:"<< load_current<<endl; while (child!=source) { Vertex* parent = child->GetParent(); Edge* connect_edge = child->GetParentEdge(); assert(connect_edge->GetNeighbor(child)==parent); connect_edge->SetCurrent(connect_edge->GetCurrent()+load_current); child = parent; } } } //cout <<"finish:"<<source_name<<endl; } //cout <<"counting..."<<endl; wire_upgrade = 0.0; switch_change =0; for (auto pair:edges_) { if (pair.second->GetType()==Edge::Type::EDGE){ Wire* wire = pair.second->GetRaw(); if (pair.second->GetCurrent() > wire->GetCurrentLimit()){ //cout <<"Current:"<< pair.second->GetCurrent()<< " Limit:"<< wire->GetCurrentLimit()<<endl; wire_upgrade += pair.second->GetCurrent()-wire->GetCurrentLimit(); } }else if(pair.second->GetType()==Edge::Type::SWITCH){ Switch* switches = dynamic_cast<Switch*> (pair.second->GetRaw()); Vertex* a = pair.second->GetIncidentVertex(0); Vertex* b = pair.second->GetIncidentVertex(1); if ( (a->GetAssignSource() != b->GetAssignSource() && switches->GetIsOn()) || (a->GetAssignSource() == b->GetAssignSource() && !switches->GetIsOn() )) switch_change++; } } cout << "wire upgrade :" << wire_upgrade << endl; cout << "wire change :" << switch_change << endl; }