00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "ADH_Graph.h"
00011 #include "ADH_Graph_Lib.h"
00012
00013 #include <algorithm>
00014
00015 namespace ADH {
00016
00017
00018 std::ostream& operator<< (std::ostream &COUT, const Graph& G) {
00019 Graph::Rep::const_iterator it = G.m_DICC.begin();
00020 for ( ; it != G.m_DICC.end(); ++it ) {
00021 COUT << it->first << " ==> [";
00022 Graph::mapped_type::const_iterator jt = it->second.begin();
00023 while ( jt != it->second.end() ) {
00024 COUT << '<' << jt->second << '>' << jt->first ;
00025 ++jt;
00026 if ( jt != it->second.end() ) {
00027 COUT << " , ";
00028 }
00029 }
00030 COUT << ']' << std::endl;
00031 }
00032 return COUT;
00033 }
00034
00035
00036 void dump( std::ostream & COUT, const Graph& G ) {
00037 Graph::Rep::const_iterator it = G.m_DICC.begin();
00038 for ( ; it != G.m_DICC.end(); ++it ) {
00039 Graph::Rep::const_iterator jt = G.m_DICC.begin();
00040 for ( ; jt != G.m_DICC.end(); ++jt ) {
00041 int val = 666;
00042 if ( G.isArc( it->first , jt->first , val ) ) {
00043 COUT << it->first << "->" << jt->first << " == " << val << std::endl;
00044 }
00045 }
00046 }
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 bool connected_set(
00061 const Graph & G ,
00062 std::set< std::string > & S ,
00063 const std::string & src ,
00064 const std::string & dst ,
00065 std::list< std::string > & C
00066 ) {
00067 if ( src == dst ) {
00068 C.push_back( dst );
00069 return true;
00070 }
00071 std::list< std::string > L;
00072 std::list< std::string >::const_iterator it;
00073 G.vertexList ( src , L );
00074
00075 for ( it=L.begin(); it!=L.end(); ++it ) {
00076 if ( S.find( *it ) != S.end() ) {
00077 continue;
00078 }
00079 S.insert( *it );
00080 C.push_back( src );
00081 if ( connected_set( G, S, *it, dst , C ) ) {
00082 return true;
00083 }
00084 else {
00085 C.pop_back();
00086 }
00087 }
00088 return false;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 bool connected(
00106 const Graph & G ,
00107 const std::string & src ,
00108 const std::string & dst ,
00109 std::list< std::string > & C
00110 ) {
00111 using std::string;
00112 std::set< std::string > S;
00113 C.clear();
00114 if ( ! ( G.isVertex(src) && G.isVertex(dst) ) ) {
00115 return false;
00116 }
00117 if ( connected_set ( G, S, src , dst , C ) ) {
00118
00119 return true;
00120 }
00121 return false;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 bool isConnected( const Graph & G ) {
00134 std::list< std::string > TMP;
00135 std::list< std::string > G_Vertices;
00136 G.vertexList(G_Vertices);
00137 std::list< std::string >::const_iterator i , j ;
00138
00139 for ( i = G_Vertices.begin(); i != G_Vertices.end() ; ++i ) {
00140 for ( j = G_Vertices.begin(); j != G_Vertices.end() ; ++j ) {
00141
00142 if ( ! connected( G , *i , *j , TMP ) ) {
00143 return false;
00144 }
00145 }
00146 }
00147 return true;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 bool isCircuit( const Graph & G , std::list< std::string > &C ) {
00162
00163 if ( C.back() != C.front() ) {
00164 return false;
00165 }
00166
00167
00168 bool esCircuito = true;
00169 std::list< std::string >::iterator it2 = ++(C.begin());
00170 for (std::list< std::string >::iterator it1 = C.begin();
00171 it2 != C.end() && esCircuito; ++it1, ++it2
00172 ) {
00173 int ch;
00174 esCircuito = G.isArc(*it1, *it2, ch);
00175 }
00176 return esCircuito;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 bool isTree( const Graph& G ) {
00204
00205 std::list< std::string > G_Raices;
00206 std::list< std::string > G_Vertices;
00207 G.vertexList(G_Vertices);
00208
00209 for ( std::list< std::string >::iterator iter = G_Vertices.begin();
00210 iter != G_Vertices.end(); ++iter
00211 ) {
00212 bool esRaiz = true;
00213 std::list< std::string > TMP;
00214
00215
00216 for (std::list< std::string >::iterator iter2 = G_Vertices.begin();
00217 iter2 != G_Vertices.end() && esRaiz; ++iter2
00218 ) {
00219
00220 esRaiz = connected( G, *iter, *iter2, TMP );
00221 }
00222
00223 if (esRaiz) {
00224 G_Raices.push_back(*iter);
00225 }
00226 }
00227
00228 if ( G_Raices.size() != 1) {
00229 return false;
00230 }
00231
00232
00233 std::list< std::string > G_Visitados;
00234 for ( std::list< std::string >::iterator iter = G_Vertices.begin();
00235 iter != G_Vertices.end() ; ++iter
00236 ) {
00237 std::list< std::string > Visitados_Nodo;
00238 G.vertexList( *iter, Visitados_Nodo );
00239
00240 for ( std::list< std::string >::iterator iterT = Visitados_Nodo.begin();
00241 iterT != Visitados_Nodo.end(); ++iterT
00242 ) {
00243
00244 if ( G_Visitados.end() == std::find( G_Visitados.begin(),G_Visitados.end(), *iterT ) ) {
00245 G_Visitados.push_back(*iterT);
00246 } else {
00247 return false;
00248 }
00249 }
00250 }
00251
00252
00253
00254
00255 return ( G_Visitados.size() == G_Vertices.size()-1 );
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 bool spanningTree( const Graph & G , Graph & T ) {
00290 Graph RES;
00291 std::list<std::string> G_nodosUsados;
00292
00293 std::list<std::string> G_Vertices;
00294 G.vertexList(G_Vertices);
00295 for ( std::list< std::string >::iterator iter = G_Vertices.begin();
00296 iter != G_Vertices.end(); ++iter
00297 ) {
00298 std::list< std::string > G_Visitados;
00299 G.vertexList( *iter, G_Visitados );
00300
00301 if ( G_nodosUsados.end() == std::find(G_nodosUsados.begin(),G_nodosUsados.end(), *iter) ) {
00302 G_nodosUsados.push_back(*iter);
00303 }
00304
00305
00306 for ( std::list< std::string >::iterator iterT = G_Visitados.begin();
00307 iterT != G_Visitados.end(); ++iterT
00308 ) {
00309
00310 if ( G_nodosUsados.end() == std::find( G_nodosUsados.begin(),G_nodosUsados.end(), *iter ) ||
00311 G_nodosUsados.end() == std::find( G_nodosUsados.begin(),G_nodosUsados.end(), *iterT)
00312 ) {
00313 G_nodosUsados.push_back(*iterT);
00314
00315 RES.set(*iter, *iterT, 0);
00316 }
00317 }
00318 }
00319
00320
00321 if ( G_nodosUsados.size() == G_Vertices.size() ) {
00322 T = RES;
00323 return true;
00324 }
00325 else {
00326 return false;
00327 }
00328 }
00329
00330 }
00331
00332