![]() |
Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
|
A sparse implementation of graphs that achieves O(1) in most operations.

Classes | |
| class | elementa::adts::graphs::SparseGraph< Vertex, Edge > |
| A graph that stores all its info in a sparse manner. More... | |
| class elementa::adts::graphs::SparseGraph |
A graph that stores all its info in a sparse manner.
This type of graph stores only once each vertex and edge datum, but contains redundant information in edges and vertices to improve time efficiency in many operations. Therefore, it is more memory-efficient when the Vertex and Edge types are large.
In this kind of graph, all operations are O(1) except erasing a node, which is O(degree(node)) due to the need of deleting all edges involved in that node.
Copying an entire graph into another is O(V) + 3*O(E).
All iterators in this implementation of graphs are never invalidated except when they are deleted (not affecting any other one).
Definition at line 73 of file sparse_graphs.h.


Public Types | |
Basic types, consts, etc. | |
| using | BaseGraph = Graph< Vertex, Edge > |
Basic types, consts, etc. | |
| using | VertexType = Vertex |
| For making Vertex and Edge available to the public. More... | |
| using | EdgeType = Edge |
| using | viterator = elementa::base::MultIterator< Vertex > |
| Vertex iterator. ForwardLegacyIterator. More... | |
| using | eiterator = elementa::base::MultIterator< Edge > |
| Edge iterator. ForwardLegacyIterator. More... | |
Public Member Functions | |
Constructors | |
| SparseGraph (void) | |
| Default constructor. More... | |
| SparseGraph (const SparseGraph &oth) | |
| Copy constructor. More... | |
| SparseGraph & | operator= (const SparseGraph &oth) |
| Copy assignment. More... | |
| SparseGraph (SparseGraph &&oth) | |
| Move constructor. More... | |
| SparseGraph & | operator= (SparseGraph &&oth) |
| Move assignment. More... | |
| virtual | ~SparseGraph (void) |
| Destructor. More... | |
| SparseGraph * | clone (void) const |
| Derived classes must implement this method varying its return type. More... | |
| SparseGraph * | emptyClone (void) const override |
| Derived classes can override this to provide a clone that is "empty". More... | |
Consulting methods | |
| bool | empty (void) const |
| Must return true if the graph is empty. More... | |
| void | clear (void) |
| Must clear the content of the graph. More... | |
| size_vertex | size_vertices (void) const |
| Must return the number of vertices. More... | |
| size_edge | size_edges (void) const |
| Must return the number of edges. More... | |
| BaseGraph::viterator | origin (const typename BaseGraph::eiterator &it) const |
| BaseGraph::viterator | destination (const typename BaseGraph::eiterator &it) const |
Modification methods | |
| BaseGraph::viterator | insert (const Vertex &v) |
| Must insert one more vertex in the graph, unconnected to any other. More... | |
| BaseGraph::eiterator | connect (const typename BaseGraph::viterator &itv0, const typename BaseGraph::viterator &itv1, const Edge &e) |
| void | unconnect (const typename BaseGraph::eiterator &ite) |
| void | unconnect (const typename BaseGraph::VectorOfEIts &sinds) |
| void | erase (const typename BaseGraph::viterator &itv) |
| void | erase (const typename BaseGraph::VectorOfVIts &sinds) |
| void | splice (BaseGraph &oth, typename BaseGraph::VectorOfVIts *svothinds=nullptr, typename BaseGraph::VectorOfEIts *seothinds=nullptr, typename BaseGraph::VectorOfVIts *svinds=nullptr, typename BaseGraph::VectorOfEIts *seinds=nullptr) |
Consulting methods | |
| bool | isLeaf (const viterator &it) const noexcept |
| Return whether the given vertex iterator points to a leaf vertex. More... | |
| bool | toLeaf (const eiterator &it) const noexcept |
| Return whether the given edge iterator points to an edge ending in leaf. More... | |
| bool | toLastChild (const eiterator &it, const viterator &vit) const noexcept |
| Return whether the given edge iterator points to the last child of VIT. More... | |
| virtual viterator | origin (const eiterator &it) const =0 |
| Must return a VRange_All iterator to the origin vertex of an edge. More... | |
| virtual viterator | destination (const eiterator &it) const =0 |
| Must return a VRange_All iterator to the destination vertex of an edge. More... | |
| virtual void | connection (const eiterator &it, viterator &vorg, viterator &vdest) const |
| Fill VORG and VDEST with VRange_All iterators to the edge vertices. More... | |
| virtual eiterator | connected (const viterator &v0, const viterator &v1, const EPred &epred=trivial_epred) const |
| Return the first edge that connects V0 to V1 or erangeLeaving().end(). More... | |
| virtual viterator | find_vertex (const Vertex &v) const |
| Look for the first vertex that contains the given data. More... | |
| virtual eiterator | find_edge (const Edge &e) const |
| Look for the first edge that contains the given data. More... | |
Numerical indexing methods | |
| virtual size_t | vertexToPos (const viterator &vit) const |
| Calculate a position of a single vertex from its iterator, from 0 to V-1. More... | |
| virtual viterator | posToVertex (size_t vpos) const |
| Return the iterator correponding to the given position (from 0 to V-1). More... | |
| virtual size_t | edgeToPos (const eiterator &eit) const |
| Calculate a position of a single edge from its iterator, from 0 to E-1. More... | |
| virtual eiterator | posToEdge (size_t epos) const |
| Return the iterator correponding to the given position (from 0 to E-1). More... | |
Modification methods | |
| virtual eiterator | connect (const viterator &itv0, const viterator &itv1, const Edge &e)=0 |
| Must add a new edge in the graph connecting two existing vertices. More... | |
| virtual void | unconnect (const eiterator &ite)=0 |
| Must delete the given edge. More... | |
| virtual void | unconnect (const VectorOfEIts &sinds)=0 |
| Must delete all edges indicated in SINDS (they must not be repeated) More... | |
| virtual void | erase (const viterator &itv)=0 |
| Must delete the given vertex and all edges connected to it. More... | |
| virtual void | erase (const VectorOfVIts &sinds)=0 |
| Must delete all verts indicated in SINDS (they must not be repeated) More... | |
| virtual void | splice (Graph &oth, VectorOfVIts *svothits=nullptr, VectorOfEIts *seothits=nullptr, VectorOfVIts *svits=nullptr, VectorOfEIts *seits=nullptr)=0 |
| Must add another graph to the one existing in this object. More... | |
Iteration methods | |
| VRange_All | vrangeAll (void) const |
| Return a range to iterate on all Vertex data, no special order. More... | |
| VRange_Pred | vrangePred (eiterator &eit, typename VRange_Pred::Pred &pred) const |
| Return a range for iterating on a predicate of the vertices. More... | |
| ERange_All | erangeAll (void) const |
| Return a range to iterate on all Edge data, no special order. More... | |
| ERange_Pred | erangePred (eiterator &eit, const EPred &pred) const |
| Return a range for iterating on a predicate of the edges. More... | |
| ERange_Leaving | erangeLeaving (const viterator &v) const |
| Return a range to iterate on edges leaving a vertex, no special order. More... | |
| ERange_Entering | erangeEntering (const viterator &v) const |
| Return a range to iterate on edges entering a vertex, no special order. More... | |
| ERange_DepthFirst | erangeDepthFirst (const viterator &v) const |
| Return a range to iterate on edges in depth-first fashion. More... | |
| ERange_BreadthFirst | erangeBreadthFirst (const viterator &v) const |
| Return a range to iterate on edges in breadth-first fashion. More... | |
Protected Member Functions | |
| virtual eiterator | eleavingbegin (const viterator &v) const =0 |
| Must return the begin() iterator for ERange_Leaving. | |
| virtual eiterator | eleavingend (const viterator &v) const =0 |
| Must return the end() iterator for ERange_Leaving. | |
| virtual eiterator | eenteringbegin (const viterator &v) const =0 |
| Must return the begin() iterator for ERange_Entering. | |
| virtual eiterator | eenteringend (const viterator &v) const =0 |
| Must return the end() iterator for ERange_Entering. | |
Iterator ranges and therefore iterator behaviours | |
| using | EPred = std::function< bool(const Edge &) > |
| Predicate that indicates which edges to iterate on. More... | |
| static constexpr bool | trivial_epred (const Edge &) noexcept |
| A trivial predicate that always return true. More... | |
| static constexpr bool | nil_epred (const Edge &) noexcept |
| A trivial predicate that always return false. More... | |
| using elementa::adts::graphs::SparseGraph< Vertex, Edge >::BaseGraph = Graph<Vertex,Edge> |
Definition at line 80 of file sparse_graphs.h.
|
inherited |
|
inherited |
|
inherited |
|
inherited |
|
inherited |
|
inline |
Default constructor.
Definition at line 89 of file sparse_graphs.h.
|
inline |
Copy constructor.
Definition at line 92 of file sparse_graphs.h.
|
inline |
Move constructor.
Definition at line 102 of file sparse_graphs.h.
|
inlinevirtual |
Destructor.
Definition at line 112 of file sparse_graphs.h.
|
inline |
Copy assignment.
Definition at line 97 of file sparse_graphs.h.
References elementa::adts::graphs::Graph< Vertex, Edge >::operator=().
|
inline |
Move assignment.
Definition at line 107 of file sparse_graphs.h.
References elementa::adts::graphs::Graph< Vertex, Edge >::operator=().
|
inlinevirtual |
Derived classes must implement this method varying its return type.
Implements elementa::patterns::Clonable.
Definition at line 114 of file sparse_graphs.h.
|
inlineoverridevirtual |
Derived classes can override this to provide a clone that is "empty".
"Emptiness" here means that the clone is not actually a clone of the derived object, but a new object of the same class of the derived object, without no content (i.e., right after creation).
Reimplemented from elementa::patterns::Clonable.
Definition at line 117 of file sparse_graphs.h.
|
inlinevirtual |
Must return true if the graph is empty.
Implements elementa::adts::graphs::Graph< Vertex, Edge >.
Definition at line 126 of file sparse_graphs.h.
|
inlinevirtual |
Must clear the content of the graph.
Implements elementa::adts::graphs::Graph< Vertex, Edge >.
Definition at line 129 of file sparse_graphs.h.
|
inlinevirtual |
Must return the number of vertices.
Implements elementa::adts::graphs::Graph< Vertex, Edge >.
Definition at line 132 of file sparse_graphs.h.
|
inlinevirtual |
Must return the number of edges.
Implements elementa::adts::graphs::Graph< Vertex, Edge >.
Definition at line 134 of file sparse_graphs.h.
|
inline |
Definition at line 136 of file sparse_graphs.h.
|
inline |
Definition at line 141 of file sparse_graphs.h.
|
inlinevirtual |
Must insert one more vertex in the graph, unconnected to any other.
Copy V inside the graph and return a VRange_All iterator to it.
Inserting a vertex could invalidate ALL existing vertex iterators and also existing edge iterators, depending on the implementation of the graph.
Implements elementa::adts::graphs::Graph< Vertex, Edge >.
Definition at line 152 of file sparse_graphs.h.
|
inline |
Definition at line 155 of file sparse_graphs.h.
|
inline |
Definition at line 164 of file sparse_graphs.h.
|
inline |
Definition at line 168 of file sparse_graphs.h.
|
inline |
Definition at line 171 of file sparse_graphs.h.
|
inline |
Definition at line 175 of file sparse_graphs.h.
|
inline |
There is no need to pass more than one argument, since iterators are not invalidated in this kind of graph
Definition at line 180 of file sparse_graphs.h.
|
inlinestaticconstexprnoexceptinherited |
|
inlinestaticconstexprnoexceptinherited |
A trivial predicate that always return false.
Definition at line 285 of file graphs.h.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Pred::end().
|
inlinenoexceptinherited |
Return whether the given vertex iterator points to a leaf vertex.
Definition at line 553 of file graphs.h.
References elementa::adts::graphs::Graph< Vertex, Edge >::erangeLeaving().
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::toLeaf().
|
inlinenoexceptinherited |
Return whether the given edge iterator points to an edge ending in leaf.
Definition at line 557 of file graphs.h.
References elementa::adts::graphs::Graph< Vertex, Edge >::destination(), and elementa::adts::graphs::Graph< Vertex, Edge >::isLeaf().
|
pure virtualinherited |
Must return a VRange_All iterator to the origin vertex of an edge.
If the edge is invalid or does not point anywhere, must throw.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::connection(), and elementa::adts::Tree< Vertex, Edge >::depth().
|
pure virtualinherited |
Must return a VRange_All iterator to the destination vertex of an edge.
If the edge is invalid or does not point anywhere, must throw.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::connected(), elementa::adts::graphs::Graph< Vertex, Edge >::connection(), and elementa::adts::graphs::Graph< Vertex, Edge >::toLeaf().
|
inlinevirtualinherited |
Fill VORG and VDEST with VRange_All iterators to the edge vertices.
This trivial implementation can be improved in derived classes.
Definition at line 574 of file graphs.h.
References elementa::adts::graphs::Graph< Vertex, Edge >::destination(), and elementa::adts::graphs::Graph< Vertex, Edge >::origin().
Referenced by elementa::adts::graphs::FloydWarshall< Vertex, Edge, Weight >::FloydWarshall(), elementa::adts::Tree< Vertex, Edge >::eraseNode(), elementa::adts::graphs::Szer_GraphML< Vertex, Edge >::ser(), and elementa::adts::graphs::Szer_GraphCompactText< Vertex, Edge >::ser().
|
pure virtualinherited |
Must add a new edge in the graph connecting two existing vertices.
Copy E inside the graph and return an ERange_All iterator to it.
Connecting vertices must not invalidate existing vertex iterators, but may invalidate ALL existing edge iterators (depending on the graph implementation).
Referenced by elementa::adts::Tree< Vertex, Edge >::addChild(), and elementa::adts::Tree< Vertex, Edge >::splice().
|
pure virtualinherited |
Must delete the given edge.
Unconnecting an edge invalidates that iterator and may invalidate ALL existing edge iterators (depending on the implementation), but must not invalidate existing vertex iterators.
|
pure virtualinherited |
Must delete all edges indicated in SINDS (they must not be repeated)
If some of the iterators is invalid or do not point to an edge, throw. If throw, the status of the graph is undefined but it can be safely cleared.
This method is needed for unconnecting sets of edges while making sure that the successive invalidations of the iterators pointing to those edges do not affect the whole unconnecting operation.
After this operation, vertex iterators already existing must not be invalidated, but ALL edge iterators may be (depending on the implementation) and all iterators used for the indicators in SINDS certainly are invalidated.
|
pure virtualinherited |
Must delete the given vertex and all edges connected to it.
Deleting a vertex invalidates this vertex iterator and (depending on the implementation of the graph) also existing vertex and edge iterators.
Referenced by elementa::adts::Tree< Vertex, Edge >::eraseNode().
|
pure virtualinherited |
Must delete all verts indicated in SINDS (they must not be repeated)
If some of the iterators is invalid or do not point to a vertex, throw. If throw, the status of the graph is undefined but it can be safely cleared.
This method is needed for erasing sets of vertices while making sure that the successive invalidations of the iterators pointing to those vertices do not affect the whole erasing operation.
After this operation, vertex and iterators already existing may be invalidated (depending on the implementation), and all iterators used for the indicators in SINDS certainly are invalidate.
|
pure virtualinherited |
Must add another graph to the one existing in this object.
By default, the intended behaviour is to move the graph in OTH (leaving it empty) to this one, but depending on the implementation the move may be a copy (although OTH will always be empty after the operation). Vertices and edges of OTH will not clash with the vertices and edges in this graph, but considered as new and unconnected to vertices of this graph. If SVOTHITS/SEOTHITS/SVITS/SEITS are not empty, they must have iterators in OTH and this, respectively, and after the merging will be changed by new iterators pointing to those same vertices and edges once they are moved to this graph, maybe in another order with respect to the original vectors.
Referenced by elementa::adts::Tree< Vertex, Edge >::splice().
|
inlineinherited |
Return a range to iterate on all Vertex data, no special order.
Definition at line 721 of file graphs.h.
Referenced by elementa::adts::graphs::FloydWarshall< Vertex, Edge, Weight >::FloydWarshall(), elementa::adts::graphs::Graph< Vertex, Edge >::MapOfVItPos::MapOfVItPos(), elementa::adts::graphs::Graph< Vertex, Edge >::VectorOfVIts::VectorOfVIts(), elementa::adts::graphs::Graph< Vertex, Edge >::find_vertex(), elementa::adts::Tree< Vertex, Edge >::root(), elementa::adts::graphs::Szer_GraphML< Vertex, Edge >::ser(), elementa::adts::graphs::Szer_GraphCompactText< Vertex, Edge >::ser(), and elementa::adts::Tree< Vertex, Edge >::setRoot().
|
inlineinherited |
|
inlineinherited |
Return a range to iterate on all Edge data, no special order.
Definition at line 730 of file graphs.h.
Referenced by elementa::adts::graphs::FloydWarshall< Vertex, Edge, Weight >::FloydWarshall(), elementa::adts::graphs::Graph< Vertex, Edge >::MapOfEItPos::MapOfEItPos(), elementa::adts::graphs::Graph< Vertex, Edge >::VectorOfEIts::VectorOfEIts(), elementa::adts::graphs::Graph< Vertex, Edge >::find_edge(), elementa::adts::graphs::Szer_GraphML< Vertex, Edge >::ser(), and elementa::adts::graphs::Szer_GraphCompactText< Vertex, Edge >::ser().
|
inlineinherited |
|
inlineinherited |
Return a range to iterate on edges leaving a vertex, no special order.
Definition at line 738 of file graphs.h.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::connected(), elementa::adts::graphs::Graph< Vertex, Edge >::isLeaf(), and elementa::adts::graphs::Graph< Vertex, Edge >::toLastChild().
|
inlineinherited |
Return a range to iterate on edges entering a vertex, no special order.
Definition at line 742 of file graphs.h.
Referenced by elementa::adts::Tree< Vertex, Edge >::depth().
|
inlineinherited |
Return a range to iterate on edges in depth-first fashion.
Definition at line 746 of file graphs.h.
Referenced by elementa::adts::graphs::AllPathsVertices< Vertex, Edge, KIND >::AllPathsVertices(), and elementa::adts::Tree< Vertex, Edge >::eraseNode().
|
inlineinherited |