GraphAlgorithms.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _GraphAlgorithms_h_
00010 #define _GraphAlgorithms_h_
00011
00012
00013 #include "iostream"
00014 #include "map"
00015 #include "set"
00016 #include "list"
00017 #include <stdlib.h>
00018
00019 using namespace std;
00020
00021
00022
00023
00024
00025
00026
00027 template < class Graph >
00028 map< int , typename Graph::edge_type > getGeodesicTree_in( const Graph& graph , int init_st )
00029 {
00030 typedef typename Graph::state_type state_type;
00031 typedef typename Graph::edge_type edge_type;
00032 map< int , edge_type > result;
00033
00034 const map< int , state_type >& theStates = graph.getStates( );
00035
00036 if( theStates.find( init_st )==theStates.end( ) ) {
00037 cout << "Nonexisting initial state. (9482)" << endl;
00038 exit( 1 );
00039 }
00040
00041 set< int > checked_pts;
00042 set< int > cur_pts;
00043 set< int > new_pts;
00044 new_pts.insert( init_st );
00045 result[init_st] = edge_type( );
00046
00047 while( new_pts.size( ) ) {
00048
00049 cur_pts = new_pts;
00050 new_pts.clear( );
00051 while( cur_pts.size( ) ) {
00052
00053 int cur_st = *cur_pts.begin( );
00054 cur_pts.erase( cur_pts.begin( ) );
00055 checked_pts.insert( cur_st );
00056
00057 typename map< int , state_type >::const_iterator st_it = theStates.find( cur_st );
00058 if( st_it==theStates.end( ) ) {
00059 cout << "Unexpected situation in getGeodesicTree. (0276)" << endl;
00060 exit( 1 );
00061 }
00062 const state_type& cur_state = (*st_it).second;
00063 const set< edge_type >& in = cur_state.in;
00064 typename set< edge_type >::const_iterator in_it = in.begin( );
00065 for( ; in_it!=in.end( ) ; ++in_it ) {
00066
00067 int new_st = (*in_it).target;
00068 if( checked_pts.find(new_st)==checked_pts.end() && cur_pts.find(new_st)==cur_pts.end() && new_pts.find(new_st)==new_pts.end() ) {
00069 new_pts.insert( new_st );
00070 edge_type edge = *in_it;
00071 edge.target = cur_st;
00072 result[new_st] = edge;
00073 }
00074 }
00075 }
00076 }
00077
00078 return result;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 template < class Graph >
00088 map< int , typename Graph::edge_type > getGeodesicTree_out( const Graph& graph , int init_st )
00089 {
00090 typedef typename Graph::state_type state_type;
00091 typedef typename Graph::edge_type edge_type;
00092 map< int , edge_type > result;
00093
00094 const map< int , state_type >& theStates = graph.getStates( );
00095
00096 if( theStates.find( init_st )==theStates.end( ) ) {
00097 cout << "Nonexisting initial state. (9482)" << endl;
00098 exit( 1 );
00099 }
00100
00101 set< int > checked_pts;
00102 set< int > cur_pts;
00103 set< int > new_pts;
00104 new_pts.insert( init_st );
00105 result[init_st] = edge_type( );
00106
00107 while( new_pts.size( ) ) {
00108
00109 cur_pts = new_pts;
00110 new_pts.clear( );
00111 while( cur_pts.size( ) ) {
00112
00113 int cur_st = *cur_pts.begin( );
00114 cur_pts.erase( cur_pts.begin( ) );
00115 checked_pts.insert( cur_st );
00116
00117 typename map< int , state_type >::const_iterator st_it = theStates.find( cur_st );
00118 if( st_it==theStates.end( ) ) {
00119 cout << "Unexpected situation in getGeodesicTree. (0276)" << endl;
00120 exit( 1 );
00121 }
00122 const state_type& cur_state = (*st_it).second;
00123 const set< edge_type >& out = cur_state.out;
00124 typename set< edge_type >::const_iterator out_it = out.begin( );
00125 for( ; out_it!=out.end( ) ; ++out_it ) {
00126
00127 int new_st = (*out_it).target;
00128 if( checked_pts.find(new_st)==checked_pts.end() && cur_pts.find(new_st)==cur_pts.end() && new_pts.find(new_st)==new_pts.end() ) {
00129 new_pts.insert( new_st );
00130 edge_type edge = *out_it;
00131 edge.target = cur_st;
00132 result[new_st] = edge;
00133 }
00134 }
00135 }
00136 }
00137
00138 return result;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 template < class Graph >
00148 map< int , int > getDistances_out( const Graph& graph , int init_st )
00149 {
00150 typedef typename Graph::state_type state_type;
00151 typedef typename Graph::edge_type edge_type;
00152 map< int , int > result;
00153
00154 const map< int , state_type >& theStates = graph.getStates( );
00155
00156 if( theStates.find( init_st )==theStates.end( ) ) {
00157 cout << "Nonexisting initial state. (9432)" << endl;
00158 exit( 1 );
00159 }
00160
00161 set< int > checked_pts;
00162 set< int > cur_pts;
00163 set< int > new_pts;
00164 new_pts.insert( init_st );
00165 result[init_st] = 0;
00166
00167 for( int dist=1 ; new_pts.size( ) ; ++dist ) {
00168 cur_pts = new_pts;
00169 new_pts.clear( );
00170 while( cur_pts.size( ) ) {
00171
00172 int cur_st = *cur_pts.begin( );
00173 cur_pts.erase( cur_pts.begin( ) );
00174 checked_pts.insert( cur_st );
00175
00176 typename map< int , state_type >::const_iterator st_it = theStates.find( cur_st );
00177 if( st_it==theStates.end( ) ) {
00178 cout << "Unexpected situation in getGeodesicTree. (89766)" << endl;
00179 exit( 1 );
00180 }
00181 const state_type& cur_state = (*st_it).second;
00182 const set< edge_type >& out = cur_state.out;
00183 typename set< edge_type >::const_iterator out_it = out.begin( );
00184 for( ; out_it!=out.end( ) ; ++out_it ) {
00185
00186 int new_st = (*out_it).target;
00187 if( checked_pts.find(new_st)==checked_pts.end() && cur_pts.find(new_st)==cur_pts.end() && new_pts.find(new_st)==new_pts.end() ) {
00188 new_pts.insert( new_st );
00189 result[new_st] = dist;
00190 }
00191 }
00192 }
00193 }
00194
00195 return result;
00196 }
00197
00198
00199
00200
00201
00202
00203
00205 template < class edge_type >
00206 list< edge_type > readoffGeodesicTree( const map< int , edge_type >& tree , int st_num )
00207 {
00208 list< edge_type > result;
00209
00210 while( 1 ) {
00211
00212 typename map< int , edge_type >::const_iterator t_it = tree.find( st_num );
00213 if( t_it==tree.end( ) ) {
00214 cout << "Error. Unpredicted situation in readoffGeodesicTree( ... )" << endl;
00215 exit( 1 );
00216 }
00217 if( (*t_it).second.target==-1 )
00218 break;
00219 result.push_back( (*t_it).second );
00220 st_num = (*t_it).second.target;
00221 }
00222
00223 return result;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 template < class LabelledGraph , class ConstIterator >
00233 int trace( const LabelledGraph& LG , int init , ConstIterator B , ConstIterator E )
00234 {
00235 typedef typename LabelledGraph::state_type state_type;
00236 typedef typename LabelledGraph::edge_type edge_type;
00237 const map< int , state_type >& theStates = LG.getStates( );
00238
00239 int cur_st = init;
00240 for( ; B!=E ; ++B ) {
00241
00242 typename map< int , state_type >::const_iterator st_it = theStates.find( cur_st );
00243 if( st_it==theStates.end( ) )
00244 return -1;
00245
00246 bool success = false;
00247 int label = *B;
00248 const state_type& cur_state = (*st_it).second;
00249 const set< edge_type >& out = cur_state.out;
00250 typename set< edge_type >::const_iterator out_it = out.begin( );
00251 for( ; out_it!=out.end( ) ; ++out_it ) {
00252 if( (*out_it).label == label ) {
00253 cur_st = (*out_it).target;
00254 success = true;
00255 break;
00256 }
00257 }
00258 if( !success )
00259 return -1;
00260 }
00261
00262 return cur_st;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 template < class LabelledGraph , class ConstIterator >
00272 pair< bool , list< typename LabelledGraph::edge_type > > trace_path( const LabelledGraph& LG , int init , ConstIterator B , ConstIterator E )
00273 {
00274 typedef typename LabelledGraph::state_type state_type;
00275 typedef typename LabelledGraph::edge_type edge_type;
00276 const map< int , state_type >& theStates = LG.getStates( );
00277
00278 int cur_st = init;
00279 pair< bool , list< edge_type > > result;
00280 result.first = true;
00281 for( ; B!=E ; ++B ) {
00282
00283 typename map< int , state_type >::const_iterator st_it = theStates.find( cur_st );
00284 if( st_it==theStates.end( ) )
00285 return pair< bool , list< edge_type > >( false , list< edge_type >( ) );
00286
00287 bool success = false;
00288 int label = *B;
00289 const state_type& cur_state = (*st_it).second;
00290 const set< edge_type >& out = cur_state.out;
00291 typename set< edge_type >::const_iterator out_it = out.begin( );
00292 for( ; out_it!=out.end( ) ; ++out_it ) {
00293 if( (*out_it).label == label ) {
00294 cur_st = (*out_it).target;
00295 success = true;
00296 result.second.push_back( *out_it );
00297 break;
00298 }
00299 }
00300 if( !success )
00301 return pair< bool , list< edge_type > >( false , list< edge_type >( ) );
00302 }
00303
00304 return result;
00305 }
00306
00307
00308 #endif
00309