ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
NetworKit.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File NetworKit.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_interop_networkit_NetworKit
76 #define __ADS_interop_networkit_NetworKit
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#if ! __has_include( <networkit/graph/Graph.hpp> )
84 #error "the interoperation with NetworKit needs the headers of NetworKit"
85#endif
86
87#include "ADS/Contract.h"
88#include "ADS/graph/Graph.h"
90
91#include <networkit/base/DynAlgorithm.hpp>
92#include <networkit/dynamics/GraphEvent.hpp>
93#include <networkit/graph/Graph.hpp>
94
95#include <cstddef>
96#include <span>
97#include <type_traits>
98#include <variant>
99#include <vector>
100
101/*--------------------------------------------------------------------------*/
102/*--------------------------- NAMESPACE ------------------------------------*/
103/*--------------------------------------------------------------------------*/
104
106{
107
108/*--------------------------------------------------------------------------*/
109/*-------------------------------- THE COPY --------------------------------*/
110/*--------------------------------------------------------------------------*/
111
114template< class W >
115[[nodiscard]] NetworKit::edgeweight weightOf( const W & w )
116{
117 if constexpr( std::is_arithmetic_v< W > )
118 return( NetworKit::edgeweight( w ) );
119 else
120 return( NetworKit::defaultEdgeWeight );
121 }
122
125template< Graph G >
126[[nodiscard]] NetworKit::Graph toNetworKit( const G & g )
127{
128 using W = typename G::Weight;
129 constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
130 if constexpr( requires {
131 g.offsets();
132 g.arcHeads();
133 } ) {
134 // the rows of fromCSR(), of 64-bit indices, an unoriented edge once
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 ] ) );
146 }
147 off[ u + 1 ] = NetworKit::index( col.size() );
148 }
149 return( NetworKit::Graph::fromCSR( off , col , w , g.directed() ,
150 weighted ) );
151 }
152 else {
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 ) )
157 continue;
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 ) );
162 } );
163 else
164 g.forEachAdj( u , [ & ]( int v ) {
165 if( g.directed() || ( v > u ) )
166 N.addEdge( NetworKit::node( u ) , NetworKit::node( v ) );
167 } );
168 }
169 for( int u = 0 ; u < b ; ++u )
170 if( ! g.hasNode( u ) )
171 N.removeNode( NetworKit::node( u ) );
172 return( N );
173 }
174 }
175
176/*--------------------------------------------------------------------------*/
177/*------------------------- CLASS NetworKitMirror --------------------------*/
178/*--------------------------------------------------------------------------*/
179
183template< Graph G >
185{
186
187/*--------------------------------------------------------------------------*/
188 public:
189 using Weight = typename G::Weight;
191
193 explicit NetworKitMirror( const G & g ) : f_nk( toNetworKit( g ) ) { }
194
195 NetworKitMirror( const NetworKitMirror & ) = delete;
197
199 [[nodiscard]] const NetworKit::Graph & graph( void ) const {
200 return( f_nk );
201 }
202
205 int attach( NetworKit::DynAlgorithm & a ,
206 unsigned kinds = GraphEvents::EDGES ) {
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 };
210 return( int( q ) );
211 }
212 f_algo.push_back( Slot{ &a , kinds } );
213 return( int( f_algo.size() ) - 1 );
214 }
215
218 void detach( int id ) {
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;
222 }
223
225 [[nodiscard]] std::size_t told( void ) const { return( f_told ); }
226
228 void on( const NodeInserted & e ) { one( Event( e ) ); }
230 void on( const NodeDeleted & e ) noexcept { one( Event( e ) ); }
232 void on( const EdgeInserted< Weight > & e ) { one( Event( e ) ); }
234 void on( const EdgeDeleted< Weight > & e ) noexcept { one( Event( e ) ); }
236 void on( const WeightChanged< Weight > & e ) { one( Event( e ) ); }
237
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 ) );
247 K.push_back( kindOf< Weight >( e ) );
248 }
249 std::vector< NetworKit::GraphEvent > F;
250 for( const Slot & x : f_algo ) {
251 if( x.a == nullptr )
252 continue;
253 F.clear();
254 for( std::size_t q = 0 ; q < E.size() ; ++q )
255 if( ( K[ q ] & x.kinds ) != 0 )
256 F.push_back( E[ q ] );
257 if( ! F.empty() ) {
258 x.a->updateBatch( std::span< const NetworKit::GraphEvent >( F ) );
259 f_told += F.size();
260 }
261 }
262 }
263
264/*--------------------------------------------------------------------------*/
265 private:
267 struct Slot
268 {
269 NetworKit::DynAlgorithm * a;
270 unsigned kinds;
271 };
272
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 ) );
282 }
283 while( f_nk.upperNodeIdBound() <= u )
284 ( void )f_nk.addNode();
285 return( NE( NE::NODE_ADDITION , u ) );
286 }
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 ) ) );
290 }
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 ) );
295 }
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 ) ) );
299 }
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 ) );
304 }
305
307 void one( const Event & e ) {
308 const NetworKit::GraphEvent x = apply( e );
309 const unsigned k = kindOf< Weight >( e );
310 for( const Slot & s : f_algo )
311 if( ( s.a != nullptr ) && ( ( s.kinds & k ) != 0 ) ) {
312 s.a->update( x );
313 ++f_told;
314 }
315 }
316
317 NetworKit::Graph f_nk;
318 std::vector< Slot > f_algo;
319 std::size_t f_told = 0;
320
321 }; // end( class NetworKitMirror )
322
323/*--------------------------------------------------------------------------*/
324
325} // end( namespace ADS::networkit )
326
327/*--------------------------------------------------------------------------*/
328
329#endif /* NetworKit.h included */
330
331/*--------------------------------------------------------------------------*/
332/*-------------------------- End File NetworKit.h --------------------------*/
333/*--------------------------------------------------------------------------*/
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