Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
DirectedAcyclicGraph.hpp
Go to the documentation of this file.
1#pragma once
5
6namespace fishnet::graph{
7
8template<class G> requires (G::edge_type::isDirected()==true)
10private:
12 using N = typename G::node_type;
13 using E = typename G::edge_type;
14
22 bool hasCycleAfterAddingEdge(const N & from, const N & to) noexcept{
23 return BFS::findPath(this->g,to,from).get().has_value();
24 }
25
26public:
27 DirectedAcyclicGraph(G && underlyingGraph):Base(std::move(underlyingGraph)){}
28
30
32
34 this->g = other.g;
35 return *this;
36 }
37
39 this->g = std::move(other.g);
40 return *this;
41 }
42
44
45 bool addEdge(const N & from, const N & to) noexcept{
46 if(hasCycleAfterAddingEdge(from,to))
47 return false;
48 this->g.addEdge(from,to);
49 return true;
50 }
51
52 bool addEdge(N && from, N && to) noexcept{
53 if(hasCycleAfterAddingEdge(from,to))
54 return false;
55 this->g.addEdge(std::move(from),std::move(to));
56 return true;
57 }
58
59 bool addEdge(const E & edge) noexcept{
60 return addEdge(edge.getFrom(),edge.getTo());
61 }
62
63 void addEdges(util::forward_range_of<std::pair<N,N>> auto && edges) noexcept{
64 std::ranges::for_each(edges,[this](auto && pair){
65 auto && [from,to] = pair;
66 this->addEdge(from,to);
67 });
68 }
69
70 void addEdges(util::forward_range_of<E> auto && edges) noexcept{
71 addEdges(std::views::transform(edges,[](auto && edge){return std::make_pair(edge.getFrom(),edge.getTo());}));
72 }
73
74 size_t inDegree(const N & node)const noexcept{
75 return fishnet::util::size(this->g.getReachableFrom(node));
76 }
77
78 size_t outDegree(const N & node)const noexcept{
79 return fishnet::util::size(this->g.getNeighbours(node));
80 }
81
82 util::forward_range_of<N> auto rootNodes() const noexcept {
83 return this->g.getNodes() | std::views::filter([this](const auto & node){return inDegree(node)==0;});
84 }
85};
86}
Definition DirectedAcyclicGraph.hpp:9
bool addEdge(N &&from, N &&to) noexcept
Definition DirectedAcyclicGraph.hpp:52
void addEdges(util::forward_range_of< E > auto &&edges) noexcept
Definition DirectedAcyclicGraph.hpp:70
DirectedAcyclicGraph & operator=(DirectedAcyclicGraph &&other)
Definition DirectedAcyclicGraph.hpp:38
DirectedAcyclicGraph(G &&underlyingGraph)
Definition DirectedAcyclicGraph.hpp:27
void addEdges(util::forward_range_of< std::pair< N, N > > auto &&edges) noexcept
Definition DirectedAcyclicGraph.hpp:63
size_t outDegree(const N &node) const noexcept
Definition DirectedAcyclicGraph.hpp:78
DirectedAcyclicGraph(const DirectedAcyclicGraph &other)
Definition DirectedAcyclicGraph.hpp:31
size_t inDegree(const N &node) const noexcept
Definition DirectedAcyclicGraph.hpp:74
DirectedAcyclicGraph & operator=(const DirectedAcyclicGraph &other)
Definition DirectedAcyclicGraph.hpp:33
util::forward_range_of< N > auto rootNodes() const noexcept
Definition DirectedAcyclicGraph.hpp:82
bool addEdge(const N &from, const N &to) noexcept
Definition DirectedAcyclicGraph.hpp:45
DirectedAcyclicGraph()
Definition DirectedAcyclicGraph.hpp:43
bool addEdge(const E &edge) noexcept
Definition DirectedAcyclicGraph.hpp:59
DirectedAcyclicGraph(DirectedAcyclicGraph &&other)
Definition DirectedAcyclicGraph.hpp:29
Abstract Graph Decorator using CRTP.
Definition GraphDecorator.hpp:14
G g
Definition GraphDecorator.hpp:16
Definition CollectionConcepts.hpp:27
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
Definition AdjacencyContainer.hpp:4
constexpr size_t size(std::ranges::range auto &&range) noexcept
Definition CollectionConcepts.hpp:50
STL namespace.