75#ifndef __ADS_interop_networkit_NetworKit
76 #define __ADS_interop_networkit_NetworKit
83#if ! __has_include( <networkit/graph/Graph.hpp> )
84 #error "the interoperation with NetworKit needs the headers of NetworKit"
91#include <networkit/base/DynAlgorithm.hpp>
92#include <networkit/dynamics/GraphEvent.hpp>
93#include <networkit/graph/Graph.hpp>
115[[nodiscard]] NetworKit::edgeweight
weightOf(
const W & w )
117 if constexpr( std::is_arithmetic_v< W > )
118 return( NetworKit::edgeweight( w ) );
120 return( NetworKit::defaultEdgeWeight );
128 using W =
typename G::Weight;
129 constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
130 if constexpr(
requires {
135 const std::size_t n = std::size_t( g.n() );
136 std::vector< NetworKit::index > off( n + 1 , 0 ) , col;
137 std::vector< double > w;
138 col.reserve( std::size_t( g.arcs() ) );
139 const auto h = g.arcHeads();
140 for( std::size_t u = 0 ; u < n ; ++u ) {
141 for(
int k = g.offsets()[ u ] ; k < g.offsets()[ u + 1 ] ; ++k )
142 if( g.directed() || ( std::size_t( h[ k ] ) > u ) ) {
143 col.push_back( NetworKit::index( h[ k ] ) );
144 if constexpr( weighted )
145 w.push_back(
weightOf( g.arcWeights()[ k ] ) );
147 off[ u + 1 ] = NetworKit::index( col.size() );
149 return( NetworKit::Graph::fromCSR( off , col , w , g.directed() ,
153 const int b = g.bound();
154 NetworKit::Graph N( NetworKit::count( b ) , weighted , g.directed() );
155 for(
int u = 0 ; u < b ; ++u ) {
156 if( ! g.hasNode( u ) )
158 if constexpr( weighted )
159 g.forEachEdge( u , [ & ](
int v ,
const W & x ) {
160 if( g.directed() || ( v > u ) )
161 N.addEdge( NetworKit::node( u ) , NetworKit::node( v ) , weightOf( x ) );
164 g.forEachAdj( u , [ & ](
int v ) {
165 if( g.directed() || ( v > u ) )
166 N.addEdge( NetworKit::node( u ) , NetworKit::node( v ) );
169 for(
int u = 0 ; u < b ; ++u )
170 if( ! g.hasNode( u ) )
171 N.removeNode( NetworKit::node( u ) );
199 [[nodiscard]]
const NetworKit::Graph &
graph(
void )
const {
205 int attach( NetworKit::DynAlgorithm & a ,
207 for( std::size_t q = 0 ; q < f_algo.size() ; ++q )
208 if( f_algo[ q ].a ==
nullptr ) {
209 f_algo[ q ] = Slot{ &a , kinds };
212 f_algo.push_back( Slot{ &a , kinds } );
213 return(
int( f_algo.size() ) - 1 );
219 ADS_PRE( (
id >= 0 ) && ( std::size_t(
id ) < f_algo.size() ) &&
220 ( f_algo[ std::size_t(
id ) ].a !=
nullptr ) );
221 f_algo[ std::size_t(
id ) ].a =
nullptr;
225 [[nodiscard]] std::size_t
told(
void )
const {
return( f_told ); }
240 void on( std::span< const Event > s ) {
241 std::vector< NetworKit::GraphEvent > E;
242 std::vector< unsigned > K;
243 E.reserve( s.size() );
244 K.reserve( s.size() );
245 for(
const Event & e : s ) {
246 E.push_back( apply( e ) );
249 std::vector< NetworKit::GraphEvent > F;
250 for(
const Slot & x : f_algo ) {
254 for( std::size_t q = 0 ; q < E.size() ; ++q )
255 if( ( K[ q ] & x.kinds ) != 0 )
256 F.push_back( E[ q ] );
258 x.a->updateBatch( std::span< const NetworKit::GraphEvent >( F ) );
269 NetworKit::DynAlgorithm * a;
274 NetworKit::GraphEvent apply(
const Event & e ) {
275 using NE = NetworKit::GraphEvent;
276 using NetworKit::node;
277 if(
const auto * x = std::get_if< NodeInserted >( &e ) ) {
278 const node u = node( x->u );
279 if( u < f_nk.upperNodeIdBound() ) {
280 f_nk.restoreNode( u );
281 return( NE( NE::NODE_RESTORATION , u ) );
283 while( f_nk.upperNodeIdBound() <= u )
284 ( void )f_nk.addNode();
285 return( NE( NE::NODE_ADDITION , u ) );
287 if(
const auto * x = std::get_if< NodeDeleted >( &e ) ) {
288 f_nk.removeNode( node( x->u ) );
289 return( NE( NE::NODE_REMOVAL , node( x->u ) ) );
291 if(
const auto * x = std::get_if< EdgeInserted< Weight > >( &e ) ) {
292 const NetworKit::edgeweight w =
weightOf( x->w );
293 f_nk.addEdge( node( x->u ) , node( x->v ) , w );
294 return( NE( NE::EDGE_ADDITION , node( x->u ) , node( x->v ) , w ) );
296 if(
const auto * x = std::get_if< EdgeDeleted< Weight > >( &e ) ) {
297 f_nk.removeEdge( node( x->u ) , node( x->v ) );
298 return( NE( NE::EDGE_REMOVAL , node( x->u ) , node( x->v ) ) );
300 const auto & x = std::get< WeightChanged< Weight > >( e );
301 const NetworKit::edgeweight w =
weightOf( x.to );
302 f_nk.setWeight( node( x.u ) , node( x.v ) , w );
303 return( NE( NE::EDGE_WEIGHT_UPDATE , node( x.u ) , node( x.v ) , w ) );
307 void one(
const Event & e ) {
308 const NetworKit::GraphEvent x = apply( e );
310 for(
const Slot & s : f_algo )
311 if( ( s.a !=
nullptr ) && ( ( s.kinds & k ) != 0 ) ) {
317 NetworKit::Graph f_nk;
318 std::vector< Slot > f_algo;
319 std::size_t f_told = 0;
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A GRAPH that tells its changes, beyond the book: the wrapper Observable< G , O... > routes the change...
void on(const EdgeInserted< Weight > &e)
the edge inserted
Definition NetworKit.h:232
NetworKitMirror(const G &g)
the mirror of as it is now, by toNetworKit()
Definition NetworKit.h:193
void on(const WeightChanged< Weight > &e)
the weight of an edge changed
Definition NetworKit.h:236
const NetworKit::Graph & graph(void) const
the graph of NetworKit, on which the algorithms are built
Definition NetworKit.h:199
typename G::Weight Weight
the type of the weights
Definition NetworKit.h:189
NetworKitMirror & operator=(const NetworKitMirror &)=delete
void detach(int id)
the algorithm number detached
Definition NetworKit.h:218
NetworKitMirror(const NetworKitMirror &)=delete
void on(std::span< const Event > s)
the events of a batch, coalesced: the graph changed by all of them, then one updateBatch() for each a...
Definition NetworKit.h:240
std::size_t told(void) const
the number of the events told to the algorithms, one per algorithm
Definition NetworKit.h:225
void on(const NodeDeleted &e) noexcept
the node deleted, after its edges
Definition NetworKit.h:230
int attach(NetworKit::DynAlgorithm &a, unsigned kinds=GraphEvents::EDGES)
the algorithm attached, told of the events of the kinds kinds [see GraphEvents]; its number
Definition NetworKit.h:205
void on(const EdgeDeleted< Weight > &e) noexcept
the edge deleted
Definition NetworKit.h:234
GraphEvent< Weight > Event
the events of ADS
Definition NetworKit.h:190
void on(const NodeInserted &e)
the node inserted
Definition NetworKit.h:228
The GRAPH of chapter 9 and its realizations.
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
std::variant< NodeInserted, NodeDeleted, EdgeInserted< W >, EdgeDeleted< W >, WeightChanged< W > > GraphEvent
an event, whose kind is its index in the variant, the variant of GraphModifications< W >
Definition Observable.h:237
constexpr unsigned kindOf(const GraphEvent< W > &e)
the bit of the kind of
Definition Observable.h:255
NetworKit::Graph toNetworKit(const G &g)
copied into a graph of NetworKit, oriented and weighted as it is, the names of the nodes kept,...
Definition NetworKit.h:126
NetworKit::edgeweight weightOf(const W &w)
the weight of ADS as a weight of NetworKit, the default weight of NetworKit if the graph is unweight...
Definition NetworKit.h:115
Definition NetworKit.h:106
an edge deleted, the weight that it had
Definition Observable.h:180
an edge , or with , inserted with the weight
Definition Observable.h:168
static constexpr unsigned EDGES
the three on edges
Definition Observable.h:249
a node deleted, after the deletion of its edges
Definition Observable.h:157
a node inserted
Definition Observable.h:148
the weight of an edge changed from from to to
Definition Observable.h:192