80 return _width + _height;
90 return index < _height;
100 return index >= _height;
122 return index - _height;
142 return column + _height;
158 return std::make_pair(first, second - _height);
160 return std::make_pair(second, first - _height);
181 template <
typename MatrixType,
typename StartNodesContainerType,
typename EndNodesContainerType>
183 const EndNodesContainerType& end_nodes,
bool reach_all, std::vector <bipartite_graph_bfs_node>& result)
185 typedef std::vector <bipartite_graph_bfs_node> node_vector_type;
186 typedef std::list <size_t> node_list_type;
188 const size_t size = dimensions.
size();
189 const size_t height = dimensions.
height();
191 assert(height <= matrix.size1());
192 assert(dimensions.
width() <= matrix.size2());
195 int needed_end_nodes = (reach_all ? end_nodes.size() : 1);
196 node_list_type unprocessed;
198 for (
typename node_vector_type::iterator iter = result.begin(); iter != result.end(); ++iter)
201 iter->predecessor = 0;
203 for (
typename StartNodesContainerType::const_iterator iter = start_nodes.begin(); iter != start_nodes.end(); ++iter)
205 result[*iter].distance = 0;
206 result[*iter].predecessor = *iter;
207 unprocessed.push_back(*iter);
209 for (
typename EndNodesContainerType::const_iterator iter = end_nodes.begin(); iter != end_nodes.end(); ++iter)
211 if (result[*iter].distance == 0)
213 result[*iter].distance = -2;
216 if (needed_end_nodes <= 0)
220 while (!unprocessed.empty())
222 size_t current_index = unprocessed.front();
223 int current_distance = result[current_index].distance;
224 unprocessed.pop_front();
226 if (dimensions.
is_row(current_index))
229 const size_t row = current_index;
230 for (
size_t neighbor_index = height; neighbor_index < size; ++neighbor_index)
232 const size_t column = neighbor_index - height;
233 if (!result[neighbor_index].is_reachable() && matrix(row, column))
235 if (result[neighbor_index].distance == -2)
238 result[neighbor_index].distance = current_distance + 1;
239 result[neighbor_index].predecessor = current_index;
241 if (needed_end_nodes <= 0)
244 unprocessed.push_back(neighbor_index);
250 assert(dimensions.
is_column(current_index));
253 const size_t column = current_index - height;
254 for (
size_t neighbor_index = 0; neighbor_index < height; ++neighbor_index)
256 const size_t row = neighbor_index;
257 if (!result[neighbor_index].is_reachable() && matrix(row, column))
259 if (result[neighbor_index].distance == -2)
262 result[neighbor_index].distance = current_distance + 1;
263 result[neighbor_index].predecessor = current_index;
265 if (needed_end_nodes <= 0)
268 unprocessed.push_back(neighbor_index);
Definition: algorithm.hpp:14
bool bipartite_graph_bfs(const MatrixType &matrix, const bipartite_graph_dimensions &dimensions, const StartNodesContainerType &start_nodes, const EndNodesContainerType &end_nodes, bool reach_all, std::vector< bipartite_graph_bfs_node > &result)
Definition: bipartite_graph_bfs.hpp:182
Definition: bipartite_graph_bfs.hpp:15
bool is_reachable() const
Definition: bipartite_graph_bfs.hpp:26
int distance
Combinatorial distance to the search root or -1 if not reachable.
Definition: bipartite_graph_bfs.hpp:17
size_t predecessor
Index of the predecessor in the search tree.
Definition: bipartite_graph_bfs.hpp:20
Definition: bipartite_graph_bfs.hpp:39
bool is_row(index_type index) const
Definition: bipartite_graph_bfs.hpp:88
size_t width() const
Definition: bipartite_graph_bfs.hpp:69
std::pair< size_t, size_t > indexes_to_coordinates(index_type first, index_type second) const
Definition: bipartite_graph_bfs.hpp:155
size_t size() const
Definition: bipartite_graph_bfs.hpp:78
size_t index_to_column(index_type index) const
Definition: bipartite_graph_bfs.hpp:119
bipartite_graph_dimensions(size_t height, size_t width)
Definition: bipartite_graph_bfs.hpp:50
index_type column_to_index(size_t column) const
Definition: bipartite_graph_bfs.hpp:140
index_type row_to_index(size_t row) const
Definition: bipartite_graph_bfs.hpp:130
size_t index_to_row(index_type index) const
Definition: bipartite_graph_bfs.hpp:108
size_t height() const
Definition: bipartite_graph_bfs.hpp:60
size_t index_type
Definition: bipartite_graph_bfs.hpp:40
bool is_column(index_type index) const
Definition: bipartite_graph_bfs.hpp:98