2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 contact: julien.jorge@gamned.org
26 * \file graph_algorithm.tpp
27 * \brief Graph algorithms implementation.
28 * \author Julien Jorge
33/*---------------------------------------------------------------------------*/
36 * \param g Graph to scan.
37 * \param source Start_Vertexing vertex.
38 * \param events User's processings.
40template<class Graph, class Events>
41claw::breadth_scan<Graph, Events>::breadth_scan( const Graph& g,
42 const vertex_type& source,
44 : m_g(g), m_source(source), m_events(events)
47} // breadth_scan::breadth_scan() [constructor]
49/*---------------------------------------------------------------------------*/
51 * \brief Performs the scan.
53template<class Graph, class Events>
54void claw::breadth_scan<Graph, Events>::operator()()
56 coloration seen_vertices;
57 std::queue<vertex_type> pending_vertices;
58 vertex_type current_vertex;
59 std::vector<vertex_type> neighbourhood;
60 typename std::vector<vertex_type>::const_iterator it;
64 for (vertex_iterator it_v=m_g.vertex_begin(); it_v!=m_g.vertex_end(); ++it_v)
65 seen_vertices[*it_v] = 0;
67 seen_vertices[m_source] = 1;
68 pending_vertices.push( m_source );
70 while ( !pending_vertices.empty() )
72 current_vertex = pending_vertices.front();
73 m_events.start_vertex(current_vertex);
75 m_g.neighbours( current_vertex, neighbourhood );
77 for( it = neighbourhood.begin(); it != neighbourhood.end(); ++it )
79 if ( seen_vertices[*it] == 0 )
81 m_events.visit_edge(current_vertex, *it);
82 seen_vertices[*it] = 1;
86 pending_vertices.pop();
87 m_events.end_vertex( current_vertex );
88 seen_vertices[current_vertex] = 2;
90} // breadth_scan::operator()
94//****************************** depth_scan ***********************************
98 /*---------------------------------------------------------------------------*/
100 * \brief Constructor.
101 * \param g Graph to scan.
102 * \param events User's processings.
104template<class Graph, class Events>
105claw::depth_scan<Graph, Events>::depth_scan( const Graph& g, Events& events )
106 : m_g(g), m_events(events)
109} // depth_scan::depth_scan() [constructor]
111/*---------------------------------------------------------------------------*/
113 * \brief Performs the scan.
115template<class Graph, class Events>
116void claw::depth_scan<Graph, Events>::operator()()
118 coloration seen_vertices;
123 for (it=m_g.vertex_begin(); it!=m_g.vertex_end(); ++it)
124 seen_vertices[*it] = 0;
126 for (it = m_g.vertex_begin(); it!=m_g.vertex_end(); ++it)
127 if ( seen_vertices[*it] == 0 )
128 recursive_scan( *it, seen_vertices );
129} // depth_scan::operator()()
131/*---------------------------------------------------------------------------*/
133 * \brief Performs the recursive part of the scan.
135template<class Graph, class Events>
136void claw::depth_scan<Graph, Events>::recursive_scan
137( const vertex_type& s, coloration& seen_vertices )
139 std::vector<vertex_type> neighbourhood;
140 typename std::vector<vertex_type>::const_iterator it;
142 m_events.start_vertex(s);
143 seen_vertices[s] = 1;
145 m_g.neighbours( s, neighbourhood );
147 for( it = neighbourhood.begin(); it != neighbourhood.end(); ++it )
148 if ( seen_vertices[*it] == 0 )
150 m_events.visit_edge(s, *it);
151 recursive_scan( *it, seen_vertices );
154 m_events.end_vertex(s);
155 seen_vertices[s] = 2;
156} // depth_scan::operator()
163//********************** topological_sort ***********************************
173 /*---------------------------------------------------------------------------*/
175 * \brief Initialize the scan.
176 * \param g The graph that will be scanned.
179void claw::topological_sort<Graph>::init( const Graph& g )
181 m_result.resize( g.vertices_count() );
182 m_next_index = (int)g.vertices_count()-1;
183} // topological_sort::init()
186/*---------------------------------------------------------------------------*/
188void claw::topological_sort<Graph>::end_vertex( const vertex_type& s )
190 m_result[m_next_index] = s;
192} // topological_sort::end()
194/*---------------------------------------------------------------------------*/
196void claw::topological_sort<Graph>::operator()( const Graph& g )
198 claw::depth_scan< Graph, self_type > scan( g, *this );
200} // topological_sort::operator()()
202/*---------------------------------------------------------------------------*/
204const typename claw::topological_sort<Graph>::vertex_type &
205claw::topological_sort<Graph>::operator[](unsigned int index) const
207 return m_result[index];
208} // topological_sort::operator[]()
210/*---------------------------------------------------------------------------*/
212typename claw::topological_sort<Graph>::const_iterator
213claw::topological_sort<Graph>::begin() const
215 return m_result.begin();
216} // topological_sort::begin()
218/*---------------------------------------------------------------------------*/
220typename claw::topological_sort<Graph>::const_iterator
221claw::topological_sort<Graph>::end() const
223 return m_result.end();
224} // topological_sort::end()