ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MatrixGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MatrixGraph.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
30/*--------------------------------------------------------------------------*/
31/*----------------------------- DEFINITIONS --------------------------------*/
32/*--------------------------------------------------------------------------*/
33
34#ifndef __ADS_MatrixGraph
35 #define __ADS_MatrixGraph
36/* self-identification: #endif constant */
37
38/*--------------------------------------------------------------------------*/
39/*------------------------------ INCLUDES ----------------------------------*/
40/*--------------------------------------------------------------------------*/
41
42#include "ADS/Contract.h"
43#include "ADS/Iterators.h"
44#include "ADS/graph/Graph.h"
45
46#include <cstddef>
47#include <optional>
48#include <ranges>
49#include <variant>
50#include <vector>
51
52/*--------------------------------------------------------------------------*/
53/*--------------------------- NAMESPACE ------------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56namespace ADS
57{
58
59/*--------------------------------------------------------------------------*/
60/*--------------------------- CLASS MatrixGraph ----------------------------*/
61/*--------------------------------------------------------------------------*/
62
64template< class W = std::monostate , class T = std::monostate >
65class MatrixGraph : public GraphNodes< T >
66{
67
68 using B = GraphNodes< T >;
69
70/*--------------------------------------------------------------------------*/
71 public:
72 using Weight = W;
73 using Label = T;
74
77 explicit MatrixGraph( int n = 0 , bool directed = true );
78
80 int insertNode( const T & t = T() );
81
98 void insertEdge( int u , int v , const W & w = W() );
99
102 void deleteEdge( int u , int v );
103
107 void deleteNode( int u );
108
110 [[nodiscard]] bool hasEdge( int u , int v ) const;
111
114 [[nodiscard]] std::optional< W > weight( int u , int v ) const;
115
119 [[nodiscard]] std::vector< int > adj( int u ) const;
120
138 [[nodiscard]] std::vector< int > inAdj( int u ) const;
139
141 template< class F >
142 void forEachAdj( int u , F && f ) const;
143
145 template< class F >
146 void forEachEdge( int u , F && f ) const;
147
148/*------------------------- THE STANDARD INTERFACE -------------------------*/
149
151 struct Present
152 {
154 static std::size_t size( const auto & r ) { return( r.size() ); }
156 static bool occupied( const auto & r , std::size_t i ) {
157 return( r[ i ].has_value() );
158 }
159
160 static int read( const auto & , std::size_t i ) { return( int( i ) ); }
161 };
162
165 [[nodiscard]] auto neighbors( int u ) const {
167 const auto & r = f_M[ this->check( u ) ];
168 return( std::ranges::subrange( It( &r , 0 ) , It( &r , r.size() ) ) );
169 }
170
171/*--------------------------------------------------------------------------*/
172 private:
176 [[nodiscard]] bool consistent( void ) const;
177
180 [[nodiscard]] bool consistentAt( int u ) const;
181
182 std::vector< std::vector< std::optional< W > > > f_M;
183
184 }; // end( class MatrixGraph )
185
186/*--------------------------------------------------------------------------*/
187
188} // end( namespace ADS )
189
190/*--------------------------------------------------------------------------*/
191/*-------------------------- IMPLEMENTATION --------------------------------*/
192/*--------------------------------------------------------------------------*/
193
194#include "ADS/graph/impl/MatrixGraph.tpp"
195
196/*--------------------------------------------------------------------------*/
197
198#endif /* MatrixGraph.h included */
199
200/*--------------------------------------------------------------------------*/
201/*------------------------- End File MatrixGraph.h -------------------------*/
202/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
GraphNodes(int n, bool directed)
int n(void) const
Definition Graph.h:126
bool directed(void) const
Definition Graph.h:123
void forEachAdj(int u, F &&f) const
void forEachEdge(int u, F &&f) const
std::optional< W > weight(int u, int v) const
the weight of , nothing (the infinity of the book) if there is no such edge
std::vector< int > adj(int u) const
the set of the nodes adjacent to , in increasing order, time
T Label
the type of the labels
Definition MatrixGraph.h:73
bool hasEdge(int u, int v) const
time
std::vector< int > inAdj(int u) const
the nodes with an edge to , in increasing order, the column of , time (the inEdges( i ) of the Adjac...
void insertEdge(int u, int v, const W &w=W())
the edge , or , of weight , time; only the weight changes if it is there
MatrixGraph(int n=0, bool directed=true)
the graph of the nodes , with no edge
W Weight
the type of the weights
Definition MatrixGraph.h:72
void deleteEdge(int u, int v)
void deleteNode(int u)
removes the node and its edges, time
auto neighbors(int u) const
the nodes adjacent to in increasing order, bidirectional
Definition MatrixGraph.h:165
int insertNode(const T &t=T())
a new node, a new row and a new column, time
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
The GRAPH of chapter 9 and its realizations.
Definition ADT.h:96
the columns of the edges in a row, for SlotIterator
Definition MatrixGraph.h:152
static int read(const auto &, std::size_t i)
the node of the column
Definition MatrixGraph.h:160
static bool occupied(const auto &r, std::size_t i)
whether the column holds an edge
Definition MatrixGraph.h:156
static std::size_t size(const auto &r)
the number of the columns
Definition MatrixGraph.h:154