1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__DataStructures__MinimumSpanningTree__hxx__
6 #define __fpa__DataStructures__MinimumSpanningTree__hxx__
8 // -------------------------------------------------------------------------
9 template< class _TVertex, class _Superclass >
10 const typename fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
11 TCollisions& fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
12 GetCollisions( ) const
14 return( this->m_Collisions );
17 // -------------------------------------------------------------------------
18 template< class _TVertex, class _Superclass >
19 void fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
20 SetCollisions( const TCollisions& collisions )
22 static const unsigned long _inf =
23 std::numeric_limits< unsigned long >::max( );
24 if( this->m_Collisions == collisions )
27 this->m_Collisions = collisions;
29 // Prepare a front graph
30 unsigned long N = this->m_Collisions.size( );
31 _TMatrix dist( N, _TRow( N, _inf ) );
32 this->m_FrontPaths = dist;
33 for( unsigned long i = 0; i < N; ++i )
35 for( unsigned long j = 0; j < N; ++j )
37 if( this->m_Collisions[ i ][ j ].second )
41 this->m_FrontPaths[ i ][ j ] = j;
42 this->m_FrontPaths[ j ][ i ] = i;
48 this->m_FrontPaths[ i ][ i ] = i;
52 // Use Floyd-Warshall to compute all possible paths between fronts
53 for( unsigned long k = 0; k < N; ++k )
55 for( unsigned long i = 0; i < N; ++i )
57 for( unsigned long j = 0; j < N; ++j )
59 // WARNING: you don't want a numeric overflow!!!
60 unsigned long dik = dist[ i ][ k ];
61 unsigned long dkj = dist[ k ][ j ];
62 unsigned long sum = _inf;
63 if( dik < _inf && dkj < _inf )
66 // Ok, continue Floyd-Warshall
67 if( sum < dist[ i ][ j ] )
70 this->m_FrontPaths[ i ][ j ] = this->m_FrontPaths[ i ][ k ];
79 this->m_Seeds.resize( N );
83 // -------------------------------------------------------------------------
84 template< class _TVertex, class _Superclass >
85 void fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
88 this->m_Seeds.clear( );
92 // -------------------------------------------------------------------------
93 template< class _TVertex, class _Superclass >
94 void fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
95 AddSeed( const _TVertex& seed, unsigned long fId )
97 this->m_Seeds[ fId - 1 ] = seed;
101 // -------------------------------------------------------------------------
102 template< class _TVertex, class _Superclass >
103 typename fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
104 TVertices fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
105 GetAxis( const _TVertex& a ) const
109 _TVertex p = this->GetParent( it );
112 vertices.push_back( it );
114 p = this->GetParent( it );
117 vertices.push_back( it );
121 // -------------------------------------------------------------------------
122 template< class _TVertex, class _Superclass >
123 typename fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
124 TVertices fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
125 GetAxis( const _TVertex& a, const _TVertex& b ) const
127 static const unsigned long _inf =
128 std::numeric_limits< unsigned long >::max( );
131 TVertices pa = this->GetAxis( a );
132 TVertices pb = this->GetAxis( b );
133 if( pa.size( ) > 0 && pb.size( ) > 0 )
135 // Find front identifiers
136 unsigned long ia = _inf, ib = _inf;
137 unsigned long N = this->m_Seeds.size( );
138 for( unsigned long i = 0; i < N; ++i )
140 if( this->m_Seeds[ i ] == pa[ pa.size( ) - 1 ] )
142 if( this->m_Seeds[ i ] == pb[ pb.size( ) - 1 ] )
147 // Check if there is a front-jump between given seeds
150 // Compute front path
151 std::vector< long > fpath;
152 fpath.push_back( ia );
155 ia = this->m_FrontPaths[ ia ][ ib ];
156 fpath.push_back( ia );
160 // Continue only if both fronts are connected
161 unsigned int N = fpath.size( );
164 // First path: from start vertex to first collision
165 vertices = this->GetAxis(
166 a, this->m_Collisions[ fpath[ 0 ] ][ fpath[ 1 ] ].first
169 // Intermediary paths
170 for( unsigned int i = 1; i < N - 1; ++i )
174 this->m_Collisions[ fpath[ i - 1 ] ][ fpath[ i ] ].first,
175 this->m_Collisions[ fpath[ i + 1 ] ][ fpath[ i ] ].first
177 for( long id = 0; id < ipath.size( ); ++id )
178 vertices.push_back( ipath[ id ] );
182 // Final path: from last collision to end point
185 this->m_Collisions[ fpath[ N - 1 ] ][ fpath[ N - 2 ] ].first, b
187 for( long id = 0; id < lpath.size( ); ++id )
188 vertices.push_back( lpath[ id ] );
194 // Ignore common part: find common ancestor
195 long aIt = pa.size( ) - 1;
196 long bIt = pb.size( ) - 1;
198 while( aIt >= 0 && bIt >= 0 && cont )
200 cont = ( pa[ aIt ] == pb[ bIt ] );
209 for( long cIt = 0; cIt <= aIt; ++cIt )
210 vertices.push_back( pa[ cIt ] );
211 for( ; bIt >= 0; --bIt )
212 vertices.push_back( pb[ bIt ] );
220 // -------------------------------------------------------------------------
221 template< class _TVertex, class _Superclass >
222 fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
223 MinimumSpanningTree( )
228 // -------------------------------------------------------------------------
229 template< class _TVertex, class _Superclass >
230 fpa::DataStructures::MinimumSpanningTree< _TVertex, _Superclass >::
231 ~MinimumSpanningTree( )
235 #endif // __fpa__DataStructures__MinimumSpanningTree__hxx__