Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
BFSAlgorithm.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <queue>
4#include <memory>
8
10#include "SearchPath.hpp"
11#include "Neighborhood.hpp"
12
13namespace fishnet::graph::__impl{
14
24template<Graph G>
25static void bfs(const G & g, auto & searchResult, const typename G::node_type & start,fishnet::util::BiPredicate<typename G::node_type> auto const& predicate) {
26 using N = G::node_type;
27 std::queue<N> q;
28 q.push(start);
29 while(not q.empty()and not searchResult.stop()) {
30 N current = q.front();
31 q.pop();
32 searchResult.open(current);
33 for(auto const& neighbour : g.getNeighbours(current)){
34 searchResult.onEdge(current,neighbour);
35 if (searchResult.isUnknown(neighbour) and predicate(current,neighbour) ) {
36 searchResult.open(neighbour);
37 q.push(neighbour);
38 }
39 }
40 searchResult.close(current);
41 }
42}
43
52template<Graph G>
53static void bfs(const G & g, auto & searchResult, const typename G::node_type & start) {
54 bfs(g,searchResult,start,fishnet::util::TrueBiPredicate());
55}
56
65template<Graph G>
66static void bfs_all(const Graph auto & g, auto& searchResult,fishnet::util::BiPredicate<typename G::node_type> auto const& predicate){
67 auto nodes = g.getNodes();
68 for(auto const& n : nodes){
69 if (searchResult.stop()){
70 return;
71 }
72 if(searchResult.isUnknown(n)){
73 bfs(g, searchResult,n,predicate);
74 }
75 }
76}
77
78template<Graph G>
79static void bfs_all(const Graph auto & g, auto& searchResult){
80 bfs_all<G>(g,searchResult,fishnet::util::TrueBiPredicate());
81}
82}
83
85
94template<Graph G>
95auto connectedComponents(const G & graph, fishnet::util::BiPredicate<typename G::node_type> auto const& inRelation) {
96 using H = G::adj_container_type::hash_function;
97 using E = G::adj_container_type::equality_predicate;
99 __impl::bfs_all<G>(graph,connectedComponents,inRelation);
100 return connectedComponents;
101}
102
110template<Graph G>
111auto connectedComponents(const G & graph) {
113}
114
124template<Graph G>
125auto connectedComponents(const G & graph,std::shared_ptr<fishnet::util::BlockingQueue<std::pair<int,std::vector<typename G::node_type>>>> queue, fishnet::util::BiPredicate<typename G::node_type> auto const& inRelation) {
126 using H = G::adj_container_type::hash_function;
127 using E = G::adj_container_type::equality_predicate;
128 auto concurrentConnectedComponents = ConcurrentConnectedComponents<typename G::node_type,H,E>(queue);
129 __impl::bfs_all<G>(graph,concurrentConnectedComponents,inRelation);
130 return concurrentConnectedComponents;
131}
132
141template<Graph G>
142auto connectedComponents(const G & graph, std::shared_ptr<fishnet::util::BlockingQueue<std::pair<int,std::vector<typename G::node_type>>>> queue) {
144}
145
155template<Graph G>
156auto findPath(const G & graph, const typename G::node_type & start, const typename G::node_type & goal) {
157 using H = G::adj_container_type::hash_function;
158 using E = G::adj_container_type::equality_predicate;
159 auto p = SearchPath<typename G::edge_type,H,E>(start,goal);
160 __impl::bfs(graph,p,start);
161 return p;
162}
163
173template<Graph G>
174util::input_range_of<typename G::edge_type> auto getPath(const G & graph, const typename G::node_type & start, const typename G::node_type & goal) {
175 using N = G::node_type;
176 std::optional<std::vector<N>> nodesOpt = findPath(graph,start,goal).get();
177 if (not nodesOpt){
178 return std::vector<typename G::edge_type>();
179 }
180 std::vector<N> nodes = *nodesOpt;
181 std::vector<typename G::edge_type> edges;
182 for(size_t i = 0; i < nodes.size()-1; i++) {
183 edges.push_back(graph.makeEdge(nodes[i],nodes[i+1]));
184 }
185 return edges;
186}
187
188template<Graph G, Graph O> requires std::same_as<typename G::node_type, typename O::node_type>
189G neighborhood(const G & graph, const typename G::node_type & node, size_t order, O && outputGraph){
190 outputGraph.addNode(node); // adding start node to output graph, so that it is included in neighborhood of order 0
191 auto neighborhood = Neighborhood(std::forward<O>(outputGraph),node,order);
192 __impl::bfs(graph,neighborhood,node);
193 return std::move(neighborhood).get();
194}
195
196template<Graph G> requires std::is_default_constructible_v<G>
197G neighborhood(const G & graph, const typename G::node_type & node, size_t order){
198 return neighborhood(graph,node,order,G());
199}
200
201template<Graph G> requires (!std::is_default_constructible_v<G> && std::is_copy_constructible_v<G>)
202G neighborhood(const G & graph, const typename G::node_type & node, size_t order){
203 auto copy = graph;
204 copy.clear();
205 return neighborhood(graph,node,order,copy);
206}
207
216template<Graph G, typename P, typename R = std::invoke_result_t<std::decay_t<P>>> requires std::same_as<typename G::node_type, typename R::node_type>
217fishnet::util::forward_range_of<R> auto subgraphs(const G & graph, P && emptyGraphProducer){
218 auto subgraphs = Subgraphs<R>(std::move(emptyGraphProducer));
219 __impl::bfs_all<G>(graph,subgraphs);
220 return subgraphs.get();
221}
222
230template<Graph G> requires std::is_default_constructible_v<G>
232 auto subgraphs = Subgraphs<G>();
233 __impl::bfs_all<G>(graph,subgraphs);
234 return subgraphs.get();
235}
236
237
238
239} // namespace fishnet::graph::BFS
Concurrent specialization for computing connected components.
Definition ConnectedComponents.hpp:159
Search Result for Connected Components.
Definition ConnectedComponents.hpp:73
Definition Neighborhood.hpp:8
Search Path implementation.
Definition SearchPath.hpp:16
Definition ConnectedComponents.hpp:175
Blocking queue implementation. Blocks when no elements are present and take() is called until further...
Definition BlockingQueue.hpp:14
Definition FunctionalConcepts.hpp:42
Definition CollectionConcepts.hpp:27
Definition CollectionConcepts.hpp:33
Definition BFSAlgorithm.hpp:84
auto connectedComponents(const G &graph, fishnet::util::BiPredicate< typename G::node_type > auto const &inRelation)
Compute connected components of the graph.
Definition BFSAlgorithm.hpp:95
G neighborhood(const G &graph, const typename G::node_type &node, size_t order, O &&outputGraph)
Definition BFSAlgorithm.hpp:189
auto findPath(const G &graph, const typename G::node_type &start, const typename G::node_type &goal)
Find breadth-first search between between start and goal node.
Definition BFSAlgorithm.hpp:156
util::input_range_of< typename G::edge_type > auto getPath(const G &graph, const typename G::node_type &start, const typename G::node_type &goal)
Find breadth-first search between between start and goal node.
Definition BFSAlgorithm.hpp:174
fishnet::util::forward_range_of< R > auto subgraphs(const G &graph, P &&emptyGraphProducer)
Find all subgraphs in the given graph.
Definition BFSAlgorithm.hpp:217
Definition FunctionalConcepts.hpp:83