|
ADS
Algorithms and Data Structures
|
a graph with lists of adjacency More...
#include <ADS/graph/ListGraph.h>
Public types | |
| using | Weight = W |
| the type of the weights | |
| using | Label = T |
| the type of the labels | |
Public member functions | |
| ListGraph (int n=0, bool directed=true) | |
| the graph of the nodes \( 0 , \ldots , n - 1 \), with no edge | |
| int | insertNode (const T &t=T()) |
| a new node, with no edge, and its name | |
| void | insertEdge (int u, int v, const W &w=W()) |
| the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \); only the weight changes if it is there, \( O( \deg( u ) ) \) time (\( O( \deg( u ) + \deg( v ) ) \) if unoriented) for the search of the edge | |
| void | deleteEdge (int u, int v) |
| removes the edge \( ( u , v ) \), or \( [ u , v ] \), if it is there | |
| void | deleteNode (int u) |
| removes the node \( u \) and its edges, \( O( n + m ) \) time | |
| bool | hasEdge (int u, int v) const |
| whether \( ( u , v ) \) is an edge | |
| std::optional< W > | weight (int u, int v) const |
| the weight of \( ( u , v ) \), nothing if there is no such edge | |
| std::vector< int > | adj (int u) const |
| the set of the nodes adjacent to \( u \), in the order of insertion | |
| std::vector< int > | inAdj (int u) const |
| the nodes with an edge to \( u \), in increasing order, by a scan of all the lists, \( O( n + m ) \) time (the inEdges( i ) of the AdjacencyLists of section 12.2 of Morin's book); the lists of an unoriented graph give them in \( O( \deg( u ) ) \) | |
| template<class F> | |
| void | forEachAdj (int u, F &&f) const |
| \( f( v ) \) on the nodes adjacent to \( u \) | |
| template<class F> | |
| void | forEachEdge (int u, F &&f) const |
| \( f( v , w ) \) on the nodes adjacent to \( u \) and the weights of the edges | |
| auto | neighbors (int u) const |
| the nodes adjacent to \( u \), in the order of its list, bidirectional | |
| Public member functions inherited from ADS::GraphNodes< std::monostate > | |
| bool | directed (void) const |
| whether the graph is oriented | |
| int | n (void) const |
| the number of nodes, the G.n of the book | |
| int | m (void) const |
| the number of edges, an unoriented one counted once | |
| int | bound (void) const |
| the names of the nodes are in \( [ 0 , \mathrm{bound}() ) \) | |
| bool | hasNode (int u) const |
| whether \( u \) is a node | |
| std::vector< int > | V (void) const |
| the set of the nodes, in increasing order | |
| const std::monostate & | label (int u) const |
| the label of the node \( u \) | |
| void | setLabel (int u, const std::monostate &t) |
| the label of the node \( u \) becomes \( t \) | |
| auto | nodes (void) const |
| the nodes in increasing order, bidirectional | |
Additional Inherited Members | |
| Protected member functions inherited from ADS::GraphNodes< std::monostate > | |
| GraphNodes (int n, bool directed) | |
| int | addNode (const std::monostate &t) |
| a new node of label \( t \), and its name | |
| int | check (int u) const |
| \( u \), which must be a node | |
| void | edge (int u, int v) const |
| \( u \) and \( v \) must be two distinct nodes | |
| bool | nodesCounted (void) const |
| n() counts the names in use, and each has a label | |
| Protected attributes inherited from ADS::GraphNodes< std::monostate > | |
| bool | f_directed |
| oriented or not | |
| std::vector< bool > | f_alive |
| whether a name is a node | |
| std::vector< std::monostate > | f_label |
| the labels | |
| int | f_n |
| the number of nodes | |
| int | f_m |
| the number of edges | |
a graph with lists of adjacency
| using ADS::ListGraph< W, T >::Label = T |
the type of the labels
| using ADS::ListGraph< W, T >::Weight = W |
the type of the weights
|
explicit |
the graph of the nodes \( 0 , \ldots , n - 1 \), with no edge
|
nodiscard |
the set of the nodes adjacent to \( u \), in the order of insertion
| void ADS::ListGraph< W, T >::deleteEdge | ( | int | u, |
| int | v ) |
removes the edge \( ( u , v ) \), or \( [ u , v ] \), if it is there
| void ADS::ListGraph< W, T >::deleteNode | ( | int | u | ) |
removes the node \( u \) and its edges, \( O( n + m ) \) time
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{deleteNode}( u ) \cr 2 & \quad \textbf{foreach}\ x \in V\ \textbf{do}\ \text{remove } u \text{ from the list of } x \cr 3 & \quad \text{empty the list of } u;\ \text{mark the name } u \text{ } \text{unused} \end{array}} \]
| void ADS::ListGraph< W, T >::forEachAdj | ( | int | u, |
| F && | f ) const |
\( f( v ) \) on the nodes adjacent to \( u \)
| void ADS::ListGraph< W, T >::forEachEdge | ( | int | u, |
| F && | f ) const |
\( f( v , w ) \) on the nodes adjacent to \( u \) and the weights of the edges
|
nodiscard |
whether \( ( u , v ) \) is an edge
|
nodiscard |
the nodes with an edge to \( u \), in increasing order, by a scan of all the lists, \( O( n + m ) \) time (the inEdges( i ) of the AdjacencyLists of section 12.2 of Morin's book); the lists of an unoriented graph give them in \( O( \deg( u ) ) \)
In pseudocode (section 12.2 of Morin's book), for an oriented graph:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inAdj}( u ) \cr 2 & \quad I \gets () \cr 3 & \quad \textbf{foreach}\ v \in V\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ x \in G.\mathrm{adj}( v )\ \textbf{with} \ x = u\ \textbf{do}\ I \gets I \cdot ( v ) \cr 5 & \quad \textbf{return}\ I \end{array}} \]
| void ADS::ListGraph< W, T >::insertEdge | ( | int | u, |
| int | v, | ||
| const W & | w = W() ) |
the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \); only the weight changes if it is there, \( O( \deg( u ) ) \) time (\( O( \deg( u ) + \deg( v ) ) \) if unoriented) for the search of the edge
In pseudocode (section 9.4), \( L_u \) the list of \( u \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertEdge}( u , v , w ) \cr 2 & \quad c \gets \text{the cell of } v \text{ in } L_u \text{, by a scan} \cr 3 & \quad \textbf{if}\ c \ne \textbf{nil}\ \textbf{then}\ \text{give } c \text{ the weight } w\quad \triangleright\ \text{and the cell of } u \text{ in } L_v \text{ if unoriented} \cr 4 & \quad \textbf{else}\ \text{append } ( v , w ) \text{ to } L_u \text{, and } ( u , w ) \text{ to } L_v \text{ if unoriented};\ m \gets m + 1 \end{array}} \]
| int ADS::ListGraph< W, T >::insertNode | ( | const T & | t = T() | ) |
a new node, with no edge, and its name
|
inlinenodiscard |
the nodes adjacent to \( u \), in the order of its list, bidirectional
|
nodiscard |
the weight of \( ( u , v ) \), nothing if there is no such edge