ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Graph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Graph.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
51/*--------------------------------------------------------------------------*/
52/*----------------------------- DEFINITIONS --------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55#ifndef __ADS_interop_boost_Graph
56 #define __ADS_interop_boost_Graph
57/* self-identification: #endif constant */
58
59#if defined( ADS_WITH_BOOST )
60
61/*--------------------------------------------------------------------------*/
62/*------------------------------ INCLUDES ----------------------------------*/
63/*--------------------------------------------------------------------------*/
64
66#include "ADS/graph/ListGraph.h"
69
70#include <boost/graph/graph_traits.hpp>
71#include <boost/graph/properties.hpp>
72#include <boost/iterator/counting_iterator.hpp>
73#include <boost/iterator/iterator_facade.hpp>
74#include <boost/property_map/property_map.hpp>
75
76#include <cstddef>
77#include <ranges>
78#include <type_traits>
79#include <utility>
80#include <variant>
81#include <vector>
82
83/*--------------------------------------------------------------------------*/
84/*--------------------------- NAMESPACE ------------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87namespace ADS::bgl
88{
89
90/*--------------------------------------------------------------------------*/
91/*----------------------- THE GRAPHS IN COMPRESSED ROWS --------------------*/
92/*--------------------------------------------------------------------------*/
93
97template< class G >
98struct Rows;
99
102template< class W , ArcLayout L >
103struct Rows< StaticGraph< W , L > >
104{
105 using Weight = W;
106 static constexpr bool weighted = StaticGraph< W , L >::weighted;
108 static const int * offsets( const StaticGraph< W , L > & g ) {
109 return( g.offsets() );
110 }
112 static int head( const StaticGraph< W , L > & g , int k ) {
113 return( g.arcHeads()[ k ] );
114 }
116 static const W & weight( const StaticGraph< W , L > & g , int k )
117 requires weighted
118 {
119 return( g.arcWeights()[ k ] );
120 }
121 };
122
125template< class W >
126struct Rows< ArrayGraph< W > >
127{
128 using Weight = W;
129 static constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
131 static const int * offsets( const ArrayGraph< W > & g ) {
132 return( g.offsets() );
133 }
135 static int head( const ArrayGraph< W > & g , int k ) {
136 return( g.heads()[ k ] );
137 }
139 static const W & weight( const ArrayGraph< W > & g , int k )
140 requires weighted
141 {
142 return( g.weightData()[ k ] );
143 }
144 };
145
147
149template< class G >
150concept RowGraph = requires { Rows< G >::weighted; };
151
154struct Arc
155{
156 int src = -1;
157 int pos = -1;
158 friend bool operator==( Arc , Arc ) = default;
159 };
160
163class OutArcIterator
164 : public boost::iterator_facade< OutArcIterator , Arc ,
165 boost::random_access_traversal_tag , Arc >
166{
167 public:
169 OutArcIterator( void ) = default;
171 OutArcIterator( int u , int k ) : f_u( u ) , f_k( k ) { }
172
173 private:
174 friend class boost::iterator_core_access;
175 Arc dereference( void ) const { return( Arc{ f_u , f_k } ); }
176 bool equal( const OutArcIterator & o ) const { return( f_k == o.f_k ); }
177 void increment( void ) { ++f_k; }
178 void decrement( void ) { --f_k; }
179 void advance( std::ptrdiff_t d ) { f_k += int( d ); }
180 std::ptrdiff_t distance_to( const OutArcIterator & o ) const {
181 return( o.f_k - f_k );
182 }
183
184 int f_u = -1;
185 int f_k = -1;
186 };
187
190template< class G >
191class AllArcIterator
192 : public boost::iterator_facade< AllArcIterator< G > , Arc ,
193 boost::forward_traversal_tag , Arc >
194{
195 public:
197 AllArcIterator( void ) = default;
199 AllArcIterator( const G & g , int k ) : f_g( &g ) , f_k( k ) { skip(); }
200
201 private:
202 friend class boost::iterator_core_access;
204 void skip( void ) {
205 const int * off = Rows< G >::offsets( *f_g );
206 while( ( f_u < f_g->n() ) && ( off[ f_u + 1 ] <= f_k ) )
207 ++f_u;
208 }
209 Arc dereference( void ) const { return( Arc{ f_u , f_k } ); }
210 bool equal( const AllArcIterator & o ) const { return( f_k == o.f_k ); }
211 void increment( void ) {
212 ++f_k;
213 skip();
214 }
215
216 const G * f_g = nullptr;
217 int f_k = 0;
218 int f_u = 0;
219 };
220
222template< class G >
223class HeadIterator
224 : public boost::iterator_facade< HeadIterator< G > , int ,
225 boost::random_access_traversal_tag , int >
226{
227 public:
229 HeadIterator( void ) = default;
231 HeadIterator( const G & g , int k ) : f_g( &g ) , f_k( k ) { }
232
233 private:
234 friend class boost::iterator_core_access;
235 int dereference( void ) const { return( Rows< G >::head( *f_g , f_k ) ); }
236 bool equal( const HeadIterator & o ) const { return( f_k == o.f_k ); }
237 void increment( void ) { ++f_k; }
238 void decrement( void ) { --f_k; }
239 void advance( std::ptrdiff_t d ) { f_k += int( d ); }
240 std::ptrdiff_t distance_to( const HeadIterator & o ) const {
241 return( o.f_k - f_k );
242 }
243
244 const G * f_g = nullptr;
245 int f_k = -1;
246 };
247
249
251template< class G >
252struct RowWeightMap
253{
254 using key_type = Arc;
255 using value_type = typename Rows< G >::Weight;
256 using reference = const value_type &;
257 using category = boost::readable_property_map_tag;
258 const G * g = nullptr;
260 friend reference get( const RowWeightMap & m , Arc a ) {
261 return( Rows< G >::weight( *m.g , a.pos ) );
262 }
263 };
264
266struct ArcIndexMap
267{
268 using key_type = Arc;
269 using value_type = int;
270 using reference = int;
271 using category = boost::readable_property_map_tag;
273 friend int get( ArcIndexMap , Arc a ) { return( a.pos ); }
274 };
275
278struct row_traversal : virtual boost::incidence_graph_tag ,
279 virtual boost::adjacency_graph_tag ,
280 virtual boost::vertex_list_graph_tag ,
281 virtual boost::edge_list_graph_tag
282{ };
283
285template< class G >
286struct RowTraits
287{
288 using vertex_descriptor = int;
289 using edge_descriptor = Arc;
290 using directed_category = boost::directed_tag;
291 using edge_parallel_category = boost::allow_parallel_edge_tag;
292 using traversal_category = row_traversal;
293 using vertex_iterator = boost::counting_iterator< int >;
294 using out_edge_iterator = OutArcIterator;
295 using adjacency_iterator = HeadIterator< G >;
296 using edge_iterator = AllArcIterator< G >;
297 using in_edge_iterator = void;
298 using vertices_size_type = int;
299 using edges_size_type = int;
300 using degree_size_type = int;
302 static vertex_descriptor null_vertex( void ) { return( -1 ); }
303 };
305
306/*--------------------------------------------------------------------------*/
307/*------------------------- THE GRAPHS OF LISTS ----------------------------*/
308/*--------------------------------------------------------------------------*/
309
313template< class G >
314struct Lists
315{ };
316
318template< class W , class T >
319struct Lists< ListGraph< W , T > >
320{
321 using Weight = W;
323 static const ListGraph< W , T > & lists( const ListGraph< W , T > & g ) {
324 return( g );
325 }
326 };
327
330template< class W , class T , class P >
331struct Lists< ObservedGraph< ListGraph< W , T > , P > >
332{
333 using Weight = W;
335 static const ListGraph< W , T > &
336 lists( const ObservedGraph< ListGraph< W , T > , P > & g ) {
337 return( g.graph() );
338 }
339 };
340
342
344template< class G >
345concept ListedGraph = requires { typename Lists< G >::Weight; };
346
348struct Edge
349{
350 int src = -1;
351 int dst = -1;
352 friend bool operator==( Edge , Edge ) = default;
353 };
354
357template< class G >
358class NodeIterator
359 : public boost::iterator_facade< NodeIterator< G > , int ,
360 boost::forward_traversal_tag , int >
361{
362 public:
364 NodeIterator( void ) = default;
366 NodeIterator( const G & g , int u ) : f_g( &g ) , f_u( u ) { skip(); }
367
368 private:
369 friend class boost::iterator_core_access;
370 void skip( void ) {
371 while( ( f_u < f_g->bound() ) && ! f_g->hasNode( f_u ) )
372 ++f_u;
373 }
374 int dereference( void ) const { return( f_u ); }
375 bool equal( const NodeIterator & o ) const { return( f_u == o.f_u ); }
376 void increment( void ) {
377 ++f_u;
378 skip();
379 }
380
381 const G * f_g = nullptr;
382 int f_u = 0;
383 };
384
386template< class I >
387class OutEdgeIterator
388 : public boost::iterator_facade< OutEdgeIterator< I > , Edge ,
389 boost::forward_traversal_tag , Edge >
390{
391 public:
393 OutEdgeIterator( void ) = default;
395 OutEdgeIterator( int u , I i ) : f_u( u ) , f_i( i ) { }
396
397 private:
398 friend class boost::iterator_core_access;
399 Edge dereference( void ) const { return( Edge{ f_u , int( *f_i ) } ); }
400 bool equal( const OutEdgeIterator & o ) const { return( f_i == o.f_i ); }
401 void increment( void ) { ++f_i; }
402
403 int f_u = -1;
404 I f_i{};
405 };
406
408template< class I >
409class AdjacentIterator
410 : public boost::iterator_facade< AdjacentIterator< I > , int ,
411 boost::forward_traversal_tag , int >
412{
413 public:
415 AdjacentIterator( void ) = default;
417 explicit AdjacentIterator( I i ) : f_i( i ) { }
418
419 private:
420 friend class boost::iterator_core_access;
421 int dereference( void ) const { return( int( *f_i ) ); }
422 bool equal( const AdjacentIterator & o ) const { return( f_i == o.f_i ); }
423 void increment( void ) { ++f_i; }
424
425 I f_i{};
426 };
427
429
432template< class G >
433struct ListWeightMap
434{
435 using key_type = Edge;
436 using value_type = typename Lists< G >::Weight;
437 using reference = value_type;
438 using category = boost::readable_property_map_tag;
439 const G * g = nullptr;
442 friend value_type get( const ListWeightMap & m , Edge e ) {
443 return( *Lists< G >::lists( *m.g ).weight( e.src , e.dst ) );
444 }
445 };
446
449struct list_traversal : virtual boost::incidence_graph_tag ,
450 virtual boost::adjacency_graph_tag ,
451 virtual boost::vertex_list_graph_tag
452{ };
453
455template< class G >
456struct ListTraits
457{
459 using List = std::ranges::iterator_t<
460 decltype( std::declval< const decltype( Lists< G >::lists(
461 std::declval< const G & >() ) ) & >()
462 .neighbors( 0 ) ) >;
463 using vertex_descriptor = int;
464 using edge_descriptor = Edge;
465 using directed_category = boost::directed_tag;
466 using edge_parallel_category = boost::disallow_parallel_edge_tag;
467 using traversal_category = list_traversal;
468 using vertex_iterator = NodeIterator< G >;
469 using out_edge_iterator = OutEdgeIterator< List >;
470 using adjacency_iterator = AdjacentIterator< List >;
471 using edge_iterator = void;
472 using in_edge_iterator = void;
473 using vertices_size_type = int;
474 using edges_size_type = int;
475 using degree_size_type = int;
477 static vertex_descriptor null_vertex( void ) { return( -1 ); }
478 };
480
481} // end( namespace ADS::bgl )
482
483/*--------------------------------------------------------------------------*/
484/*------------------------ THE TRAITS OF BOOST.GRAPH -----------------------*/
485/*--------------------------------------------------------------------------*/
486
488namespace boost
489{
490
491template< class W , ADS::ArcLayout L >
492struct graph_traits< ADS::StaticGraph< W , L > >
493 : ADS::bgl::RowTraits< ADS::StaticGraph< W , L > >
494{ };
495
496template< class W >
497struct graph_traits< ADS::ArrayGraph< W > >
498 : ADS::bgl::RowTraits< ADS::ArrayGraph< W > >
499{ };
500
501template< class W , class T >
502struct graph_traits< ADS::ListGraph< W , T > >
503 : ADS::bgl::ListTraits< ADS::ListGraph< W , T > >
504{ };
505
506template< class W , class T , class P >
507struct graph_traits< ADS::ObservedGraph< ADS::ListGraph< W , T > , P > >
508 : ADS::bgl::ListTraits< ADS::ObservedGraph< ADS::ListGraph< W , T > , P > >
509{ };
510
511template< class G >
512 requires( ADS::bgl::RowGraph< G > || ADS::bgl::ListedGraph< G > )
513struct graph_traits< const G > : graph_traits< G >
514{ };
515
516template< class G >
517 requires ADS::bgl::RowGraph< std::remove_const_t< G > >
518struct property_map< G , edge_weight_t >
519{
520 using type = ADS::bgl::RowWeightMap< std::remove_const_t< G > >;
521 using const_type = type;
522 };
523
524template< class G >
525 requires ADS::bgl::ListedGraph< std::remove_const_t< G > >
526struct property_map< G , edge_weight_t >
527{
528 using type = ADS::bgl::ListWeightMap< std::remove_const_t< G > >;
529 using const_type = type;
530 };
531
532template< class G >
533 requires( ADS::bgl::RowGraph< std::remove_const_t< G > > ||
534 ADS::bgl::ListedGraph< std::remove_const_t< G > > )
535struct property_map< G , vertex_index_t >
536{
537 using type = typed_identity_property_map< int >;
538 using const_type = type;
539 };
540
541template< class G >
542 requires ADS::bgl::RowGraph< std::remove_const_t< G > >
543struct property_map< G , edge_index_t >
544{
545 using type = ADS::bgl::ArcIndexMap;
546 using const_type = type;
547 };
548
549} // end( namespace boost )
551
552/*--------------------------------------------------------------------------*/
553/*---------------------- THE FUNCTIONS OF THE CONCEPTS ---------------------*/
554/*--------------------------------------------------------------------------*/
555
556namespace ADS
557{
558
560// the graphs in compressed rows
561
562template< bgl::RowGraph G >
563std::pair< boost::counting_iterator< int > , boost::counting_iterator< int > >
564vertices( const G & g )
565{
566 return( std::pair( boost::counting_iterator< int >( 0 ) ,
567 boost::counting_iterator< int >( g.n() ) ) );
568 }
569
570template< bgl::RowGraph G >
571int num_vertices( const G & g )
572{
573 return( g.n() );
574 }
575
576template< bgl::RowGraph G >
577int num_edges( const G & g )
578{
579 return( bgl::Rows< G >::offsets( g )[ g.n() ] );
580 }
581
582template< bgl::RowGraph G >
583std::pair< bgl::OutArcIterator , bgl::OutArcIterator >
584out_edges( int u , const G & g )
585{
586 const int * off = bgl::Rows< G >::offsets( g );
587 return( std::pair( bgl::OutArcIterator( u , off[ u ] ) ,
588 bgl::OutArcIterator( u , off[ u + 1 ] ) ) );
589 }
590
591template< bgl::RowGraph G >
592int out_degree( int u , const G & g )
593{
594 const int * off = bgl::Rows< G >::offsets( g );
595 return( off[ u + 1 ] - off[ u ] );
596 }
597
598template< bgl::RowGraph G >
599int source( bgl::Arc a , const G & )
600{
601 return( a.src );
602 }
603
604template< bgl::RowGraph G >
605int target( bgl::Arc a , const G & g )
606{
607 return( bgl::Rows< G >::head( g , a.pos ) );
608 }
609
610template< bgl::RowGraph G >
611std::pair< bgl::HeadIterator< G > , bgl::HeadIterator< G > >
612adjacent_vertices( int u , const G & g )
613{
614 const int * off = bgl::Rows< G >::offsets( g );
615 return( std::pair( bgl::HeadIterator< G >( g , off[ u ] ) ,
616 bgl::HeadIterator< G >( g , off[ u + 1 ] ) ) );
617 }
618
619// edges() for each graph, more specialized than edges( g ) of
620// MinimumSpanningTree.h, which is ADS::edges< G >( g ) then
621template< class W , ArcLayout L >
622std::pair< bgl::AllArcIterator< StaticGraph< W , L > > ,
623 bgl::AllArcIterator< StaticGraph< W , L > > >
624edges( const StaticGraph< W , L > & g )
625{
626 using It = bgl::AllArcIterator< StaticGraph< W , L > >;
627 return( std::pair( It( g , 0 ) , It( g , num_edges( g ) ) ) );
628 }
629
630template< class W >
631std::pair< bgl::AllArcIterator< ArrayGraph< W > > ,
632 bgl::AllArcIterator< ArrayGraph< W > > >
633edges( const ArrayGraph< W > & g )
634{
635 using It = bgl::AllArcIterator< ArrayGraph< W > >;
636 return( std::pair( It( g , 0 ) , It( g , num_edges( g ) ) ) );
637 }
638
639template< bgl::RowGraph G >
640 requires bgl::Rows< G >::weighted
641bgl::RowWeightMap< G > get( boost::edge_weight_t , const G & g )
642{
643 return( bgl::RowWeightMap< G >{ &g } );
644 }
645
646template< bgl::RowGraph G >
647 requires bgl::Rows< G >::weighted
648const typename bgl::Rows< G >::Weight & get( boost::edge_weight_t ,
649 const G & g ,
650 bgl::Arc a )
651{
652 return( bgl::Rows< G >::weight( g , a.pos ) );
653 }
654
655template< bgl::RowGraph G >
656bgl::ArcIndexMap get( boost::edge_index_t , const G & )
657{
658 return( bgl::ArcIndexMap() );
659 }
660
661template< bgl::RowGraph G >
662int get( boost::edge_index_t , const G & , bgl::Arc a )
663{
664 return( a.pos );
665 }
666
667// the graphs of lists
668
669template< bgl::ListedGraph G >
670std::pair< bgl::NodeIterator< G > , bgl::NodeIterator< G > >
671vertices( const G & g )
672{
673 return( std::pair( bgl::NodeIterator< G >( g , 0 ) ,
674 bgl::NodeIterator< G >( g , g.bound() ) ) );
675 }
676
677template< bgl::ListedGraph G >
678int num_vertices( const G & g )
679{
680 return( g.bound() );
681 }
682
683template< bgl::ListedGraph G >
684auto out_edges( int u , const G & g )
685{
686 using It = typename boost::graph_traits< G >::out_edge_iterator;
687 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
688 return( std::pair< It , It >( It( u , r.begin() ) , It( u , r.end() ) ) );
689 }
690
691template< bgl::ListedGraph G >
692int out_degree( int u , const G & g )
693{
694 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
695 return( int( std::ranges::distance( r ) ) );
696 }
697
698template< bgl::ListedGraph G >
699int source( bgl::Edge e , const G & )
700{
701 return( e.src );
702 }
703
704template< bgl::ListedGraph G >
705int target( bgl::Edge e , const G & )
706{
707 return( e.dst );
708 }
709
710template< bgl::ListedGraph G >
711auto adjacent_vertices( int u , const G & g )
712{
713 using It = typename boost::graph_traits< G >::adjacency_iterator;
714 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
715 return( std::pair< It , It >( It( r.begin() ) , It( r.end() ) ) );
716 }
717
718template< bgl::ListedGraph G >
719std::pair< bgl::Edge , bool > edge( int u , int v , const G & g )
720{
721 return( std::pair( bgl::Edge{ u , v } ,
722 bgl::Lists< G >::lists( g ).hasEdge( u , v ) ) );
723 }
724
725template< bgl::ListedGraph G >
726std::pair< bgl::Edge , bool > add_edge( int u , int v , G & g )
727{
728 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
729 return( std::pair( bgl::Edge{ u , v } , false ) );
730 g.insertEdge( u , v );
731 return( std::pair( bgl::Edge{ u , v } , true ) );
732 }
733
734template< bgl::ListedGraph G >
735std::pair< bgl::Edge , bool >
736add_edge( int u , int v , const typename bgl::Lists< G >::Weight & w , G & g )
737{
738 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
739 return( std::pair( bgl::Edge{ u , v } , false ) );
740 g.insertEdge( u , v , w );
741 return( std::pair( bgl::Edge{ u , v } , true ) );
742 }
743
744template< bgl::ListedGraph G >
745void remove_edge( int u , int v , G & g )
746{
747 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
748 g.deleteEdge( u , v );
749 }
750
751template< bgl::ListedGraph G >
752void remove_edge( bgl::Edge e , G & g )
753{
754 remove_edge( e.src , e.dst , g );
755 }
756
757template< bgl::ListedGraph G >
758int add_vertex( G & g )
759{
760 return( g.insertNode() );
761 }
762
763template< bgl::ListedGraph G >
764void clear_vertex( int u , G & g )
765{
766 const auto & L = bgl::Lists< G >::lists( g );
767 for( int v : L.adj( u ) )
768 if( L.hasEdge( u , v ) ) // gone with [ v , u ] if unoriented
769 g.deleteEdge( u , v );
770 if( L.directed() )
771 for( int v : L.inAdj( u ) )
772 g.deleteEdge( v , u );
773 }
774
775template< bgl::ListedGraph G >
776void remove_vertex( int u , G & g )
777{
778 g.deleteNode( u );
779 }
780
781template< bgl::ListedGraph G >
782bgl::ListWeightMap< G > get( boost::edge_weight_t , const G & g )
783{
784 return( bgl::ListWeightMap< G >{ &g } );
785 }
786
787template< bgl::ListedGraph G >
788typename bgl::Lists< G >::Weight get( boost::edge_weight_t , const G & g ,
789 bgl::Edge e )
790{
791 return( *bgl::Lists< G >::lists( g ).weight( e.src , e.dst ) );
792 }
793
794// both
795
796template< class G >
797 requires( bgl::RowGraph< G > || bgl::ListedGraph< G > )
798boost::typed_identity_property_map< int > get( boost::vertex_index_t ,
799 const G & )
800{
801 return( boost::typed_identity_property_map< int >() );
802 }
803
804template< class G >
805 requires( bgl::RowGraph< G > || bgl::ListedGraph< G > )
806int get( boost::vertex_index_t , const G & , int u )
807{
808 return( u );
809 }
811
812/*--------------------------------------------------------------------------*/
813
814} // end( namespace ADS )
815
816#endif /* ADS_WITH_BOOST */
817
818/*--------------------------------------------------------------------------*/
819
820#endif /* Graph.h included */
821
822/*--------------------------------------------------------------------------*/
823/*----------------------------- End File Graph.h ---------------------------*/
824/*--------------------------------------------------------------------------*/
The static GRAPH with vectors of adjacency of section 9.4 [see Graph.h]: the adjacent nodes of all th...
The GRAPH with lists of adjacency of section 9.4 [see Graph.h]: for each node the LIST of its adjacen...
A GRAPH that tells its changes, beyond the book: the wrapper Observable< G , O... > routes the change...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
a static graph with vectors of adjacency, all in one vector
Definition ArrayGraph.h:62
a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their...
Definition StaticGraph.h:275
std::vector< WeightedEdge< typename G::Weight > > edges(const G &g)
the edges of an unoriented graph, each once, from its smaller end
GrB_Type type(void)
the type of GraphBLAS of the numbers T: GrB_BOOL, the integers of 8 to 64 bits with or without sign,...
Definition GraphBLAS.h:128
Definition ADT.h:96