ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Digraphs.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Digraphs.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
57/*--------------------------------------------------------------------------*/
58/*----------------------------- DEFINITIONS --------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#ifndef __ADS_interop_lemon_Digraphs
62 #define __ADS_interop_lemon_Digraphs
63/* self-identification: #endif constant */
64
65/*--------------------------------------------------------------------------*/
66/*------------------------------ INCLUDES ----------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69#if ! __has_include( <lemon/core.h> )
70 #error "the interoperation with LEMON needs the headers of LEMON"
71#endif
72
73#include "ADS/Contract.h"
74#include "ADS/graph/Graph.h"
76
77#include <lemon/bits/graph_extender.h>
78#include <lemon/core.h>
79
80#include <cstddef>
81#include <tuple>
82#include <vector>
83
84/*--------------------------------------------------------------------------*/
85/*--------------------------- NAMESPACE ------------------------------------*/
86/*--------------------------------------------------------------------------*/
87
89{
90
91/*--------------------------------------------------------------------------*/
92/*----------------------- CLASS StaticDigraphBase --------------------------*/
93/*--------------------------------------------------------------------------*/
94
97template< class W , ArcLayout L = ArcLayout::Separate >
99{
100
101/*--------------------------------------------------------------------------*/
102 public:
104 struct Node
105 {
106 int id = -1;
108 Node( void ) = default;
110 explicit Node( int i ) : id( i ) { }
112 Node( ::lemon::Invalid ) { }
114 bool operator==( Node o ) const { return( id == o.id ); }
116 bool operator!=( Node o ) const { return( id != o.id ); }
118 bool operator<( Node o ) const { return( id < o.id ); }
119 };
120
122 struct Arc
123 {
124 int id = -1;
126 Arc( void ) = default;
128 explicit Arc( int i ) : id( i ) { }
130 Arc( ::lemon::Invalid ) { }
132 bool operator==( Arc o ) const { return( id == o.id ); }
134 bool operator!=( Arc o ) const { return( id != o.id ); }
136 bool operator<( Arc o ) const { return( id < o.id ); }
137 };
138
139 using NodeNumTag = ::lemon::True;
140 using ArcNumTag = ::lemon::True;
141
144 void attach( const StaticGraph< W , L > & g );
145
147 [[nodiscard]] const StaticGraph< W , L > & graph( void ) const {
148 return( *f_g );
149 }
150
152 [[nodiscard]] int nodeNum( void ) const { return( f_g->n() ); }
154 [[nodiscard]] int arcNum( void ) const { return( f_g->arcs() ); }
156 [[nodiscard]] int maxNodeId( void ) const { return( f_g->n() - 1 ); }
158 [[nodiscard]] int maxArcId( void ) const { return( f_g->arcs() - 1 ); }
159
161 static int id( Node u ) { return( u.id ); }
163 static int id( Arc a ) { return( a.id ); }
165 static Node nodeFromId( int i ) { return( Node( i ) ); }
167 static Arc arcFromId( int i ) { return( Arc( i ) ); }
168
170 [[nodiscard]] Node source( Arc a ) const {
171 return( Node( f_tail[ std::size_t( a.id ) ] ) );
172 }
173
174 [[nodiscard]] Node target( Arc a ) const {
175 return( Node( f_g->arcHeads()[ a.id ] ) );
176 }
177
179 void first( Node & u ) const { u.id = f_g->n() - 1; }
181 static void next( Node & u ) { --u.id; }
183 void first( Arc & a ) const { a.id = f_g->arcs() - 1; }
185 static void next( Arc & a ) { --a.id; }
186
188 void firstOut( Arc & a , const Node & u ) const {
189 const int k = f_g->offsets()[ u.id ];
190 a.id = k < f_g->offsets()[ u.id + 1 ] ? k : -1;
191 }
192
193 void nextOut( Arc & a ) const {
194 const int k = a.id + 1;
195 a.id = k < f_g->offsets()[ f_tail[ std::size_t( a.id ) ] + 1 ] ? k : -1;
196 }
197
198 void firstIn( Arc & a , const Node & v ) const {
199 a.id = f_firstIn[ std::size_t( v.id ) ];
200 }
201
202 void nextIn( Arc & a ) const { a.id = f_nextIn[ std::size_t( a.id ) ]; }
203
204/*--------------------------------------------------------------------------*/
205 private:
206 const StaticGraph< W , L > * f_g = nullptr;
207 std::vector< int > f_tail;
208 std::vector< int > f_nextIn;
209 std::vector< int > f_firstIn;
210
211 }; // end( class StaticDigraphBase )
212
213template< class W , ArcLayout L >
215{
216 f_g = &g;
217 const int n = g.n();
218 const int a = g.arcs();
219 f_tail.assign( std::size_t( a ) , 0 );
220 f_nextIn.assign( std::size_t( a ) , -1 );
221 f_firstIn.assign( std::size_t( n ) , -1 );
222 for( int u = n - 1 ; u >= 0 ; --u )
223 for( int k = g.offsets()[ u + 1 ] - 1 ; k >= g.offsets()[ u ] ; --k ) {
224 f_tail[ std::size_t( k ) ] = u;
225 const int v = g.arcHeads()[ k ];
226 f_nextIn[ std::size_t( k ) ] = f_firstIn[ std::size_t( v ) ];
227 f_firstIn[ std::size_t( v ) ] = k;
228 }
229 }
230
231/*--------------------------------------------------------------------------*/
232/*----------------------- CLASS StaticDigraphView --------------------------*/
233/*--------------------------------------------------------------------------*/
234
237template< class W , ArcLayout L = ArcLayout::Separate >
239 : public ::lemon::DigraphExtender< StaticDigraphBase< W , L > >
240{
241
242 using P = ::lemon::DigraphExtender< StaticDigraphBase< W , L > >;
243
244/*--------------------------------------------------------------------------*/
245 public:
248 P::attach( g );
249 P::notifier( typename P::Node() ).build(); // the maps get their room
250 P::notifier( typename P::Arc() ).build();
251 }
252
257
260 {
261 using Key = typename P::Arc;
262 using Value = W;
265 W operator[]( Key a ) const { return( g->graph().arcWeights()[ a.id ] ); }
266 };
267
269 [[nodiscard]] WeightMap weights( void ) const
271 {
272 return( WeightMap{ this } );
273 }
274
275 }; // end( class StaticDigraphView )
276
277/*--------------------------------------------------------------------------*/
278/*---------------------------- CLASS GraphView -----------------------------*/
279/*--------------------------------------------------------------------------*/
280
283template< class GR , class LM >
285{
286
287/*--------------------------------------------------------------------------*/
288 public:
289 using Weight = typename LM::Value;
290
292 GraphView( const GR & g , const LM & l ) : f_g( g ) , f_l( l ) { }
293
295 [[nodiscard]] int n( void ) const { return( ::lemon::countNodes( f_g ) ); }
296
298 [[nodiscard]] int bound( void ) const { return( f_g.maxNodeId() + 1 ); }
299
301 [[nodiscard]] bool directed( void ) const { return( true ); }
302
304 [[nodiscard]] bool hasNode( int u ) const {
305 if( ( u < 0 ) || ( u >= bound() ) )
306 return( false );
307 if constexpr( requires { f_g.valid( f_g.nodeFromId( u ) ); } )
308 return( f_g.valid( f_g.nodeFromId( u ) ) );
309 else
310 return( true );
311 }
312
314 [[nodiscard]] std::vector< int > V( void ) const {
315 std::vector< int > v;
316 for( typename GR::NodeIt x( f_g ) ; x != ::lemon::INVALID ; ++x )
317 v.push_back( f_g.id( x ) );
318 return( v );
319 }
320
323 template< class F >
324 void forEachAdj( int u , F && f ) const {
325 for( typename GR::OutArcIt a( f_g , f_g.nodeFromId( u ) ) ;
326 a != ::lemon::INVALID ; ++a )
327 f( f_g.id( f_g.target( a ) ) );
328 }
329
332 template< class F >
333 void forEachEdge( int u , F && f ) const {
334 for( typename GR::OutArcIt a( f_g , f_g.nodeFromId( u ) ) ;
335 a != ::lemon::INVALID ; ++a )
336 f( f_g.id( f_g.target( a ) ) , Weight( f_l[ a ] ) );
337 }
338
341 [[nodiscard]] std::vector< int > adj( int u ) const {
342 std::vector< int > v;
343 forEachAdj( u , [ & ]( int x ) { v.push_back( x ); } );
344 return( v );
345 }
346
348 [[nodiscard]] const GR & digraph( void ) const { return( f_g ); }
349
350/*--------------------------------------------------------------------------*/
351 private:
352 const GR & f_g;
353 const LM & f_l;
354
355 }; // end( class GraphView )
356
357/*--------------------------------------------------------------------------*/
358/*------------------------------- toStatic ---------------------------------*/
359/*--------------------------------------------------------------------------*/
360
364template< class GR , class LM >
365[[nodiscard]] StaticGraph< typename LM::Value > toStatic( const GR & g ,
366 const LM & l )
367{
368 using W = typename LM::Value;
369 std::vector< std::tuple< int , int , W > > E;
370 E.reserve( std::size_t( ::lemon::countArcs( g ) ) );
371 for( typename GR::ArcIt a( g ) ; a != ::lemon::INVALID ; ++a ) {
372 const int u = g.id( g.source( a ) );
373 const int v = g.id( g.target( a ) );
374 if( u != v )
375 E.emplace_back( u , v , W( l[ a ] ) );
376 }
377 return( StaticGraph< W >( g.maxNodeId() + 1 , E , true ) );
378 }
379
380/*--------------------------------------------------------------------------*/
381
382} // end( namespace ADS::lemon_interop )
383
384/*--------------------------------------------------------------------------*/
385
386#endif /* Digraphs.h included */
387
388/*--------------------------------------------------------------------------*/
389/*-------------------------- End File Digraphs.h ---------------------------*/
390/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their...
Definition StaticGraph.h:275
static constexpr bool weighted
whether the arcs have a weight
Definition StaticGraph.h:282
int arcs(void) const
the number of arcs, 2 m() if the graph is unoriented
Definition StaticGraph.h:377
int n(void) const
the number of nodes
Definition StaticGraph.h:371
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
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
bool hasNode(int u) const
whether is the id of a node
Definition Digraphs.h:304
bool directed(void) const
oriented, as the digraphs of LEMON
Definition Digraphs.h:301
void forEachAdj(int u, F &&f) const
for the head of each arc out of
Definition Digraphs.h:324
std::vector< int > adj(int u) const
the heads of the arcs out of
Definition Digraphs.h:341
int n(void) const
the number of nodes, counted in unless the graph knows it
Definition Digraphs.h:295
std::vector< int > V(void) const
the ids of the nodes
Definition Digraphs.h:314
typename LM::Value Weight
the type of the weights
Definition Digraphs.h:289
GraphView(const GR &g, const LM &l)
the view of with the lengths , which must outlive it
Definition Digraphs.h:292
void forEachEdge(int u, F &&f) const
for each arc of length
Definition Digraphs.h:333
const GR & digraph(void) const
the digraph of LEMON
Definition Digraphs.h:348
int bound(void) const
the names are in , maxNodeId() + 1
Definition Digraphs.h:298
the base of lemon::DigraphExtender on a StaticGraph< W , L >: the arcs are the places of its compress...
Definition Digraphs.h:99
int maxNodeId(void) const
the largest id of a node
Definition Digraphs.h:156
const StaticGraph< W, L > & graph(void) const
the graph of ADS
Definition Digraphs.h:147
static void next(Node &u)
the next node
Definition Digraphs.h:181
::lemon::True NodeNumTag
nodeNum() in O( 1 )
Definition Digraphs.h:139
::lemon::True ArcNumTag
arcNum() in O( 1 )
Definition Digraphs.h:140
void attach(const StaticGraph< W, L > &g)
the base of : the tails and the lists of the arcs into each node, in time
Definition Digraphs.h:214
Node target(Arc a) const
the head of an arc
Definition Digraphs.h:174
void first(Node &u) const
the first node, the last name
Definition Digraphs.h:179
Node source(Arc a) const
the tail of an arc
Definition Digraphs.h:170
int nodeNum(void) const
the number of nodes
Definition Digraphs.h:152
void first(Arc &a) const
the first arc, the last place
Definition Digraphs.h:183
void nextOut(Arc &a) const
the next arc out of the same node
Definition Digraphs.h:193
static Arc arcFromId(int i)
the arc of id
Definition Digraphs.h:167
static Node nodeFromId(int i)
the node of id
Definition Digraphs.h:165
void firstIn(Arc &a, const Node &v) const
the first arc into
Definition Digraphs.h:198
int maxArcId(void) const
the largest id of an arc
Definition Digraphs.h:158
void firstOut(Arc &a, const Node &u) const
the first arc out of , in the order of the row
Definition Digraphs.h:188
void nextIn(Arc &a) const
the next arc into the same node
Definition Digraphs.h:202
static int id(Node u)
the id of a node
Definition Digraphs.h:161
static int id(Arc a)
the id of an arc
Definition Digraphs.h:163
static void next(Arc &a)
the next arc
Definition Digraphs.h:185
int arcNum(void) const
the number of arcs
Definition Digraphs.h:154
StaticDigraphView(const StaticGraph< W, L > &g)
the view of , which must outlive it
Definition Digraphs.h:247
StaticDigraphView & operator=(const StaticDigraphView &)=delete
not assigned
StaticDigraphView(const StaticDigraphView &)=delete
not copied, as the graphs of LEMON
WeightMap weights(void) const
the weights of the arcs, read in the rows of the graph of ADS
Definition Digraphs.h:269
The GRAPH of chapter 9 and its realizations.
StaticGraph< typename LM::Value > toStatic(const GR &g, const LM &l)
the copy of the digraph of LEMON with the lengths into a StaticGraph of ADS, the nodes by their ids...
Definition Digraphs.h:365
Definition Digraphs.h:89
an arc, by its place in the rows
Definition Digraphs.h:123
Arc(int i)
the arc
Definition Digraphs.h:128
Arc(::lemon::Invalid)
the invalid arc of LEMON
Definition Digraphs.h:130
int id
the place, -1 if invalid
Definition Digraphs.h:124
bool operator<(Arc o) const
the order of the ids
Definition Digraphs.h:136
bool operator==(Arc o) const
whether the two arcs are the same
Definition Digraphs.h:132
bool operator!=(Arc o) const
whether the two arcs differ
Definition Digraphs.h:134
a node, by its name in the graph of ADS
Definition Digraphs.h:105
bool operator==(Node o) const
whether the two nodes are the same
Definition Digraphs.h:114
Node(::lemon::Invalid)
the invalid node of LEMON
Definition Digraphs.h:112
Node(int i)
the node
Definition Digraphs.h:110
bool operator<(Node o) const
the order of the ids
Definition Digraphs.h:118
bool operator!=(Node o) const
whether the two nodes differ
Definition Digraphs.h:116
int id
the name, -1 if invalid
Definition Digraphs.h:106
Node(void)=default
an invalid node
the weights of the arcs, a ReadMap of LEMON
Definition Digraphs.h:260
typename P::Arc Key
the arcs
Definition Digraphs.h:261
W operator[](Key a) const
the weight of the arc
Definition Digraphs.h:265
W Value
the weights
Definition Digraphs.h:262
const StaticDigraphView * g
the view
Definition Digraphs.h:263