61#ifndef __ADS_interop_lemon_Digraphs
62 #define __ADS_interop_lemon_Digraphs
69#if ! __has_include( <lemon/core.h> )
70 #error "the interoperation with LEMON needs the headers of LEMON"
77#include <lemon/bits/graph_extender.h>
78#include <lemon/core.h>
97template<
class W , ArcLayout L = ArcLayout::Separate >
130 Arc( ::lemon::Invalid ) { }
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 ); }
171 return(
Node( f_tail[ std::size_t( a.
id ) ] ) );
175 return(
Node( f_g->arcHeads()[ a.
id ] ) );
189 const int k = f_g->offsets()[ u.
id ];
190 a.
id = k < f_g->offsets()[ u.
id + 1 ] ? k : -1;
194 const int k = a.
id + 1;
195 a.
id = k < f_g->offsets()[ f_tail[ std::size_t( a.
id ) ] + 1 ] ? k : -1;
199 a.
id = f_firstIn[ std::size_t( v.
id ) ];
207 std::vector< int > f_tail;
208 std::vector< int > f_nextIn;
209 std::vector< int > f_firstIn;
213template<
class W , ArcLayout L >
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;
226 f_nextIn[ std::size_t( k ) ] = f_firstIn[ std::size_t( v ) ];
227 f_firstIn[ std::size_t( v ) ] = k;
237template<
class W , ArcLayout L = ArcLayout::Separate >
239 :
public ::lemon::DigraphExtender< StaticDigraphBase< W , L > >
242 using P = ::lemon::DigraphExtender< StaticDigraphBase< W , L > >;
249 P::notifier(
typename P::Node() ).build();
250 P::notifier(
typename P::Arc() ).build();
261 using Key =
typename P::Arc;
283template<
class GR ,
class LM >
292 GraphView(
const GR & g ,
const LM & l ) : f_g( g ) , f_l( l ) { }
295 [[nodiscard]]
int n(
void )
const {
return( ::lemon::countNodes( f_g ) ); }
298 [[nodiscard]]
int bound(
void )
const {
return( f_g.maxNodeId() + 1 ); }
301 [[nodiscard]]
bool directed(
void )
const {
return(
true ); }
305 if( ( u < 0 ) || ( u >=
bound() ) )
307 if constexpr(
requires { f_g.valid( f_g.nodeFromId( u ) ); } )
308 return( f_g.valid( f_g.nodeFromId( u ) ) );
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 ) );
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 ) ) );
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 ] ) );
341 [[nodiscard]] std::vector< int >
adj(
int u )
const {
342 std::vector< int > v;
343 forEachAdj( u , [ & ](
int x ) { v.push_back( x ); } );
348 [[nodiscard]]
const GR &
digraph(
void )
const {
return( f_g ); }
364template<
class GR ,
class LM >
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 ) );
375 E.emplace_back( u , v , W( l[ a ] ) );
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
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
Arc(void)=default
an invalid arc
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