Class Avogadro::Core::Graph#

class Graph#

The Graph class represents a graph data structure.

A graph consists of vertices and edges, wherein every edge connects two vertices. Each vertex is assigned an index, starting from 0 up to size() - 1. Each edge is also assigned an index, from 0 to edgeCount() - 1.

Public Functions

Graph()#

Creates a new, empty graph.

explicit Graph(size_t n)#

Creates a new graph containing size n vertices.

~Graph()#

Destroys the graph.

void setSize(size_t n)#

Sets the number of vertices in the graph to size n.

If n is smaller than size(), this removes all vertices with index n or higher, as well as any edges connected to them, while preserving all other vertices and edges. These vertices keep their existing indices, while no guarantee is made regarding preserved edge indices.

If n is larger than size(), a number of unconnected vertices are added, up to the requested size. All existing vertices and edges are preserved in their current form.

size_t size() const#
Returns:

the number of vertices in the graph.

bool isEmpty() const#
Returns:

true if the graph is empty (i.e. size() == 0).

void clear()#

Removes all vertices and edges from the graph.

size_t addVertex()#

Adds a vertex to the graph and returns its index. The new vertex is initially not connected. All existing vertices and edges are preserved and their indices unchanged.

void removeVertex(size_t index)#

Removes the vertex at index from the graph, as well as all edges to it. If index is not the highest vertex index in the graph, the vertex with highest index will be assigned the index of the removed vertex. All other vertices will keep their indices. No guarantees are made regarding edge indices.

void swapVertexIndices(size_t a, size_t b)#

Swaps two vertices’ indices, without affecting connectivity. All other vertices and all edges keep their existing indices.

size_t vertexCount() const#
Returns:

the number of vertices in the graph.

size_t addEdge(size_t a, size_t b)#

Adds an edge between vertices a and b and returns its index. All existing vertices and edges are preserved unchanged.

void removeEdge(size_t a, size_t b)#

Removes the edge between vertices a and b. All vertices keep their indices. If the removed edge has an index lower than the highest edge index in the graph, the edge with the highest index is given the index of the removed edge. All other edges remain unchanged.

void removeEdge(size_t edgeIndex)#

Removes edge with index edgeIndex. All vertices keep their indices. If edgeIndex is lower than the highest edge index in the graph, the edge with the highest index is given the index of the removed edge. All other edges remain unchanged.

void removeEdges()#

Removes all of the edges from the graph, without affecting vertices.

void removeEdges(size_t index)#

Removes all of the edges that contain the vertex at index from the graph.

void editEdgeInPlace(size_t edgeIndex, size_t a, size_t b)#

Removes the edge at edgeIndex, and creates a new one between vertices a and b, with the same index as the removed edge. All other edges and vertices keep their current indices.

void swapEdgeIndices(size_t edgeIndex1, size_t edgeIndex2)#

Swaps two edges’ indices, without affecting connectivity. All other edges and all vertices keep their current indices.

size_t edgeCount() const#
Returns:

the number of edges in the graph.

std::vector<size_t> neighbors(size_t index) const#
Returns:

a vector containing the indices of each vertex that the vertex at index shares an edge with.

std::vector<size_t> edges(size_t index) const#
Returns:

a vector containing the indices of each edge that the vertex at index is an endpoint of; that is, the edges incident at it.

std::pair<size_t, size_t> endpoints(size_t edgeIndex) const#
Returns:

the indices of the two vertices that the edge at index connects; that is, its endpoints.

size_t degree(size_t index) const#
Returns:

the degree of the vertex at index.

bool containsEdge(size_t a, size_t b) const#
Returns:

true if the graph contains an edge between vertices a and b.

const Array<std::pair<size_t, size_t>> &edgePairs() const#
Returns:

an array with all edges, where every element contains the indices of both endpoints of the edge with index equal to the element’s array index.

std::vector<std::set<size_t>> connectedComponents() const#
Returns:

a vector of vector containing the indices of each vertex in each connected component in the graph.

std::set<size_t> connectedComponent(size_t index) const#
Returns:

a set containing the indices of each vertex connected with index.

size_t subgraphsCount() const#
Returns:

the number of connected subgraphs.

size_t subgraph(size_t index) const#
Returns:

the subgraph ID of the connected subgraph index lies in.

size_t subgraphCount(size_t index) const#
Returns:

the size of the connected subgraph that includes index, that is, the number of connected vertices in it.

size_t getConnectedID(size_t index) const#
Returns:

the subgraph ID of the connected subgraph index lies in.