|
11 | 11 | #include <boost/graph/breadth_first_search.hpp> |
12 | 12 | #include <boost/graph/depth_first_search.hpp> |
13 | 13 |
|
| 14 | +#include <iterator> |
| 15 | +#include <memory> |
| 16 | +#include <optional> |
| 17 | +#include <queue> |
| 18 | +#include <vector> |
| 19 | + |
14 | 20 | #include "graph.hpp" |
15 | 21 |
|
16 | 22 | namespace nxpp { |
17 | 23 |
|
18 | 24 | namespace detail { |
19 | 25 |
|
| 26 | +enum class traversal_order { |
| 27 | + bfs, |
| 28 | + dfs |
| 29 | +}; |
| 30 | + |
| 31 | +template <typename GraphWrapper, traversal_order Order> |
| 32 | +class traversal_edges_view { |
| 33 | +public: |
| 34 | + using NodeID = typename GraphWrapper::NodeType; |
| 35 | + using value_type = std::pair<NodeID, NodeID>; |
| 36 | + |
| 37 | + traversal_edges_view(const GraphWrapper& graph, const NodeID& start) : graph(&graph), start(start) {} |
| 38 | + |
| 39 | + class iterator { |
| 40 | + public: |
| 41 | + using iterator_category = std::input_iterator_tag; |
| 42 | + using value_type = traversal_edges_view::value_type; |
| 43 | + using difference_type = std::ptrdiff_t; |
| 44 | + using reference = const value_type&; |
| 45 | + |
| 46 | + iterator() = default; |
| 47 | + |
| 48 | + iterator(const GraphWrapper& graph, const NodeID& start) |
| 49 | + : state(std::make_shared<traversal_state>(graph, start)) { |
| 50 | + advance(); |
| 51 | + } |
| 52 | + |
| 53 | + reference operator*() const { |
| 54 | + return *state->current; |
| 55 | + } |
| 56 | + |
| 57 | + const value_type* operator->() const { |
| 58 | + return &*state->current; |
| 59 | + } |
| 60 | + |
| 61 | + iterator& operator++() { |
| 62 | + advance(); |
| 63 | + return *this; |
| 64 | + } |
| 65 | + |
| 66 | + void operator++(int) { |
| 67 | + ++(*this); |
| 68 | + } |
| 69 | + |
| 70 | + friend bool operator==(const iterator& it, std::default_sentinel_t) { |
| 71 | + return !it.state || it.state->done; |
| 72 | + } |
| 73 | + |
| 74 | + friend bool operator==(std::default_sentinel_t sentinel, const iterator& it) { |
| 75 | + return it == sentinel; |
| 76 | + } |
| 77 | + |
| 78 | + private: |
| 79 | + using GraphType = typename GraphWrapper::GraphType; |
| 80 | + using VertexDesc = typename GraphWrapper::VertexDesc; |
| 81 | + using OutEdgeIterator = typename boost::graph_traits<GraphType>::out_edge_iterator; |
| 82 | + |
| 83 | + struct edge_cursor { |
| 84 | + VertexDesc vertex; |
| 85 | + OutEdgeIterator current; |
| 86 | + OutEdgeIterator end; |
| 87 | + }; |
| 88 | + |
| 89 | + struct traversal_state { |
| 90 | + traversal_state(const GraphWrapper& graph, const NodeID& start) |
| 91 | + : graph(&graph), |
| 92 | + colors(boost::num_vertices(graph.get_impl()), boost::white_color), |
| 93 | + current(std::nullopt), |
| 94 | + done(false) { |
| 95 | + const auto start_vertex = graph.get_id_to_bgl_map().at(start); |
| 96 | + colors[graph.get_vertex_index(start_vertex)] = boost::gray_color; |
| 97 | + if constexpr (Order == traversal_order::bfs) { |
| 98 | + bfs_queue.push(start_vertex); |
| 99 | + } else { |
| 100 | + push_dfs_vertex(start_vertex); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + void push_dfs_vertex(VertexDesc vertex) { |
| 105 | + auto [edge_it, edge_end] = boost::out_edges(vertex, graph->get_impl()); |
| 106 | + dfs_stack.push_back(edge_cursor{vertex, edge_it, edge_end}); |
| 107 | + } |
| 108 | + |
| 109 | + const GraphWrapper* graph; |
| 110 | + std::vector<boost::default_color_type> colors; |
| 111 | + std::queue<VertexDesc> bfs_queue; |
| 112 | + std::optional<edge_cursor> bfs_cursor; |
| 113 | + std::vector<edge_cursor> dfs_stack; |
| 114 | + std::optional<value_type> current; |
| 115 | + bool done; |
| 116 | + }; |
| 117 | + |
| 118 | + void advance() { |
| 119 | + if (!state || state->done) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + state->current.reset(); |
| 124 | + if constexpr (Order == traversal_order::bfs) { |
| 125 | + advance_bfs(); |
| 126 | + } else { |
| 127 | + advance_dfs(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + void advance_bfs() { |
| 132 | + while (!state->bfs_queue.empty()) { |
| 133 | + if (!state->bfs_cursor.has_value()) { |
| 134 | + const auto vertex = state->bfs_queue.front(); |
| 135 | + auto [edge_it, edge_end] = boost::out_edges(vertex, state->graph->get_impl()); |
| 136 | + state->bfs_cursor = edge_cursor{vertex, edge_it, edge_end}; |
| 137 | + } |
| 138 | + |
| 139 | + while (state->bfs_cursor->current != state->bfs_cursor->end) { |
| 140 | + const auto edge = *state->bfs_cursor->current; |
| 141 | + ++state->bfs_cursor->current; |
| 142 | + const auto child = boost::target(edge, state->graph->get_impl()); |
| 143 | + const auto child_index = state->graph->get_vertex_index(child); |
| 144 | + if (state->colors[child_index] == boost::white_color) { |
| 145 | + state->colors[child_index] = boost::gray_color; |
| 146 | + state->bfs_queue.push(child); |
| 147 | + state->current = value_type{ |
| 148 | + state->graph->get_node_id(state->bfs_cursor->vertex), |
| 149 | + state->graph->get_node_id(child) |
| 150 | + }; |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + state->colors[state->graph->get_vertex_index(state->bfs_cursor->vertex)] = boost::black_color; |
| 156 | + state->bfs_queue.pop(); |
| 157 | + state->bfs_cursor.reset(); |
| 158 | + } |
| 159 | + |
| 160 | + state->done = true; |
| 161 | + } |
| 162 | + |
| 163 | + void advance_dfs() { |
| 164 | + while (!state->dfs_stack.empty()) { |
| 165 | + auto& cursor = state->dfs_stack.back(); |
| 166 | + while (cursor.current != cursor.end) { |
| 167 | + const auto edge = *cursor.current; |
| 168 | + ++cursor.current; |
| 169 | + const auto child = boost::target(edge, state->graph->get_impl()); |
| 170 | + const auto child_index = state->graph->get_vertex_index(child); |
| 171 | + if (state->colors[child_index] == boost::white_color) { |
| 172 | + state->colors[child_index] = boost::gray_color; |
| 173 | + state->current = value_type{ |
| 174 | + state->graph->get_node_id(cursor.vertex), |
| 175 | + state->graph->get_node_id(child) |
| 176 | + }; |
| 177 | + state->push_dfs_vertex(child); |
| 178 | + return; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + state->colors[state->graph->get_vertex_index(cursor.vertex)] = boost::black_color; |
| 183 | + state->dfs_stack.pop_back(); |
| 184 | + } |
| 185 | + |
| 186 | + state->done = true; |
| 187 | + } |
| 188 | + |
| 189 | + std::shared_ptr<traversal_state> state; |
| 190 | + }; |
| 191 | + |
| 192 | + iterator begin() const { |
| 193 | + return iterator(*graph, start); |
| 194 | + } |
| 195 | + |
| 196 | + std::default_sentinel_t end() const { |
| 197 | + return {}; |
| 198 | + } |
| 199 | + |
| 200 | +private: |
| 201 | + const GraphWrapper* graph; |
| 202 | + NodeID start; |
| 203 | +}; |
| 204 | + |
20 | 205 | template <typename NodeID, typename EdgeWeight, bool Directed, typename EdgeRange> |
21 | 206 | Graph<NodeID, EdgeWeight, Directed> build_tree_from_edges(const NodeID& root, const EdgeRange& edges) { |
22 | 207 | Graph<NodeID, EdgeWeight, Directed> tree; |
@@ -381,6 +566,15 @@ auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, Verte |
381 | 566 | return edges; |
382 | 567 | } |
383 | 568 |
|
| 569 | +template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector> |
| 570 | +auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, VertexSelector>::bfs_edges_view(const NodeID& start) const { |
| 571 | + if (!has_node(start)) { |
| 572 | + throw std::runtime_error("Traversal failed: start node not found."); |
| 573 | + } |
| 574 | + |
| 575 | + return detail::traversal_edges_view<Graph, detail::traversal_order::bfs>(*this, start); |
| 576 | +} |
| 577 | + |
384 | 578 | template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector> |
385 | 579 | auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, VertexSelector>::bfs_tree(const NodeID& start) const { |
386 | 580 | if (!has_node(start)) { |
@@ -455,6 +649,15 @@ auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, Verte |
455 | 649 | return edges; |
456 | 650 | } |
457 | 651 |
|
| 652 | +template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector> |
| 653 | +auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, VertexSelector>::dfs_edges_view(const NodeID& start) const { |
| 654 | + if (!has_node(start)) { |
| 655 | + throw std::runtime_error("Traversal failed: start node not found."); |
| 656 | + } |
| 657 | + |
| 658 | + return detail::traversal_edges_view<Graph, detail::traversal_order::dfs>(*this, start); |
| 659 | +} |
| 660 | + |
458 | 661 | template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector> |
459 | 662 | auto Graph<NodeID, EdgeWeight, Directed, Multi, Weighted, OutEdgeSelector, VertexSelector>::dfs_tree(const NodeID& start) const { |
460 | 663 | if (!has_node(start)) { |
|
0 commit comments