Abstract base class for a generic graph structure. More...
#include <abstract_graph.h>
Public Types | |
| using | data_type = D |
| Alias for the data type of nodes. | |
| using | id_type = I |
| Alias for the node ID type. | |
| using | edge_type = E |
| Alias for the edge type. | |
Public Member Functions | |
| virtual | ~Graph ()=default |
| Virtual destructor for safe polymorphic use. | |
| virtual bool | is_directed () const =0 |
| Check if the graph is directed. | |
| virtual bool | is_weighted () const =0 |
| Check if the graph edges are weighted. | |
| constexpr bool | operator== (const Graph &other) const |
| Equality comparison between graphs. | |
| bool | is_full () |
| Checks if the graph has reached the maximum number of nodes. | |
| bool | node_exists (const id_type id) const |
| Check if a node with the specified ID exists. | |
| id_type | node_count () const |
| Get the number of nodes in the graph. | |
| size_t | edge_count () const |
| Get the number of edges in the graph. | |
| std::unordered_set< id_type > | get_nodes () const |
| Retrieve the set of all node IDs in the graph. | |
| const data_type & | get_node_data (const id_type id) const |
| Access the data associated with a node by its ID. | |
| const std::set< id_type > | get_neighbors (const id_type id) const |
| Get neighbors (adjacent node IDs) of a given node. | |
| bool | has_edge (const id_type from_id, const id_type to_id) const |
| Check if an edge exists between two nodes. | |
| edge_type | get_edge (const id_type from_id, const id_type to_id) const |
| Retrieve the edge information between two nodes. | |
| std::set< edge_type > | get_edges (const id_type from_id) const |
| Get all edges originating from a given node. | |
| void | print_data_map () const |
| Prints the contents of the data_map_ to standard output. | |
| void | print_adjacency_list () const |
| Outputs the graph as an adjacency list to the given stream. | |
| void | print_with_data (std::ostream &os) const |
| Outputs the graph as an adjacency list to the given stream but with its data. | |
| void | print_adjacency_matrix () const |
| Prints the adjacency matrix of the graph. | |
| id_type | add_node (const data_type data) |
| Add a new node with the given data. | |
| id_type | add_node () |
| Add a new node with default data. | |
| void | add_node_with_id (const id_type id, const data_type data) |
| Insert a new node with a specified ID and associated data. | |
| void | add_node_with_id (const id_type id) |
| Insert a new node with a specified ID. | |
| void | remove_node (const id_type id) |
| Remove a node by ID. | |
| void | set_node_data (const id_type id, const data_type data) |
| Update the data of a node. | |
| virtual void | add_edge (const id_type from_id, const id_type to_id)=0 |
| Add an edge from one node to another. | |
| virtual void | remove_edge (const id_type from_id, const id_type to_id)=0 |
| Remove an edge from one node to another. | |
Protected Member Functions | |
| virtual void | add_edge_impl (const id_type from_id, const edge_type edge) |
| Helper to add an edge including reverse edge if undirected. | |
Protected Attributes | |
| AdjacencyList< id_type, edge_type > | adj_list_ {} |
| Stores edges as adjacency list. | |
| std::unordered_map< id_type, data_type > | data_map_ {} |
| Maps node IDs to node data. | |
| id_type | highest_id_ {} |
| IDs start at 1 to avoid off-by-one errors when giving node count. | |
Private Attributes | |
| std::queue< id_type > | reusable_ids_ {} |
| Queue of reusable node IDs freed from removed nodes. | |
Friends | |
| std::ostream & | operator (std::ostream &os, const Graph< data_type, id_type, edge_type > &graph) |
| friend for outputting to a std::ostream. | |
Abstract base class for a generic graph structure.
Supports directed or undirected graphs and weighted or unweighted edges. Manages nodes identified by IDs, associated with data of type D. Edges are represented by type E and stored in an adjacency list.
Provides functionality for node and edge insertion, deletion, querying, and basic graph inspection.
| D | Data type stored within each node. |
| I | Type used for node IDs. |
| E | Edge type stored in adjacency list; must be compatible with node IDs. |
Definition at line 57 of file abstract_graph.h.
| using GraphLib::Internal::Graph< D, I, E >::data_type = D |
Alias for the data type of nodes.
Definition at line 59 of file abstract_graph.h.
| using GraphLib::Internal::Graph< D, I, E >::edge_type = E |
Alias for the edge type.
Definition at line 61 of file abstract_graph.h.
| using GraphLib::Internal::Graph< D, I, E >::id_type = I |
Alias for the node ID type.
Definition at line 60 of file abstract_graph.h.
|
pure virtual |
Add an edge from one node to another.
This is a pure virtual function that must be implemented by derived classes. It adds an edge from the node with ID from_id to the node with ID to_id.
| from_id | The ID of the origin node. |
| to_id | The ID of the target node. |
Implemented in GraphLib::Internal::UnweightedGraph< D, I, E >, GraphLib::Internal::UnweightedGraph< int, std::size_t, Internal::Edge< std::size_t > >, GraphLib::Internal::WeightedGraph< D, I, E >, and GraphLib::Internal::WeightedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >.
|
inlineprotectedvirtual |
Helper to add an edge including reverse edge if undirected.
Adds an edge from from_id to edge.node_id. If the graph is undirected, adds the reverse edge automatically.
| from_id | The origin node ID. |
| edge | The edge data including target node ID and weight. |
| std::invalid_argument | if nodes do not exist or edge already exists. |
Definition at line 483 of file abstract_graph.h.
|
inline |
Add a new node with default data.
For more Information see add_node(const data_type data), as this Function is only an overload.
Definition at line 365 of file abstract_graph.h.
|
inline |
Add a new node with the given data.
Assigns a new unique ID to the node and adds it to the graph. Throws if the graph is full.
| data | The data to store in the new node. |
| std::overflow_error | if graph is full. |
| std::runtime_error | if insertion fails. |
Definition at line 326 of file abstract_graph.h.
|
inline |
Insert a new node with a specified ID.
For more Information see add_node_with_id(const id_type id, const data_type data), as this function is just an overload.
Definition at line 401 of file abstract_graph.h.
|
inline |
Insert a new node with a specified ID and associated data.
This method explicitly inserts a node using the provided ID. It throws an exception if the graph is full or if the ID already exists.
| id | The node ID to assign. |
| data | The data to associate with the new node. |
| std::overflow_error | if the graph is full. |
| std::runtime_error | if the given ID already exists or insertion fails. |
Definition at line 381 of file abstract_graph.h.
|
inline |
Get the number of edges in the graph.
divides number of edges by 2 in undirected graphs to account for n1->n2 and n2->1 being essentially a single edge
Definition at line 134 of file abstract_graph.h.
|
inline |
Retrieve the edge information between two nodes.
Throws if the edge does not exist.
| from_id | Origin node ID. |
| to_id | Target node ID. |
| std::invalid_argument | if edge does not exist. |
Definition at line 201 of file abstract_graph.h.
|
inline |
Get all edges originating from a given node.
| from_id | The origin node ID. |
Definition at line 215 of file abstract_graph.h.
|
inline |
Get neighbors (adjacent node IDs) of a given node.
| id | The node ID. |
Definition at line 174 of file abstract_graph.h.
|
inline |
Access the data associated with a node by its ID.
Throws if the node ID does not exist.
| id | The node ID. |
| std::invalid_argument | if node does not exist. |
Definition at line 160 of file abstract_graph.h.
|
inline |
Retrieve the set of all node IDs in the graph.
Definition at line 146 of file abstract_graph.h.
|
inline |
Check if an edge exists between two nodes.
| from_id | Origin node ID. |
| to_id | Target node ID. |
Definition at line 186 of file abstract_graph.h.
|
nodiscardpure virtual |
Check if the graph is directed.
Implemented in GraphLib::Internal::DirectedGraph< D, I, E >, GraphLib::Internal::DirectedGraph< int, std::size_t, Internal::Edge< std::size_t > >, GraphLib::Internal::DirectedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >, GraphLib::Internal::UndirectedGraph< D, I, E >, GraphLib::Internal::UndirectedGraph< int, std::size_t, Internal::Edge< std::size_t > >, and GraphLib::Internal::UndirectedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >.
|
inline |
Checks if the graph has reached the maximum number of nodes.
This depends on the maximum value representable by the node ID type.
Definition at line 101 of file abstract_graph.h.
|
nodiscardpure virtual |
Check if the graph edges are weighted.
Implemented in GraphLib::Internal::UnweightedGraph< D, I, E >, GraphLib::Internal::UnweightedGraph< int, std::size_t, Internal::Edge< std::size_t > >, GraphLib::Internal::WeightedGraph< D, I, E >, and GraphLib::Internal::WeightedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >.
|
inline |
Get the number of nodes in the graph.
Definition at line 121 of file abstract_graph.h.
|
inline |
Check if a node with the specified ID exists.
| id | The ID of the node to check. |
Definition at line 112 of file abstract_graph.h.
|
inlineconstexpr |
Equality comparison between graphs.
Checks if both node data and adjacency lists are identical.
| other | Another graph instance to compare to. |
Definition at line 89 of file abstract_graph.h.
|
inline |
Outputs the graph as an adjacency list to the given stream.
Internally, this simply does std::cout << *this.
Definition at line 239 of file abstract_graph.h.
|
inline |
Prints the adjacency matrix of the graph.
Prints node IDs as headers and rows, and shows 0 for no edge, 1 for unweighted edges, or the weight for weighted edges.
Definition at line 266 of file abstract_graph.h.
|
inline |
Prints the contents of the data_map_ to standard output.
Outputs each key-value pair on its own line, followed by a separator.
Definition at line 225 of file abstract_graph.h.
|
inline |
Outputs the graph as an adjacency list to the given stream but with its data.
Instead of printing the Node IDs the values behind the Nodes gets streamed.
| Throws | if the Data is not streamable (printable). |
| os | Output stream to write the graph visualization. |
Definition at line 252 of file abstract_graph.h.
|
pure virtual |
Remove an edge from one node to another.
This is a pure virtual function that must be implemented by derived classes. It removes the edge from the node with ID from_id to the node with ID to_id.
| from_id | The ID of the origin node. |
| to_id | The ID of the target node. |
Implemented in GraphLib::Internal::DirectedGraph< D, I, E >, GraphLib::Internal::DirectedGraph< int, std::size_t, Internal::Edge< std::size_t > >, GraphLib::Internal::DirectedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >, GraphLib::Internal::UndirectedGraph< D, I, E >, GraphLib::Internal::UndirectedGraph< int, std::size_t, Internal::Edge< std::size_t > >, and GraphLib::Internal::UndirectedGraph< int, std::size_t, Internal::WeightedEdge< std::size_t, double > >.
|
inline |
Remove a node by ID.
Removes the node's data and adjacency edges. The ID becomes reusable.
| id | Node ID to remove. |
| std::invalid_argument | If the node with the given ID does not exist. |
Definition at line 415 of file abstract_graph.h.
|
inline |
Update the data of a node.
| id | The node ID. |
| data | New data to assign. |
| std::invalid_argument | If the node with the given ID does not exist. |
Definition at line 432 of file abstract_graph.h.
|
protected |
Stores edges as adjacency list.
Definition at line 465 of file abstract_graph.h.
|
protected |
Maps node IDs to node data.
Definition at line 466 of file abstract_graph.h.
|
protected |
IDs start at 1 to avoid off-by-one errors when giving node count.
Highest assigned node ID so far.
Definition at line 467 of file abstract_graph.h.
|
private |
Queue of reusable node IDs freed from removed nodes.
Definition at line 505 of file abstract_graph.h.