ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Graph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Graph.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
49/*--------------------------------------------------------------------------*/
50/*----------------------------- DEFINITIONS --------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#ifndef __ADS_Graph
54 #define __ADS_Graph
55/* self-identification: #endif constant */
56
57/*--------------------------------------------------------------------------*/
58/*------------------------------ INCLUDES ----------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#include "ADS/Contract.h"
62#include "ADS/Iterators.h"
63#include "ADS/sequence/List.h"
64
65#include <concepts>
66#include <cstddef>
67#include <optional>
68#include <ranges>
69#include <string>
70#include <tuple>
71#include <type_traits>
72#include <utility>
73#include <variant>
74#include <vector>
75
76/*--------------------------------------------------------------------------*/
77/*--------------------------- NAMESPACE ------------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80namespace ADS
81{
82
83/*--------------------------------------------------------------------------*/
84/*-------------------------------- CONCEPTS --------------------------------*/
85/*--------------------------------------------------------------------------*/
86
89template< class G >
90concept Graph = requires( const G g , int u )
91{
92 { g.n() } -> std::convertible_to< int >;
93 { g.bound() } -> std::convertible_to< int >;
94 { g.directed() } -> std::same_as< bool >;
95 { g.hasNode( u ) } -> std::same_as< bool >;
96 { g.V() } -> std::same_as< std::vector< int > >;
97 { g.adj( u ) } -> std::same_as< std::vector< int > >;
98 g.forEachAdj( u , []( int ) { } );
99 };
100
102template< class G >
103concept DynamicGraph = Graph< G > && requires( G g , int u , int v )
104{
105 { g.insertNode() } -> std::same_as< int >;
106 g.insertEdge( u , v );
107 g.deleteNode( u );
108 g.deleteEdge( u , v );
109 };
110
111/*--------------------------------------------------------------------------*/
112/*---------------------------- CLASS GraphNodes ----------------------------*/
113/*--------------------------------------------------------------------------*/
114
116template< class T >
118{
119
120/*--------------------------------------------------------------------------*/
121 public:
123 [[nodiscard]] bool directed( void ) const { return( f_directed ); }
124
126 [[nodiscard]] int n( void ) const { return( f_n ); }
127
129 [[nodiscard]] int m( void ) const { return( f_m ); }
130
132 [[nodiscard]] int bound( void ) const { return( int( f_alive.size() ) ); }
133
135 [[nodiscard]] bool hasNode( int u ) const;
136
138 [[nodiscard]] std::vector< int > V( void ) const;
139
142 [[nodiscard]] const T & label( int u ) const;
143
146 void setLabel( int u , const T & t );
147
148/*------------------------- THE STANDARD INTERFACE -------------------------*/
149
150 // the nodes and (in the derived graphs) the adjacent nodes as ranges,
151 // which visit them where they are, instead of the vectors that V() and
152 // adj() build
153
155 struct Alive
156 {
158 static std::size_t size( const auto & o ) { return( o.f_alive.size() ); }
160 static bool occupied( const auto & o , std::size_t i ) {
161 return( o.f_alive[ i ] );
162 }
163
164 static int read( const auto & , std::size_t i ) { return( int( i ) ); }
165 };
166
168 [[nodiscard]] auto nodes( void ) const {
170 return(
171 std::ranges::subrange( It( this , 0 ) , It( this , f_alive.size() ) ) );
172 }
173
174/*--------------------------------------------------------------------------*/
175 protected:
177 GraphNodes( int n , bool directed );
178
180 int addNode( const T & t );
181
183 int check( int u ) const;
184
186 void edge( int u , int v ) const;
187
189 [[nodiscard]] bool nodesCounted( void ) const;
190
192 std::vector< bool > f_alive;
193 std::vector< T > f_label;
194 int f_n = 0;
195 int f_m = 0;
196
197 }; // end( class GraphNodes )
198
199/*--------------------------------------------------------------------------*/
200/*------------------------------- FUNCTIONS --------------------------------*/
201/*--------------------------------------------------------------------------*/
202
220template< Graph G >
221std::vector< std::vector< int > > incidence( const G & g );
222
239inline std::vector< int >
240adjFromIncidence( const std::vector< std::vector< int > > & B , int u ,
241 bool directed );
242
243/*--------------------------------------------------------------------------*/
244/*------------------------------ CLASS Rows --------------------------------*/
245/*--------------------------------------------------------------------------*/
246
247namespace detail
248{
249
256template< Graph G >
257class Rows
258{
259
261 static constexpr bool byArcHeads = requires( const G & h ) {
262 { h.offsets() } -> std::convertible_to< const int * >;
263 h.arcHeads()[ 0 ];
264 };
265
267 template< bool A , class = void >
268 struct Heads
269 {
270 using type = const int *;
271 };
272 template< class V >
273 struct Heads< true , V >
274 {
275 using type = decltype( std::declval< const G & >().arcHeads() );
276 };
277
278 public:
280 static constexpr bool direct = byArcHeads || requires( const G & h ) {
281 { h.offsets() } -> std::convertible_to< const int * >;
282 { h.heads() } -> std::convertible_to< const int * >;
283 h.arcData();
284 };
285
287 explicit Rows( const G & g ) {
288 if constexpr( byArcHeads ) {
289 f_o = g.offsets();
290 f_h = g.arcHeads();
291 }
292 else if constexpr( direct ) {
293 f_o = g.offsets();
294 f_h = g.heads();
295 }
296 else {
297 const int n = g.bound();
298 f_off.assign( std::size_t( n ) + 1 , 0 );
299 for( int u = 0 ; u < n ; ++u ) {
300 f_off[ u ] = f_head.size();
301 if( g.hasNode( u ) )
302 g.forEachAdj( u , [ & ]( int v ) { f_head.push_back( v ); } );
303 }
304 f_off[ n ] = f_head.size();
305 }
306 }
307
309 [[nodiscard]] std::size_t first( int u ) const {
310 if constexpr( direct )
311 return( std::size_t( f_o[ u ] ) );
312 else
313 return( f_off[ u ] );
314 }
315
317 [[nodiscard]] std::size_t last( int u ) const { return( first( u + 1 ) ); }
318
320 [[nodiscard]] int head( std::size_t i ) const {
321 if constexpr( ! direct )
322 return( f_head[ i ] );
323 else
324 return( f_h[ i ] );
325 }
326
327 private:
328 const int * f_o = nullptr;
330 typename Heads< byArcHeads >::type f_h{};
331 std::vector< std::size_t > f_off;
332 std::vector< int > f_head;
333
334 }; // end( class Rows )
335
336} // end( namespace detail )
337
338/*--------------------------------------------------------------------------*/
339
340} // end( namespace ADS )
341
342/*--------------------------------------------------------------------------*/
343/*-------------------------- IMPLEMENTATION --------------------------------*/
344/*--------------------------------------------------------------------------*/
345
346#include "ADS/graph/impl/Graph.tpp"
347
348/*--------------------------------------------------------------------------*/
349
350#endif /* Graph.h included */
351
352/*--------------------------------------------------------------------------*/
353/*---------------------------- End File Graph.h ----------------------------*/
354/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
GraphNodes(int n, bool directed)
bool nodesCounted(void) const
n() counts the names in use, and each has a label
void edge(int u, int v) const
and must be two distinct nodes
int f_n
the number of nodes
Definition Graph.h:194
bool f_directed
oriented or not
Definition Graph.h:191
void setLabel(int u, const T &t)
the label of the node becomes
std::vector< bool > f_alive
whether a name is a node
Definition Graph.h:192
std::vector< int > V(void) const
the set of the nodes, in increasing order
const T & label(int u) const
the label of the node
std::vector< T > f_label
the labels
Definition Graph.h:193
int bound(void) const
the names of the nodes are in
Definition Graph.h:132
int f_m
the number of edges
Definition Graph.h:195
int check(int u) const
, which must be a node
int m(void) const
the number of edges, an unoriented one counted once
Definition Graph.h:129
int n(void) const
the number of nodes, the G.n of the book
Definition Graph.h:126
bool hasNode(int u) const
whether is a node
auto nodes(void) const
the nodes in increasing order, bidirectional
Definition Graph.h:168
int addNode(const T &t)
a new node of label , and its name
bool directed(void) const
whether the graph is oriented
Definition Graph.h:123
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
a GRAPH with the insertions and the deletions of its specification
Definition Graph.h:103
GRAPH (section 9.2), as far as the visits need it: the nodes, the adjacent nodes of a node,...
Definition Graph.h:90
std::vector< std::vector< int > > incidence(const G &g)
the matrix of incidence nodes-edges, the edges in the order of V() and of adj(), an unoriented one wh...
std::vector< int > adjFromIncidence(const std::vector< std::vector< int > > &B, int u, bool directed)
the adjacent nodes of from the matrix of incidence of a graph, oriented or not, in the order of the...
Definition ADT.h:96
the names in use, for SlotIterator
Definition Graph.h:156
static int read(const auto &, std::size_t i)
the node of the name
Definition Graph.h:164
static bool occupied(const auto &o, std::size_t i)
whether the name is in use
Definition Graph.h:160
static std::size_t size(const auto &o)
the number of the names
Definition Graph.h:158