Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
DFSAlgorithm.hpp
Go to the documentation of this file.
1#pragma once
2#include <stack>
4#include <fishnet/Graph.hpp>
5
6#include "SearchResult.hpp"
7#include "SearchPath.hpp"
8
9namespace fishnet::graph::__impl {
20template<Graph G,class SearchResultImpl>
21static void dfs(const G & g,SearchResult<SearchResultImpl> & searchResult, const typename G::node_type & start, fishnet::util::BiPredicate<typename G::node_type> auto const& predicate) {
22 using N = G::node_type;
23 std::stack<N> nodes;
24 nodes.push(start);
25 searchResult.open(start);
26 while(not nodes.empty() and not searchResult.stop()){
27 N current = nodes.top();
28 nodes.pop();
29 if(searchResult.isOpen(current)) {
30 for(auto & neighbour: g.getNeighbours(current)){
31 if(searchResult.isUnknown(neighbour) and predicate(current,neighbour)) {
32 nodes.push(neighbour);
33 searchResult.open(neighbour);
34 searchResult.onEdge(current,neighbour);
35 }
36 }
37 searchResult.close(current);
38 }
39
40 }
41}
42}
43
53template<Graph G>
54static std::vector<typename G::edge_type> getPath(const G & g,const typename G::node_type & start, const typename G::node_type & goal) {
55 using N = typename G::node_type;
56 using E = typename G::edge_type;
57 using H = G::adj_container_type::hash_function;
58 using Eq = G::adj_container_type::equality_predicate;
59 auto searchPath = SearchPath<E,H,Eq>(start,goal);
60 fishnet::graph::__impl::dfs(g,searchPath,start,fishnet::util::TrueBiPredicate());
61 auto pathOpt = searchPath.get();
62 if (not pathOpt){
63 return std::vector<E>();
64 }
65 std::vector<N> & nodes = *pathOpt;
66 std::vector<E> edges;
67 for (size_t i = 0; i < nodes.size()-1; i++){
68 edges.emplace_back(g.makeEdge(nodes[i],nodes[i+1]));
69 }
70 return edges;
71}
72}
73
74
Search Path implementation.
Definition SearchPath.hpp:16
Definition FunctionalConcepts.hpp:42
Definition DFSAlgorithm.hpp:44
static std::vector< typename G::edge_type > getPath(const G &g, const typename G::node_type &start, const typename G::node_type &goal)
Find (any, not necessarily the shortest ) path between start and goal using depth-first search.
Definition DFSAlgorithm.hpp:54
Definition FunctionalConcepts.hpp:83