ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArrayGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ArrayGraph.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
25/*--------------------------------------------------------------------------*/
26/*----------------------------- DEFINITIONS --------------------------------*/
27/*--------------------------------------------------------------------------*/
28
29#ifndef __ADS_ArrayGraph
30 #define __ADS_ArrayGraph
31/* self-identification: #endif constant */
32
33/*--------------------------------------------------------------------------*/
34/*------------------------------ INCLUDES ----------------------------------*/
35/*--------------------------------------------------------------------------*/
36
37#include "ADS/Contract.h"
38#include "ADS/graph/Graph.h"
39
40#include <optional>
41#include <ranges>
42#include <span>
43#include <tuple>
44#include <type_traits>
45#include <variant>
46#include <vector>
47
48/*--------------------------------------------------------------------------*/
49/*--------------------------- NAMESPACE ------------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52namespace ADS
53{
54
55/*--------------------------------------------------------------------------*/
56/*---------------------------- CLASS ArrayGraph ----------------------------*/
57/*--------------------------------------------------------------------------*/
58
60template< class W = std::monostate >
62{
63
64/*--------------------------------------------------------------------------*/
65 public:
66 using Weight = W;
67
69 static constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
70
94 ArrayGraph( int n , const std::vector< std::tuple< int , int , W > > & E ,
95 bool directed = true );
96
100 template< Graph G >
101 explicit ArrayGraph( const G & g );
102
104 [[nodiscard]] bool directed( void ) const { return( f_directed ); }
105
107 [[nodiscard]] int n( void ) const { return( int( f_off.size() ) - 1 ); }
108
110 [[nodiscard]] int m( void ) const { return( f_m ); }
111
114 [[nodiscard]] int arcs( void ) const { return( int( f_to.size() ) ); }
115
117 [[nodiscard]] int bound( void ) const { return( n() ); }
118
120 [[nodiscard]] bool hasNode( int u ) const;
121
123 [[nodiscard]] std::vector< int > V( void ) const;
124
126 [[nodiscard]] std::vector< int > adj( int u ) const;
127
129 template< class F >
130 void forEachAdj( int u , F && f ) const;
131
133 template< class F >
134 void forEachEdge( int u , F && f ) const;
135
138 [[nodiscard]] std::optional< W > weight( int u , int v ) const;
139
144 bool setWeight( int u , int v , const W & w );
145
146/*------------------------- THE STANDARD INTERFACE -------------------------*/
147
149 [[nodiscard]] auto nodes( void ) const {
150 return( std::views::iota( 0 , n() ) );
151 }
152
155 [[nodiscard]] std::span< const int > neighbors( int u ) const;
156
161 [[nodiscard]] const int * offsets( void ) const { return( f_off.data() ); }
162
164 [[nodiscard]] const int * heads( void ) const { return( f_to.data() ); }
165
167 [[nodiscard]] const W * weightData( void ) const { return( f_w.data() ); }
168
170 [[nodiscard]] const int * arcHeads( void ) const { return( heads() ); }
171
174 [[nodiscard]] const W * arcWeights( void ) const
175 requires( weighted )
176 {
177 return( weightData() );
178 }
179
180/*--------------------------------------------------------------------------*/
181 private:
182 template< class G >
183 static std::vector< std::tuple< int , int , W > > edges( const G & g );
184
187 [[nodiscard]] bool consistent( void ) const;
188
189 bool f_directed;
190 std::vector< int > f_off;
191 std::vector< int > f_to;
192 std::vector< W > f_w;
193 int f_m = 0;
194
195 }; // end( class ArrayGraph )
196
197/*--------------------------------------------------------------------------*/
198
199} // end( namespace ADS )
200
201/*--------------------------------------------------------------------------*/
202/*-------------------------- IMPLEMENTATION --------------------------------*/
203/*--------------------------------------------------------------------------*/
204
205#include "ADS/graph/impl/ArrayGraph.tpp"
206
207/*--------------------------------------------------------------------------*/
208
209#endif /* ArrayGraph.h included */
210
211/*--------------------------------------------------------------------------*/
212/*------------------------- End File ArrayGraph.h --------------------------*/
213/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
ArrayGraph(int n, const std::vector< std::tuple< int, int, W > > &E, bool directed=true)
the graph of the nodes and of the given edges; an unoriented edge is given once
std::vector< int > adj(int u) const
const W * arcWeights(void) const
the weights of all the arcs read in place, weightData(), as in StaticGraph
Definition ArrayGraph.h:174
int arcs(void) const
the number of arcs, 2 m() if the graph is unoriented, as in StaticGraph
Definition ArrayGraph.h:114
std::span< const int > neighbors(int u) const
the nodes adjacent to , contiguous in the array of the edges
ArrayGraph(const G &g)
the copy of any graph, with the nodes 0 , ... , g.bound() - 1: the names that are not nodes of g,...
std::vector< int > V(void) const
the set of the nodes, in increasing order
void forEachEdge(int u, F &&f) const
bool setWeight(int u, int v, const W &w)
the weight of set to (of both its arcs if unoriented), in ; false if there is no such edge
const int * heads(void) const
the adjacent nodes, row by row
Definition ArrayGraph.h:164
bool directed(void) const
whether the graph is oriented
Definition ArrayGraph.h:104
int m(void) const
the number of edges, an unoriented one counted once
Definition ArrayGraph.h:110
const int * offsets(void) const
the offsets of the rows, of them: the adjacent nodes of are the places from offsets()[ u ] to offse...
Definition ArrayGraph.h:161
void forEachAdj(int u, F &&f) const
static constexpr bool weighted
whether the arcs have a weight, as in StaticGraph
Definition ArrayGraph.h:69
auto nodes(void) const
the nodes, , random access
Definition ArrayGraph.h:149
std::optional< W > weight(int u, int v) const
the weight of , nothing if there is no such edge, in
const int * arcHeads(void) const
the heads of all the arcs read in place, heads(), as in StaticGraph
Definition ArrayGraph.h:170
const W * weightData(void) const
the weights, in the places of heads()
Definition ArrayGraph.h:167
bool hasNode(int u) const
whether is a node
int bound(void) const
the names of the nodes,
Definition ArrayGraph.h:117
W Weight
the type of the weights
Definition ArrayGraph.h:66
int n(void) const
the number of nodes
Definition ArrayGraph.h:107
The GRAPH of chapter 9 and its realizations.
std::vector< WeightedEdge< typename G::Weight > > edges(const G &g)
the edges of an unoriented graph, each once, from its smaller end
Definition ADT.h:96