ColPack
GraphColoring/GraphCore.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002     Copyright (C) 2005-2008 Assefaw H. Gebremedhin, Arijit Tarafdar, Duc Nguyen,
00003     Alex Pothen
00004 
00005     This file is part of ColPack.
00006 
00007     ColPack is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU Lesser General Public License as published
00009     by the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ColPack is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with ColPack.  If not, see <http://www.gnu.org/licenses/>.
00019 ************************************************************************************/
00020 
00021 
00022 #include "ColPackHeaders.h"
00023 
00024 using namespace std;
00025 
00026 namespace ColPack
00027 {
00028         //Virtual Function 1102
00029         void GraphCore::Clear() 
00030         {
00031                 m_i_MaximumVertexDegree = _UNKNOWN;
00032                 m_i_MinimumVertexDegree = _UNKNOWN;
00033 
00034                 m_d_AverageVertexDegree = _UNKNOWN;
00035 
00036                 m_s_InputFile.clear();
00037 
00038                 m_vi_Vertices.clear();
00039                 m_vi_Edges.clear();
00040 
00041                 m_vd_Values.clear();
00042 
00043                 return;
00044         }
00045 
00046         //Public Function 1103
00047         int GraphCore::GetVertexCount()
00048         {
00049                 return(STEP_DOWN(m_vi_Vertices.size()));
00050         }
00051 
00052 
00053         //Public Function 1104
00054         int GraphCore::GetEdgeCount()
00055         {
00056                 return(m_vi_Edges.size()/2);
00057         }
00058 
00059 
00060         //Public Function 1105
00061         int GraphCore::GetMaximumVertexDegree()
00062         {
00063                 return(m_i_MaximumVertexDegree);
00064         }
00065 
00066 
00067         //Public Function 1106
00068         int GraphCore::GetMinimumVertexDegree()
00069         {
00070                 return(m_i_MinimumVertexDegree);
00071         }
00072 
00073 
00074         //Public Function 1107
00075         double GraphCore::GetAverageVertexDegree()
00076         {
00077                 return(m_d_AverageVertexDegree);
00078         }
00079 
00080 
00081         //Public Function 1108
00082         string GraphCore::GetInputFile()
00083         {
00084                 return(m_s_InputFile);
00085         }
00086 
00087         //Public Function 1109
00088         void GraphCore::GetVertices(vector<int> &output) const
00089         {
00090                 output = (m_vi_Vertices);
00091         }
00092 
00093 
00094         //Public Function 1110
00095         void GraphCore::GetEdges(vector<int> &output) const
00096         {
00097                 output = (m_vi_Edges);
00098         }
00099 
00100 
00101         //Public Function 1111
00102         void GraphCore::GetValues(vector<double> &output) const
00103         {
00104                 output = (m_vd_Values);
00105         }
00106 
00107         void GraphCore::GetVertexEdgeMap(map< int, map< int, int> > &output)
00108         {
00109 //cerr<<"IN GraphCore::GetVertexEdgeMa()"<<endl;
00110 //GraphColoringInterface::PrintVertexEdgeMap(m_vi_Vertices, m_vi_Edges, m_mimi2_VertexEdgeMap);
00111 //cerr<<"OUT GraphCore::GetVertexEdgeMa()"<<endl;
00112                 output = (m_mimi2_VertexEdgeMap);
00113         }
00114 
00115         void GraphCore::GetDisjointSets(DisjointSets &output)
00116         {
00117 //cerr<<"START In Graph ds_DisjointSets.Print()"<<endl;
00118 //m_ds_DisjointSets.Print();
00119 //cerr<<"END ds_DisjointSets.Print()"<<endl;
00120 //Pause();
00121                 output = (m_ds_DisjointSets);
00122         }
00123 
00124         void GraphCore::GetD1Neighbor(int VertexIndex, vector<int> &D1Neighbor, int excludedVertex) {
00125                 if(VertexIndex > (int)m_vi_Vertices.size() - 2) {
00126                         cout<<"Illegal request. VertexIndex is too large. VertexIndex > m_vi_Vertices.size() - 2"<<endl;
00127                         return;
00128                 }
00129                 if(VertexIndex < 0) {
00130                         cout<<"Illegal request. VertexIndex is too small. VertexIndex < 0"<<endl;
00131                         return;
00132                 }
00133                 D1Neighbor.clear();
00134                 for(int i=m_vi_Vertices[VertexIndex]; i<m_vi_Vertices[STEP_UP(VertexIndex)]; i++) {
00135                         if( excludedVertex == m_vi_Edges[i]) continue;
00136                         D1Neighbor.push_back(m_vi_Edges[i]);
00137                 }
00138         }
00139 
00141         void GraphCore::PrintVertexD1Neighbor(int VertexIndex, int excludedVertex) {
00142                 if(VertexIndex > (int)m_vi_Vertices.size() - 2) {
00143                         cout<<"Illegal request. VertexIndex is too large. VertexIndex > m_vi_Vertices.size() - 2"<<endl;
00144                         return;
00145                 }
00146                 if(VertexIndex < 0) {
00147                         cout<<"Illegal request. VertexIndex is too small. VertexIndex < 0"<<endl;
00148                         return;
00149                 }
00150                 cout<<"Distance-1 neighbors of "<<VertexIndex<<" are (0-based): ";
00151                 for(int i=m_vi_Vertices[VertexIndex]; i<m_vi_Vertices[STEP_UP(VertexIndex)]; i++) {
00152                         if( excludedVertex == m_vi_Edges[i]) continue;
00153                         cout<<m_vi_Edges[i]<<" ";
00154                 }
00155                 cout<<"( # of edges = "<<m_vi_Vertices[STEP_UP(VertexIndex)] - m_vi_Vertices[VertexIndex]<<")"<<endl;
00156         }
00157 
00159         void GraphCore::PrintVertexD2Neighbor(int VertexIndex) {
00160                 cout<<"--Distance-1 neighbors of "<<VertexIndex<<" are: --------------------------"<<endl;
00161                 for(int i=m_vi_Vertices[VertexIndex]; i<m_vi_Vertices[STEP_UP(VertexIndex)]; i++) {
00162                         PrintVertexD1Neighbor(m_vi_Edges[i], VertexIndex);
00163                 }
00164                 cout<<"----------------------------------------------------"<<endl;
00165         }
00166 
00168 
00174         bool GraphCore::AreD2Neighbor(int VertexIndex1, int VertexIndex2) {
00175                 set<int> D1_of_VertexIndex1, D1_of_VertexIndex2;
00176                 vector<int> Intersect_set;
00177 
00178                 for(int i=m_vi_Vertices[VertexIndex1]; i<m_vi_Vertices[STEP_UP(VertexIndex1)]; i++)
00179                         D1_of_VertexIndex1.insert(m_vi_Edges[i]);
00180                 for(int i=m_vi_Vertices[VertexIndex2]; i<m_vi_Vertices[STEP_UP(VertexIndex2)]; i++)
00181                         D1_of_VertexIndex2.insert(m_vi_Edges[i]);
00182 
00183                 Intersect_set.resize(D1_of_VertexIndex1.size(),-1);
00184                 set_intersection(D1_of_VertexIndex1.begin(), D1_of_VertexIndex1.end(),
00185                                                 D1_of_VertexIndex2.begin(), D1_of_VertexIndex2.end(),
00186                                                 Intersect_set.begin()   );
00187                 int size = Intersect_set.size();
00188                 while(Intersect_set[size-1] == -1 && size >= 1) size--;
00189                 Intersect_set.resize(size,-1);
00190 
00191 
00192                 if(size>0) {
00193                         //Print
00194                         printf("%d and %d connected through vertices: ", VertexIndex1, VertexIndex2);
00195                         copy(Intersect_set.begin(), Intersect_set.end(), ostream_iterator<int>(cout, " "));
00196                         cout << endl;
00197                         return true;
00198                 }
00199                 return false;
00200 
00201                 /*
00202                 //Print
00203                 printf("%d and %d connected through vertices: ", VertexIndex1, VertexIndex2);
00204                 set_intersection(D1_of_VertexIndex1.begin(), D1_of_VertexIndex1.end(),
00205                                                 D1_of_VertexIndex2.begin(), D1_of_VertexIndex2.end(),
00206                                                 ostream_iterator<int>(cout, " ")        );
00207                 cout << endl;
00208                 //*/
00209         }
00210         
00211         bool GraphCore::operator==(const GraphCore &other) const {
00212                 // Check for self-assignment!
00213                 if (this == &other)      // Same object?
00214                   return true;        // Yes, so the 2 objects are equal
00215                   
00216                 //Compare vector<int> m_vi_Vertices; vector<int> m_vi_Edges; vector<double> m_vd_Values;
00217                 vector<int> other_Vertices, other_Edges;
00218                 vector<double> other_Values;
00219                 
00220                 other.GetVertices(other_Vertices);
00221                 other.GetEdges(other_Edges);
00222                 other.GetValues(other_Values);
00223                 
00224                 /*
00225                 if(m_vi_Vertices==other_Vertices) cout<<"m_vi_Vertices==other_Vertices"<<endl;
00226                 else  cout<<"m_vi_Vertices!=other_Vertices"<<endl;
00227                 
00228                 if(m_vi_Edges==other_Edges) cout<<"m_vi_Edges==other_Edges"<<endl;
00229                 else  cout<<"m_vi_Edges!=other_Edges"<<endl;
00230                 
00231                 if(m_vd_Values==other_Values) cout<<"m_vd_Values==other_Values"<<endl;
00232                 else  cout<<"m_vd_Values!=other_Values"<<endl;
00233                 //*/
00234                 
00235                 if(m_vi_Vertices==other_Vertices && m_vi_Edges==other_Edges && m_vd_Values==other_Values ) return true;
00236                 else return false;
00237 
00238         }
00239         
00240         bool GraphCore::areEqual(const GraphCore &other, bool structureOnly) const {
00241                 // Check for self-assignment!
00242                 if (this == &other)      // Same object?
00243                   return true;        // Yes, so the 2 objects are equal
00244                   
00245                 //Compare vector<int> m_vi_Vertices; vector<int> m_vi_Edges; vector<double> m_vd_Values;
00246                 vector<int> other_Vertices, other_Edges;
00247                 vector<double> other_Values;
00248                 
00249                 other.GetVertices(other_Vertices);
00250                 other.GetEdges(other_Edges);
00251                 if (!structureOnly) other.GetValues(other_Values);
00252                 
00253                 /*
00254                 if(m_vi_Vertices==other_Vertices) cout<<"m_vi_Vertices==other_Vertices"<<endl;
00255                 else  cout<<"m_vi_Vertices!=other_Vertices"<<endl;
00256                 
00257                 if(m_vi_Edges==other_Edges) cout<<"m_vi_Edges==other_Edges"<<endl;
00258                 else  cout<<"m_vi_Edges!=other_Edges"<<endl;
00259                 
00260                 if(m_vd_Values==other_Values) cout<<"m_vd_Values==other_Values"<<endl;
00261                 else  cout<<"m_vd_Values!=other_Values"<<endl;
00262                 //*/
00263                 
00264                 if(!structureOnly) {
00265                   if(m_vi_Vertices==other_Vertices && m_vi_Edges==other_Edges && m_vd_Values==other_Values ) return true;
00266                   else return false;
00267                 }
00268                 else { //structureOnly
00269                   if(m_vi_Vertices==other_Vertices && m_vi_Edges==other_Edges ) return true;
00270                   else return false;
00271                 }
00272 
00273         }
00274 
00275 
00276 
00277 
00278 
00279 }
00280 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines