Debian Codegraph @

avogadro/core/graph.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_GRAPH_H
7 #define AVOGADRO_CORE_GRAPH_H
8
9 #include "avogadrocoreexport.h"
10
11 #include "array.h"
12
13 #include <set>
14 #include <vector>
15
16 namespace Avogadro::Core {
17
18 /**
19 * @class Graph graph.h <avogadro/core/graph.h>
20 * @brief The Graph class represents a graph data structure.
21 *
22 * A graph consists of vertices and edges, wherein every edge connects two
23 * vertices. Each vertex is assigned an index, starting from 0 up to size() - 1.
24 * Each edge is also assigned an index, from 0 to edgeCount() - 1.
25 */
26 class AVOGADROCORE_EXPORT Graph
27 {
28 public:
29 /** Creates a new, empty graph. */
30 Graph() = default;
31
32 /** Creates a new graph containing size @p n vertices. */
33 explicit Graph(size_t n);
34
35 /** Destroys the graph. */
36 ~Graph() = default;
37
38 /**
39 * Sets the number of vertices in the graph to size @p n.
40 *
41 * If @p n is smaller than \c size(), this removes all vertices with index
42 * @p n or higher, as well as any edges connected to them, while preserving
43 * all other vertices and edges. These vertices keep their existing indices,
44 * while no guarantee is made regarding preserved edge indices.
45 *
46 * If @p n is larger than \c size(), a number of unconnected vertices are
47 * added, up to the requested size. All existing vertices and edges are
48 * preserved in their current form.
49 */
50 void setSize(size_t n);
51
52 /** @return the number of vertices in the graph. */
53 size_t size() const;
54
55 /** @return \c true if the graph is empty (i.e. size() == \c 0). */
56 bool isEmpty() const;
57
58 /** Removes all vertices and edges from the graph. */
59 void clear();
60
61 /**
62 * Adds a vertex to the graph and returns its index.
63 * The new vertex is initially not connected. All existing vertices and edges
64 * are preserved and their indices unchanged.
65 */
66 size_t addVertex();
67
68 /**
69 * Removes the vertex at @p index from the graph, as well as all edges to it.
70 * If @p index is not the highest vertex index in the graph, the vertex with
71 * highest index will be assigned the index of the removed vertex. All other
72 * vertices will keep their indices. No guarantees are made regarding edge
73 * indices.
74 */
75 void removeVertex(size_t index);
76
77 /**
78 * Swaps two vertices' indices, without affecting connectivity.
79 * All other vertices and all edges keep their existing indices.
80 */
81 void swapVertexIndices(size_t a, size_t b);
82
83 /** @return the number of vertices in the graph. */
84 size_t vertexCount() const;
85
86 /**
87 * Adds an edge between vertices @p a and @p b and returns its index.
88 * All existing vertices and edges are preserved unchanged.
89 */
90 size_t addEdge(size_t a, size_t b);
91
92 /**
93 * Removes the edge between vertices @p a and @p b.
94 * All vertices keep their indices. If the removed edge has an index lower
95 * than the highest edge index in the graph, the edge with the highest index
96 * is given the index of the removed edge. All other edges remain unchanged.
97 */
98 void removeEdge(size_t a, size_t b);
99
100 /**
101 * Removes edge with index @p edgeIndex.
102 * All vertices keep their indices. If @p edgeIndex is lower than the highest
103 * edge index in the graph, the edge with the highest index is given the index
104 * of the removed edge. All other edges remain unchanged.
105 */
106 void removeEdge(size_t edgeIndex);
107
108 /** Removes all of the edges from the graph, without affecting vertices. */
109 void removeEdges();
110
111 /**
112 * Removes all of the edges that contain the vertex at @p index from the
113 * graph.
114 */
115 void removeEdges(size_t index);
116
117 /**
118 * Removes the edge at @p edgeIndex, and creates a new one between vertices
119 * @p a and @p b, with the same index as the removed edge. All other edges and
120 * vertices keep their current indices.
121 */
122 void editEdgeInPlace(size_t edgeIndex, size_t a, size_t b);
123
124 /**
125 * Swaps two edges' indices, without affecting connectivity.
126 * All other edges and all vertices keep their current indices.
127 */
128 void swapEdgeIndices(size_t edgeIndex1, size_t edgeIndex2);
129
130 /** @return the number of edges in the graph. */
131 size_t edgeCount() const;
132
133 /**
134 * @return a vector containing the indices of each vertex that the vertex at
135 * index shares an edge with.
136 */
137 std::vector<size_t> neighbors(size_t index) const;
138
139 /**
140 * @return a vector containing the indices of each edge that the vertex at
141 * @p index is an endpoint of; that is, the edges incident at it.
142 */
143 std::vector<size_t> edges(size_t index) const;
144
145 /**
146 * @return the indices of the two vertices that the edge at @p index connects;
147 * that is, its endpoints.
148 */
149 std::pair<size_t, size_t> endpoints(size_t edgeIndex) const;
150
151 /** @return the degree of the vertex at @p index. */
152 size_t degree(size_t index) const;
153
154 /**
155 * @return \c true if the graph contains an edge between vertices @p a and
156 * @p b.
157 */
158 bool containsEdge(size_t a, size_t b) const;
159
160 /**
161 * @return an array with all edges, where every element contains the indices
162 * of both endpoints of the edge with index equal to the element's array
163 * index.
164 */
165 const Array<std::pair<size_t, size_t>>& edgePairs() const;
166
167 /**
168 * @return a vector of vector containing the indices of each vertex in each
169 * connected component in the graph.
170 */
171 std::vector<std::set<size_t>> connectedComponents() const;
172
173 /**
174 * @return a set containing the indices of each vertex connected with @p
175 * index.
176 */
177 std::set<size_t> connectedComponent(size_t index) const;
178
179 /** @return the number of connected subgraphs. */
180 size_t subgraphsCount() const;
181
182 /** @return the subgraph ID of the connected subgraph @p index lies in. */
183 size_t subgraph(size_t index) const;
184
185 /**
186 * @return the size of the connected subgraph that includes @p index,
187 * that is, the number of connected vertices in it.
188 */
189 size_t subgraphCount(size_t index) const;
190
191 /**
192 * @return the subgraph ID of the connected subgraph @p index lies in.
193 */
194 size_t getConnectedID(size_t index) const;
195
196 private:
197 std::set<size_t> checkConectivity(size_t a, size_t b) const;
198 std::vector<std::vector<size_t>> m_adjacencyList;
199 std::vector<std::vector<size_t>> m_edgeMap;
200 Array<std::pair<size_t, size_t>> m_edgePairs;
201
202 /** @return the (new or reused) index of a newly created empty subgraph. */
203 int createNewSubgraph() const;
204
205 /**
206 * If @p subgraph is marked as dirty, traverse it
207 * to check if it has split into many new subgraphs,
208 * and mark the resulting subgraph(s) as clean.
209 */
210 void checkSplitSubgraph(int subgraph) const;
211
212 /**
213 * Traverse and mark clean all dirty subgraphs,
214 * and create new subgraphs for all lone vertices.
215 * All subgraph data becomes synchronized as a result.
216 */
217 void updateSubgraphs() const;
218
219 mutable std::vector<int> m_vertexToSubgraph;
220 mutable std::vector<std::set<size_t>> m_subgraphToVertices;
221 mutable std::vector<bool> m_subgraphDirty;
222 mutable std::set<size_t> m_loneVertices;
223 };
224
225 } // namespace Avogadro::Core
226
227 #endif // AVOGADRO_CORE_GRAPH_H