Graph.hpp

A simple, fast class for managing vertices (nodes) and edges.

Note

Status: BETA

class Graph
#include <Graph.hpp>

A graph class that maintains a set of vertices (nodes) and edges (connecting pairs of nodes)

Subclassed by WeightedGraph

Public Functions

inline Graph(size_t num_nodes = 0)

Construct a new graph with the specified number of nodes.

Graph(const Graph&) = default

Copy constructor.

Graph(Graph&&) = default

Move constructor.

inline ~Graph()
Graph &operator=(const Graph&) = default

Copy operator.

Graph &operator=(Graph&&) = default

Move operator.

inline size_t GetSize() const

Get number of vertices in this graph.

inline size_t GetEdgeCount() const

Get the total number of edges in this graph.

inline Node GetNode(int i)
Returns:

the i th node in the graph

inline vector<Node> GetNodes()
Returns:

a vector of all nodes in the graph

inline void Resize(size_t new_size)

Change the number of vertices in this graph.

inline const BitVector &GetEdgeSet(size_t id) const

Get the set of nodes that a specified node is connected to.

inline size_t GetDegree(size_t id) const

Get the degree of a specified node. For directed graphs, this is the out-degree

inline size_t GetInDegree(size_t id) const

Get the in-degree (number of incoming edges) of the node

Parameters:

id – This should only be used for directed graphs (for undirected graphs, GetDegree() is equivalent and faster)

inline size_t GetMaskedDegree(size_t id, const BitVector &mask) const

Get how many of a set of nodes that a specified node is connected to.

inline void SetLabel(size_t id, std::string lab)

Set label of node.

Parameters:
  • id – to

  • lab

inline std::string GetLabel(size_t id)

Get label of node.

Parameters:

id

inline bool HasEdge(size_t from, size_t to) const

Determine if a specific edge is included in this graph.

inline void AddEdge(size_t from, size_t to)

Add a specified edge into this graph.

inline void RemoveEdge(size_t from, size_t to)

Remove a specified edge from this graph.

inline void SetEdge(size_t from, size_t to, bool val)

Set the status of a specified edge as to whether or not it should be in the graph.

inline bool HasEdgePair(size_t from, size_t to) const

Determine if edges exist in both directions between a pair of vertices.

inline void AddEdgePair(size_t from, size_t to)

Add a pair of edges between two vertices (in both directions)

inline void RemoveEdgePair(size_t from, size_t to)

Remove edges in both directions between a pair of vertices.

inline void SetEdgePairs(size_t from, size_t to, bool val)

Set the status as to whether a pair of edges (in both direction) exist.

inline void Merge(const Graph &in_graph)

Merge a second graph into this one.

inline void PrintSym(std::ostream &os = std::cout)

Print a symmetric graph to the provided output stream (defaulting to standard out)

inline void PrintDirected(std::ostream &os = std::cout)

Print a directed graph to the provided output stream (defaulting to standard out)

Protected Attributes

vector<Node> nodes

Set of vertices in this graph.

class WeightedGraph : public Graph
#include <Graph.hpp>

A graph class that maintains a set of vertices (nodes), edges (connecting pairs of nodes), and edge weights

Public Functions

inline WeightedGraph(size_t num_nodes = 0)

The weight of each edge in the graph.

WeightedGraph(const WeightedGraph&) = default

Copy constructor.

WeightedGraph(WeightedGraph&&) = default

Move constructor.

inline ~WeightedGraph()
WeightedGraph &operator=(const WeightedGraph&) = default

Copy operator.

WeightedGraph &operator=(WeightedGraph&&) = default

Move operator.

inline void Resize(size_t new_size)
inline double GetWeight(size_t from, size_t to) const

Determine weight of a specific edge in this graph.

inline void AddEdge(size_t from, size_t to, double weight)

When Adding an edge, must also provide a weight.

inline void AddEdgePair(size_t from, size_t to, double weight)

When Adding an edge pair, must also provide a weight.

inline void Merge(const WeightedGraph &in_graph)

Merge two WeightedGraphs into one.

inline void PrintSym(std::ostream &os = std::cout)

Print a symmetric graph to the provided output stream (defaulting to standard out)

inline void PrintDirected(std::ostream &os = std::cout)

Print a directed graph to the provided output stream (defaulting to standard out)

inline vector<vector<double>> GetWeights()
Returns:

the weights for all edges in the graph

Protected Attributes

vector<vector<double>> weights