Example of Directed Weighted Graph
The General Directed Weighted Graph (GDWG) is a C++ library that provides a framework for creating and managing directed, weighted graphs with value semantics. The library allows users to efficiently perform operations on nodes and edges within a graph, such as adding nodes, inserting edges, replacing nodes, merging nodes, and checking for connectivity between nodes. It is designed to handle graphs with unique nodes and ordered edges based on source node, destination node, and edge weight.
#include "gdwg_graph.h"
#include <optional>
int main() {
using graph = gdwg::graph<int, int>;
graph g;
// Insert nodes
g.insert_node(1);
g.insert_node(7);
g.insert_node(12);
g.insert_node(14);
g.insert_node(19);
g.insert_node(21);
g.insert_node(31);
// Insert edges with and without weights
g.insert_edge(1, 1, 4); // (1 -> 1 | W | 4)
g.insert_edge(1, 7, std::nullopt); // (1 -> 7 | U)
g.insert_edge(1, 7, 2); // (1 -> 7 | W | 2)
g.insert_edge(1, 12, 3); // (1 -> 12 | W | 3)
g.insert_edge(7, 21, std::nullopt); // (7 -> 21 | U)
g.insert_edge(14, 14, 7); // (14 -> 14 | W | 7)
g.insert_edge(19, 1, 3); // (19 -> 1 | W | 3)
g.insert_edge(19, 21, 2); // (19 -> 21 | W | 2)
g.insert_edge(21, 14, 23); // (21 -> 14 | W | 23)
g.insert_edge(21, 31, 14); // (21 -> 31 | W | 14)
// Output the graph structure
std::cout << g << std::endl;
return 0;
}To use the GDWG library, include the header file in your C++ project:
#include "gdwg_graph.h"Traverse the graph using iterators:
for (const auto& edge : g) {
std::cout << "Edge from " << edge.from << " to " << edge.to;
if (edge.weight) {
std::cout << " with weight " << *edge.weight;
}
std::cout << std::endl;
}-
bool insert_node(N const& value)- Adds a new node with value
valueto the graph if it doesn't already exist. - Returns:
trueif the node is added,falseotherwise.
- Adds a new node with value
-
bool replace_node(N const& old_data, N const& new_data)- Replaces the original data,
old_data, stored at this particular node withnew_data. - Returns:
trueif successful,falseifnew_dataalready exists.
- Replaces the original data,
-
void merge_replace_node(N const& old_data, N const& new_data)- Replaces the
old_datanode withnew_data, merging connections and removing duplicates.
- Replaces the
-
bool erase_node(N const& value)- Removes the node
valuefrom the graph. - Returns:
trueif the node is removed,falseotherwise.
- Removes the node
-
bool insert_edge(N const& src, N const& dst, std::optional<E> weight = std::nullopt)- Adds a new edge from
srctodstwith an optionalweight. - Returns:
trueif the edge is added,falseotherwise.
- Adds a new edge from
-
bool erase_edge(N const& src, N const& dst, std::optional<E> weight = std::nullopt)- Removes the edge from
srctodstwith the specifiedweight. - Returns:
trueif the edge is removed,falseotherwise.
- Removes the edge from
-
iterator erase_edge(iterator i)- Removes the edge pointed to by the iterator
i. - Returns: An iterator pointing to the element immediately after
i.
- Removes the edge pointed to by the iterator
-
iterator erase_edge(iterator i, iterator s)- Removes all edges between the iterators
[i, s). - Returns: An iterator equivalent to
s.
- Removes all edges between the iterators
-
bool is_node(N const& value) const- Checks if the node
valueexists in the graph. - Returns:
trueif the node exists,falseotherwise.
- Checks if the node
-
bool empty() const noexcept- Checks if the graph is empty.
- Returns:
trueif there are no nodes,falseotherwise.
-
bool is_connected(N const& src, N const& dst) const- Checks if there is a directed edge from
srctodst. - Returns:
trueif the nodes are connected,falseotherwise.
- Checks if there is a directed edge from
-
std::vector<N> nodes() const- Retrieves all nodes in the graph, sorted in ascending order.
- Returns: A vector of nodes.
-
edge_list edges(N const& src, N const& dst) const- Retrieves all edges from
srctodst. - Returns: A list of edges.
- Retrieves all edges from
-
iterator find(N const& src, N const& dst, std::optional<E> weight = std::nullopt) const- Finds an edge from
srctodstwith the specifiedweight. - Returns: An iterator pointing to the found edge, or
end()if not found.
- Finds an edge from
-
std::vector<N> connections(N const& src) const- Retrieves all nodes connected to
src. - Returns: A vector of connected nodes.
- Retrieves all nodes connected to
-
iterator begin() const noexcept- Returns an iterator to the beginning of the graph.
-
iterator end() const noexcept- Returns an iterator to the end of the graph.
friend std::ostream& operator<<(std::ostream& os, graph<N, E> const& g)- Outputs the graph to an output stream
os. - Returns: The output stream.
- Outputs the graph to an output stream
Basic tests are provided in gdwg_graph.test.cpp. To run the test, first build a Makefile using CMake:
$ cmake -B build
Then compile and run the test (Linux environment):
$ cd build
make test_sum1
./test_sum1