ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StaticGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File StaticGraph.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
80/*--------------------------------------------------------------------------*/
81/*----------------------------- DEFINITIONS --------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84#ifndef __ADS_StaticGraph
85 #define __ADS_StaticGraph
86/* self-identification: #endif constant */
87
88/*--------------------------------------------------------------------------*/
89/*------------------------------ INCLUDES ----------------------------------*/
90/*--------------------------------------------------------------------------*/
91
92#include "ADS/Contract.h"
93#include "ADS/Portable.h"
95#include "ADS/graph/Graph.h"
96
97#include <algorithm>
98#include <compare>
99#include <concepts>
100#include <cstddef>
101#include <cstdint>
102#include <iterator>
103#include <optional>
104#include <ranges>
105#include <span>
106#include <string_view>
107#include <tuple>
108#include <type_traits>
109#include <utility>
110#include <variant>
111#include <vector>
112
113/*--------------------------------------------------------------------------*/
114/*--------------------------- NAMESPACE ------------------------------------*/
115/*--------------------------------------------------------------------------*/
116
117namespace ADS
118{
119
120/*--------------------------------------------------------------------------*/
121/*---------------------------- CLASS StaticGraph ---------------------------*/
122/*--------------------------------------------------------------------------*/
123
125template< class W >
127{
128 int to;
129 W w;
130 };
131
137
138namespace detail
139{
140
143template< class W , class T , T WeightedArc< W >::* M >
144class ArcMember
145{
146 public:
147 ArcMember( void ) = default;
148
150 explicit ArcMember( const WeightedArc< W > * a ) : f_a( a ) { }
151
153 const T & operator[]( std::ptrdiff_t k ) const { return( f_a[ k ].*M ); }
155 const T & operator*( void ) const { return( ( *f_a ).*M ); }
157 friend ArcMember operator+( ArcMember p , std::ptrdiff_t k ) {
158 p.f_a += k;
159 return( p );
160 }
161 friend bool operator==( const ArcMember & , const ArcMember & ) = default;
162
163 private:
164 const WeightedArc< W > * f_a = nullptr;
165 }; // end( class ArcMember )
166
171template< class W >
172class WeightedArcCursor
173{
174 public:
175 using iterator_concept = std::random_access_iterator_tag;
176 using iterator_category = std::input_iterator_tag;
177 using value_type = WeightedArc< W >;
178 using difference_type = std::ptrdiff_t;
179 using reference = WeightedArc< W >;
180
182 struct Pointer
183 {
184 WeightedArc< W > a;
185 const WeightedArc< W > * operator->( void ) const { return( &a ); }
186 };
187 using pointer = Pointer;
188
189 WeightedArcCursor( void ) = default;
190
192 WeightedArcCursor( const int * h , const W * w ) : f_h( h ) , f_w( w ) { }
193
195 reference operator*( void ) const { return( reference{ *f_h , *f_w } ); }
197 pointer operator->( void ) const { return( Pointer{ **this } ); }
199 reference operator[]( difference_type k ) const {
200 return( reference{ f_h[ k ] , f_w[ k ] } );
201 }
202
203 WeightedArcCursor & operator++( void ) {
204 ++f_h;
205 ++f_w;
206 return( *this );
207 }
208 WeightedArcCursor operator++( int ) {
209 WeightedArcCursor c = *this;
210 ++*this;
211 return( c );
212 }
213 WeightedArcCursor & operator--( void ) {
214 --f_h;
215 --f_w;
216 return( *this );
217 }
218 WeightedArcCursor operator--( int ) {
219 WeightedArcCursor c = *this;
220 --*this;
221 return( c );
222 }
223 WeightedArcCursor & operator+=( difference_type k ) {
224 f_h += k;
225 f_w += k;
226 return( *this );
227 }
228 WeightedArcCursor & operator-=( difference_type k ) {
229 f_h -= k;
230 f_w -= k;
231 return( *this );
232 }
233 friend WeightedArcCursor operator+( WeightedArcCursor c ,
234 difference_type k ) {
235 return( c += k );
236 }
237 friend WeightedArcCursor operator+( difference_type k ,
238 WeightedArcCursor c ) {
239 return( c += k );
240 }
241 friend WeightedArcCursor operator-( WeightedArcCursor c ,
242 difference_type k ) {
243 return( c -= k );
244 }
245 friend difference_type operator-( const WeightedArcCursor & a ,
246 const WeightedArcCursor & b ) {
247 return( a.f_h - b.f_h );
248 }
249 friend bool operator==( const WeightedArcCursor & a ,
250 const WeightedArcCursor & b ) {
251 return( a.f_h == b.f_h );
252 }
253 friend auto operator<=>( const WeightedArcCursor & a ,
254 const WeightedArcCursor & b ) {
255 return( a.f_h <=> b.f_h );
256 }
257
259 [[nodiscard]] const int * heads( void ) const { return( f_h ); }
261 [[nodiscard]] const W * weights( void ) const { return( f_w ); }
262
263 private:
264 const int * f_h = nullptr;
265 const W * f_w = nullptr;
266 }; // end( class WeightedArcCursor )
267
268} // end( namespace detail )
269
273template< class W = std::monostate , ArcLayout L = ArcLayout::Separate >
275{
276
277/*--------------------------------------------------------------------------*/
278 public:
279 using Weight = W;
280
282 static constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
283
285 static constexpr ArcLayout layout = L;
286
288 static constexpr bool interleaved =
290
292 using Arc = std::conditional_t< weighted , WeightedArc< W > , int >;
293
296 using ArcCursor = std::conditional_t<
297 ! weighted , const int * ,
298 std::conditional_t< interleaved , const WeightedArc< W > * ,
299 detail::WeightedArcCursor< W > > >;
300
302 using ArcRange = std::conditional_t<
303 ! weighted , std::span< const int > ,
304 std::conditional_t< interleaved , std::span< const WeightedArc< W > > ,
305 std::ranges::subrange< ArcCursor > > >;
306
309 using HeadArray =
310 std::conditional_t< interleaved ,
311 detail::ArcMember< W , int , &WeightedArc< W >::to > ,
312 const int * >;
313
315 using WeightArray = std::conditional_t<
316 interleaved , detail::ArcMember< W , W , &WeightedArc< W >::w > ,
317 const W * >;
318
320 StaticGraph( void ) : f_off( 1 , 0 ) { }
321
351 StaticGraph( int n , const std::vector< std::tuple< int , int , W > > & E ,
352 bool directed = true );
353
358 StaticGraph( int n , const std::vector< std::pair< int , int > > & E ,
359 bool directed = true )
360 requires( ! weighted );
361
364 template< Graph G >
365 explicit StaticGraph( const G & g );
366
368 [[nodiscard]] bool directed( void ) const { return( f_directed ); }
369
371 [[nodiscard]] int n( void ) const { return( int( f_off.size() ) - 1 ); }
372
374 [[nodiscard]] int m( void ) const { return( f_m ); }
375
377 [[nodiscard]] int arcs( void ) const {
378 return( int( interleaved ? f_arc.size() : f_head.size() ) );
379 }
380
381 [[nodiscard]] int bound( void ) const { return( n() ); }
382
383 [[nodiscard]] bool hasNode( int u ) const {
384 return( ( u >= 0 ) && ( u < n() ) );
385 }
386
388 [[nodiscard]] int degree( int u ) const;
389
390 [[nodiscard]] std::vector< int > V( void ) const;
391
393 [[nodiscard]] std::vector< int > adj( int u ) const;
394
396 template< class F >
397 void forEachAdj( int u , F && f ) const;
398
401 template< class F >
402 void forEachEdge( int u , F && f ) const
403 requires( weighted );
404
408 [[nodiscard]] std::optional< W > weight( int u , int v ) const
409 requires( weighted );
410
416 bool setWeight( int u , int v , const W & w )
417 requires( weighted );
418
423 [[nodiscard]] ArcRange out( int u ) const;
424
428 [[nodiscard]] const int * offsets( void ) const { return( f_off.data() ); }
429
432 [[nodiscard]] const int * heads( void ) const
433 requires( ! interleaved )
434 {
435 return( f_head.data() );
436 }
437
440 [[nodiscard]] const W * weightData( void ) const
441 requires( weighted && ( ! interleaved ) )
442 {
443 return( f_w.data() );
444 }
445
448 [[nodiscard]] HeadArray arcHeads( void ) const {
449 if constexpr( interleaved )
450 return( HeadArray( f_arc.data() ) );
451 else
452 return( f_head.data() );
453 }
454
456 [[nodiscard]] WeightArray arcWeights( void ) const
457 requires( weighted )
458 {
459 if constexpr( interleaved )
460 return( WeightArray( f_arc.data() ) );
461 else
462 return( f_w.data() );
463 }
464
468 [[nodiscard]] ArcCursor arcData( void ) const {
469 if constexpr( interleaved )
470 return( f_arc.data() );
471 else if constexpr( weighted )
472 return( ArcCursor( f_head.data() , f_w.data() ) );
473 else
474 return( f_head.data() );
475 }
476
478 static int head( const Arc & a ) {
479 if constexpr( weighted )
480 return( a.to );
481 else
482 return( a );
483 }
484
495 [[nodiscard]] StaticGraph transpose( void ) const;
496
508 [[nodiscard]] StaticGraph relabel( const std::vector< int > & p ) const;
509
510/*------------------------- THE STANDARD INTERFACE -------------------------*/
511
513 [[nodiscard]] auto nodes( void ) const {
514 return( std::views::iota( 0 , n() ) );
515 }
516
517/*--------------------------------------------------------------------------*/
518/*----------------------------- THE PERSISTENCE ----------------------------*/
519
521 static constexpr std::string_view persistName = "StaticGraph";
522
524 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
525 if constexpr( weighted )
526 return( detail::persistMix(
527 { sizeof( W ) , std::is_floating_point_v< W > ,
528 std::is_signed_v< W > } ) );
529 else
530 return( 0 );
531 }
532
535 template< class A >
536 void persist( A & a );
537
538 private:
542 template< class Edges , class Get >
543 void build( int n , const Edges & E , Get && get );
544
547 [[nodiscard]] int find( int u , int v ) const;
548
550 [[nodiscard]] W & weightAt( int k )
551 requires( weighted )
552 {
553 if constexpr( interleaved )
554 return( f_arc[ std::size_t( k ) ].w );
555 else
556 return( f_w[ std::size_t( k ) ] );
557 }
558
561 [[nodiscard]] bool consistent( void ) const;
562
563 bool f_directed = true;
565 std::vector< int , MappableAllocator< int > > f_off;
567 std::vector< int , MappableAllocator< int > > f_head;
569 std::vector< W , MappableAllocator< W > > f_w;
571 std::vector< WeightedArc< W > > f_arc;
572 int f_m = 0;
573
574 }; // end( class StaticGraph )
575
576/*--------------------------------------------------------------------------*/
577/*------------------------------- CONCEPTS ---------------------------------*/
578/*--------------------------------------------------------------------------*/
579
584template< class G >
585concept CsrArrays = requires( const G & g )
586{
587 { g.offsets() } -> std::same_as< const int * >;
588 { g.heads() } -> std::same_as< const int * >;
589 } && ( ( ! requires { requires G::weighted; } ) || requires( const G & g )
590{
591 {
592 g.weightData()
593 } -> std::same_as< const typename G::Weight * >;
594 } );
595
596/*--------------------------------------------------------------------------*/
597/*------------------------------- FUNCTIONS --------------------------------*/
598/*--------------------------------------------------------------------------*/
599
622template< class W , ArcLayout L >
623std::vector< int > bfsOrder( const StaticGraph< W , L > & g , int r = 0 );
624
625/*--------------------------------------------------------------------------*/
626
627} // end( namespace ADS )
628
629/*--------------------------------------------------------------------------*/
630/*-------------------------- IMPLEMENTATION --------------------------------*/
631/*--------------------------------------------------------------------------*/
632
633#include "ADS/graph/impl/StaticGraph.tpp"
634
635/*--------------------------------------------------------------------------*/
636
637#endif /* StaticGraph.h included */
638
639/*--------------------------------------------------------------------------*/
640/*------------------------- End File StaticGraph.h -------------------------*/
641/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their...
Definition StaticGraph.h:275
static int head(const Arc &a)
the head of an arc
Definition StaticGraph.h:478
void forEachAdj(int u, F &&f) const
bool setWeight(int u, int v, const W &w)
the weight of the first arc set to , and that of the first arc if the graph is unoriented,...
static constexpr ArcLayout layout
the layout of the arcs
Definition StaticGraph.h:285
static constexpr std::uint64_t persistTag(void)
the type of the weights, in a file
Definition StaticGraph.h:524
static constexpr bool interleaved
whether the arcs are the pairs of one array (never if unweighted)
Definition StaticGraph.h:288
static constexpr bool weighted
whether the arcs have a weight
Definition StaticGraph.h:282
StaticGraph(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
StaticGraph relabel(const std::vector< int > &p) const
the same graph with the node renamed
int arcs(void) const
the number of arcs, 2 m() if the graph is unoriented
Definition StaticGraph.h:377
StaticGraph transpose(void) const
the graph with each arc reversed (the same graph if unoriented)
bool hasNode(int u) const
Definition StaticGraph.h:383
std::conditional_t< ! weighted, std::span< const int >, std::conditional_t< interleaved, std::span< const WeightedArc< W > >, std::ranges::subrange< ArcCursor > > > ArcRange
the arcs of a row, random access
Definition StaticGraph.h:302
std::conditional_t< interleaved, detail::ArcMember< W, int, &WeightedArc< W >::to >, const int * > HeadArray
the heads of the arcs read in place by their places: a pointer, or the heads of the pairs if interlea...
Definition StaticGraph.h:309
int degree(int u) const
int m(void) const
the number of edges, an unoriented one counted once
Definition StaticGraph.h:374
auto nodes(void) const
the nodes, , random access
Definition StaticGraph.h:513
std::conditional_t< ! weighted, const int *, std::conditional_t< interleaved, const WeightedArc< W > *, detail::WeightedArcCursor< W > > > ArcCursor
the cursor of the arcs: a pointer to the heads if the graph is unweighted, to the pairs if interleave...
Definition StaticGraph.h:296
std::conditional_t< interleaved, detail::ArcMember< W, W, &WeightedArc< W >::w >, const W * > WeightArray
the weights of the arcs read in place by their places
Definition StaticGraph.h:315
std::vector< int > V(void) const
int bound(void) const
Definition StaticGraph.h:381
StaticGraph(const G &g)
the copy of any graph, the names that are not nodes of becoming isolated nodes
WeightArray arcWeights(void) const
the weights of all the arcs read in place, in either layout
Definition StaticGraph.h:456
StaticGraph(int n, const std::vector< std::pair< int, int > > &E, bool directed=true)
the unweighted graph of the nodes and of the edges
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
StaticGraph(void)
the graph with no node
Definition StaticGraph.h:320
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition StaticGraph.h:521
int n(void) const
the number of nodes
Definition StaticGraph.h:371
W Weight
the type of the weights
Definition StaticGraph.h:279
ArcRange out(int u) const
the arcs out of , contiguous, random access: on a weighted graph the WeightedArc values made from hea...
std::vector< int > adj(int u) const
std::conditional_t< weighted, WeightedArc< W >, int > Arc
an arc: its head, and its weight if the graph is weighted
Definition StaticGraph.h:292
ArcCursor arcData(void) const
all the arcs, row by row: the cursor at the first, whose place is the arc (the pointer heads() if t...
Definition StaticGraph.h:468
const W * weightData(void) const
the weights of all the arcs, in the places of heads(), an array of the separate layout
Definition StaticGraph.h:440
const int * offsets(void) const
the offsets of the rows: the arcs of u are those in the places [ offsets()[ u ] , offsets()[ u + 1 ] ...
Definition StaticGraph.h:428
bool directed(void) const
whether the graph is oriented
Definition StaticGraph.h:368
void forEachEdge(int u, F &&f) const
on each arc of weight
const int * heads(void) const
the heads of all the arcs, row by row, arcs() of them, an array of the separate layout
Definition StaticGraph.h:432
std::optional< W > weight(int u, int v) const
the weight of the first arc , nothing if there is none, by a binary search of the row of ,...
HeadArray arcHeads(void) const
the heads of all the arcs read in place, in either layout: heads(), or the heads of the pairs
Definition StaticGraph.h:448
a graph whose compressed rows are plain arrays, offsets(), heads() and, if weighted,...
Definition StaticGraph.h:585
The GRAPH of chapter 9 and its realizations.
ArcLayout
the layout of the arcs of a weighted StaticGraph
Definition StaticGraph.h:133
std::vector< int > bfsOrder(const StaticGraph< W, L > &g, int r=0)
the order of a visit in breadth from , then from the least node not reached, and so on: the name p[ u...
@ Interleaved
the pairs WeightedArc in one array
Definition StaticGraph.h:135
@ Separate
the heads and the weights in two arrays of the same places
Definition StaticGraph.h:134
Definition ADT.h:96
an arc of a weighted StaticGraph, its head with its weight
Definition StaticGraph.h:127
W w
the weight
Definition StaticGraph.h:129
int to
the head
Definition StaticGraph.h:128