ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ListGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File ListGraph.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
32/*--------------------------------------------------------------------------*/
33/*----------------------------- DEFINITIONS --------------------------------*/
34/*--------------------------------------------------------------------------*/
35
36#ifndef __ADS_ListGraph
37 #define __ADS_ListGraph
38/* self-identification: #endif constant */
39
40/*--------------------------------------------------------------------------*/
41/*------------------------------ INCLUDES ----------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#include "ADS/Contract.h"
45#include "ADS/Iterators.h"
46#include "ADS/graph/Graph.h"
47#include "ADS/sequence/List.h"
48
49#include <algorithm>
50#include <concepts>
51#include <optional>
52#include <ranges>
53#include <utility>
54#include <variant>
55#include <vector>
56
57/*--------------------------------------------------------------------------*/
58/*--------------------------- NAMESPACE ------------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61namespace ADS
62{
63
64/*--------------------------------------------------------------------------*/
65/*---------------------------- CLASS ListGraph -----------------------------*/
66/*--------------------------------------------------------------------------*/
67
69template< class W = std::monostate , class T = std::monostate >
70class ListGraph : public GraphNodes< T >
71{
72
73 using B = GraphNodes< T >;
74
75/*--------------------------------------------------------------------------*/
76 public:
77 using Weight = W;
78 using Label = T;
79
82 explicit ListGraph( int n = 0 , bool directed = true );
83
85 int insertNode( const T & t = T() );
86
108 void insertEdge( int u , int v , const W & w = W() );
109
113 void deleteEdge( int u , int v );
114
129 void deleteNode( int u );
130
132 [[nodiscard]] bool hasEdge( int u , int v ) const;
133
135 [[nodiscard]] std::optional< W > weight( int u , int v ) const;
136
139 [[nodiscard]] std::vector< int > adj( int u ) const;
140
158 [[nodiscard]] std::vector< int > inAdj( int u ) const;
159
162 template< class F >
163 void forEachAdj( int u , F && f ) const;
164
168 template< class F >
169 void forEachEdge( int u , F && f ) const;
170
171/*------------------------- THE STANDARD INTERFACE -------------------------*/
172
175 [[nodiscard]] auto neighbors( int u ) const {
176 using It =
178 const auto & L = f_adj[ this->check( u ) ];
179 return( std::ranges::subrange( It( L.begin() ) , It( L.end() ) ) );
180 }
181
182/*--------------------------------------------------------------------------*/
183 private:
184 bool find( int u , int v ) const;
185
186 bool set( int u , int v , const W & w );
187
188 bool erase( int u , int v );
189
192 [[nodiscard]] bool consistent( void ) const;
193
197 [[nodiscard]] bool consistentAt( int u , int v ) const;
198
199 std::vector< List< std::pair< int , W > > > f_adj;
200
201 }; // end( class ListGraph )
202
203/*--------------------------------------------------------------------------*/
204
205} // end( namespace ADS )
206
207/*--------------------------------------------------------------------------*/
208/*-------------------------- IMPLEMENTATION --------------------------------*/
209/*--------------------------------------------------------------------------*/
210
211#include "ADS/graph/impl/ListGraph.tpp"
212
213/*--------------------------------------------------------------------------*/
214
215#endif /* ListGraph.h included */
216
217/*--------------------------------------------------------------------------*/
218/*-------------------------- End File ListGraph.h --------------------------*/
219/*--------------------------------------------------------------------------*/
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...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
GraphNodes(int n, bool directed)
int n(void) const
Definition Graph.h:126
bool directed(void) const
Definition Graph.h:123
the member first of what the iterator I reads, with the category of I up to bidirectional
Definition Iterators.h:793
std::optional< W > weight(int u, int v) const
the weight of , nothing if there is no such edge
W Weight
the type of the weights
Definition ListGraph.h:77
void insertEdge(int u, int v, const W &w=W())
the edge , or , of weight ; only the weight changes if it is there, time ( if unoriented) for the s...
T Label
the type of the labels
Definition ListGraph.h:78
std::vector< int > inAdj(int u) const
the nodes with an edge to , in increasing order, by a scan of all the lists, time (the inEdges( i ) ...
void forEachAdj(int u, F &&f) const
on the nodes adjacent to
void deleteNode(int u)
removes the node and its edges, time
void forEachEdge(int u, F &&f) const
on the nodes adjacent to and the weights of the edges
bool hasEdge(int u, int v) const
whether is an edge
auto neighbors(int u) const
the nodes adjacent to , in the order of its list, bidirectional
Definition ListGraph.h:175
ListGraph(int n=0, bool directed=true)
the graph of the nodes , with no edge
void deleteEdge(int u, int v)
removes the edge , or , if it is there
std::vector< int > adj(int u) const
the set of the nodes adjacent to , in the order of insertion
int insertNode(const T &t=T())
a new node, with no edge, and its name
The GRAPH of chapter 9 and its realizations.
Definition ADT.h:96