DG-CPP 0.1.0
Directed Graph in C++
|
Directed graph base class. More...
#include <base.hpp>
Public Member Functions | |
DGraphBase ()=default | |
Construct a new DGraphBase object. More... | |
bool | hasNode (IDT id) const noexcept |
Check if the graph has a node with id. More... | |
bool | hasEdge (IDT from_id, IDT to_id) const noexcept |
Check if the graph has an edge linking from_id and to_id. More... | |
size_t | numNodes () const noexcept |
The total number of nodes in the graph. More... | |
size_t | numEdges () const noexcept |
The total number of edges in the graph. More... | |
size_t | size () const noexcept |
The total number of nodes in the graph. More... | |
EdgeT | edge (IDT from_id, IDT to_id) const |
Get the edge data between two nodes. More... | |
EdgeT_R | edge (IDT from_id, IDT to_id) |
Get the edge data reference between two nodes. More... | |
template<typename Ret , NonFunc Fn, typename... Args> requires EdgeVT<EdgeT> | |
Ret | edge (IDT from_id, IDT to_id, Fn func, Args... args) const |
Get the edge data after processing with a Lambda expression. More... | |
template<typename Ret , typename... Args> requires EdgeVT<EdgeT> | |
Ret | edge (IDT from_id, IDT to_id, std::function< Ret(EdgeT_, Args...)> func, Args... args) const |
Get the edge data after processing with a std::function . More... | |
bool | strictCheck () const |
Check if the graph is strictly valid. More... | |
std::vector< IDT > | nodesID () const |
Get all nodes IS in the graph. More... | |
std::vector< std::pair< IDT, IDT > > | edges () const |
Get all edge connections in the graph. More... | |
void | insertNode (IDT id) |
Insert a node. More... | |
void | insertNode (IDT id, const std::map< IDT, EdgeT > &map, bool nodes_exist=false) |
Insert a node with edge data. More... | |
void | removeNode (IDT id, bool keep_edge=false) |
Remove the node from the graph. More... | |
void | removeNodeIfExists (IDT id, bool keep_edge=false) |
Remove the node from the graph if it exists. More... | |
void | insertEdgeToExists (IDT from_id, IDT to_id) |
void | insertEdgeToExists (IDT from_id, IDT to_id, EdgeT_CR data) |
void | insertEdge (IDT from_id, IDT to_id, bool to_exists=false) |
Insert an edge to the graph without data. More... | |
void | insertEdge (IDT from_id, IDT to_id, EdgeT_CR data, bool to_exists=false) |
Insert an edge to the graph with data. More... | |
bool | isConnected (IDT from_id, IDT to_id) const |
Check whether to node are connected. More... | |
std::pair< EdgeT_, std::vector< IDT > > | maxWeightPath (IDT from_id, IDT to_id) const |
Obtain the maximum weight and its corresponding path between two nodes. More... | |
template<typename Ret , NonFunc Fn, typename... Args> requires EdgeVT<EdgeT> | |
std::pair< Ret, std::vector< IDT > > | maxWeightPath (IDT from_id, IDT to_id, Fn func, Args... args) const |
Obtain the maximum weight and its corresponding path between two nodes. More... | |
template<typename Ret , typename... Args> requires EdgeVT<EdgeT> | |
std::pair< Ret, std::vector< IDT > > | maxWeightPath (IDT from_id, IDT to_id, std::function< Ret(EdgeT_, Args...)> func, Args... args) const |
Obtain the maximum weight and its corresponding path between two nodes. More... | |
std::pair< EdgeT_, std::vector< IDT > > | minWeightPath (IDT from_id, IDT to_id) const |
Obtain the minimum weight and its corresponding path between two nodes. More... | |
template<typename Ret , NonFunc Fn, typename... Args> requires EdgeVT<EdgeT> | |
std::pair< Ret, std::vector< IDT > > | minWeightPath (IDT from_id, IDT to_id, Fn func, Args... args) const |
Obtain the minimum weight and its corresponding path between two nodes. More... | |
template<typename Ret , typename... Args> requires EdgeVT<EdgeT> | |
std::pair< Ret, std::vector< IDT > > | minWeightPath (IDT from_id, IDT to_id, std::function< Ret(EdgeT_, Args...)> func, Args... args) const |
Obtain the minimum weight and its corresponding path between two nodes. More... | |
void | insertSubGraph (const DGraphBase< EdgeT, IDT > &dg) |
Insert subgraph into the main graph. More... | |
void | insertSubGraph (const DGraphBase< EdgeT, IDT > &dg, IDT id) |
Insert a subgraph at a node. More... | |
void | merge (const DGraphBase< EdgeT, IDT > &dg) |
Merge a graph into the main graph. More... | |
void | clear () |
Clear the whole graph. More... | |
void | clearEdges () |
Clear all edges of the graph. More... | |
bool | operator== (const DGraphBase< EdgeT, IDT > &dg) const |
Check if two graphs are identical. More... | |
bool | operator!= (const DGraphBase< EdgeT, IDT > &dg) const |
Check if two graphs are different. More... | |
DGraphBase & | operator+= (const DGraphBase< EdgeT, IDT > &dg) |
Merge two graphs. More... | |
EdgeT | operator() (IDT from_id, IDT to_id) const |
Get the edge data between two nodes. More... | |
EdgeT_R | operator() (IDT from_id, IDT to_id) |
Get the edge data reference between two nodes. More... | |
Protected Types | |
using | _ET = Edge< EdgeT > |
using | _CN = std::map< IDT, _ET > |
template<typename T = EdgeT> | |
using | EdgeT__ = typename std::conditional< std::is_void_v< T >, int, T >::type |
Edge type that avoids void. More... | |
template<typename T = EdgeT> | |
using | EdgeT_R_ = typename std::conditional< std::is_void_v< T >, int, T >::type & |
Reference to edge type that avoids void. More... | |
template<typename T = EdgeT> | |
using | EdgeT_CR_ = const typename std::conditional< std::is_void_v< T >, int, T >::type & |
Const reference to edge type that avoids void. More... | |
using | EdgeT_ = EdgeT__<> |
using | EdgeT_R = EdgeT_R_<> |
using | EdgeT_CR = EdgeT_CR_<> |
Protected Member Functions | |
void | insertNode (IDT id, const _CN &cn, bool nodes_exist=false) |
Insert a node. More... | |
const std::map< IDT, _CN > & | graph () const |
Return the const reference to the graph map. More... | |
std::map< IDT, _CN > & | graph () |
Return the reference to the graph map. More... | |
Private Member Functions | |
template<typename Ret , typename... Args> requires EdgeVT<EdgeT> | |
std::pair< Ret, std::vector< IDT > > | minOrMaxWeightPath (bool min, IDT from_id, IDT to_id, std::function< Ret(EdgeT_, Args...)> func, Args... args) const |
Obtain the minimum/maximum weight and its corresponding path between two nodes. More... | |
size_t | removeNodeConnectedEdges (IDT id) |
Remove edges connected to a node. More... | |
Private Attributes | |
std::map< IDT, _CN > | g |
Directed graph base class.
If the edge only represents connection, the EdgeT can be void.
EdgeT | Type of edge. |
IDT | Type of ID. |
|
protected |
connection
|
protected |
edge type
|
protected |
safe edge type
|
protected |
Edge type that avoids void.
Since some operations do not allow void, so we change the void type to a non-void type, while retaining other types as the same.
T | Same as EdgeT. Do not specify this type yourself. |
|
protected |
safe edge const reference type
|
protected |
Const reference to edge type that avoids void.
Since some operations do not allow const void&, so we change the void type to non-void type, while retaining other types as the same const reference.
T | Same as EdgeT. Do not specify this type yourself. |
|
protected |
safe edge reference type
|
protected |
Reference to edge type that avoids void.
Since some operations do not allow void&, so we change the void type to non-void type, while retaining other types as the same reference.
T | Same as EdgeT. Do not specify this type yourself. |
|
default |
Construct a new DGraphBase object.
This is the default constructor.
|
inline |
Clear the whole graph.
Remove all the nodes and the connected edges.
|
inline |
Clear all edges of the graph.
DGraphBase< EdgeT, IDT >::EdgeT_R dg::DGraphBase< EdgeT, IDT >::edge | ( | IDT | from_id, |
IDT | to_id | ||
) |
Get the edge data reference between two nodes.
from_id | The from node id. |
to_id | The to node id. |
EdgeT dg::DGraphBase< EdgeT, IDT >::edge | ( | IDT | from_id, |
IDT | to_id | ||
) | const |
Get the edge data between two nodes.
from_id | The from node id. |
to_id | The to node id. |
|
inline |
Get the edge data after processing with a Lambda expression.
You can also use the std::function
version instead of the Lambda expression.
You need to manually set the return type as the function template argument, for example
Ret | The return type of the Lambda expression as well as this function. |
Fn | The Lambda expression type. |
Args | The argument types of the Lambda expression. |
from_id | The from node id. |
to_id | The to node id. |
func | The Lambda expression. |
args | The arguments of the Lambda expression. |
|
inline |
Get the edge data after processing with a std::function
.
You have to specifically define the std::function
, for example
You can also use the Lambda version of the edge
method instead of the std::function
version.
Ret | The return type of the std::function . |
Args | The argument types of the std::function . |
from_id | The from node id. |
to_id | The to node id. |
func | The std::function . |
args | The arguments of the std::function . |
std::function
.
|
inline |
Get all edge connections in the graph.
std::pair
.
|
inlineprotected |
Return the reference to the graph map.
|
inlineprotected |
Return the const reference to the graph map.
|
inlinenoexcept |
Check if the graph has an edge linking from_id and to_id.
If either from_id or to_id node does not exist, we conclude there is no such edge.
from_id | The from node id. |
to_id | The to node id. |
true | There exists the edge in the graph. |
false | There does not exist the edge in the graph. |
|
inlinenoexcept |
Check if the graph has a node with id.
id | The node id (which should be unique). |
true | There exists the node in the graph. |
false | There does not exist the node in the graph. |
void dg::DGraphBase< EdgeT, IDT >::insertEdge | ( | IDT | from_id, |
IDT | to_id, | ||
bool | to_exists = false |
||
) |
Insert an edge to the graph without data.
from_id | The from node id. If it does not exist, it will be created automatically. |
to_id | The to node id. If it does not exist, it will be created automatically. |
to_exists | Whether the to node exists before insertion. If this is set to true, the from node graph will not be updated. Please be sure before setting this to true. |
void dg::DGraphBase< EdgeT, IDT >::insertEdge | ( | IDT | from_id, |
IDT | to_id, | ||
EdgeT_CR | data, | ||
bool | to_exists = false |
||
) |
Insert an edge to the graph with data.
from_id | The from node id. If it does not exist, it will be created automatically. |
to_id | The to node id. If it does not exist, it will be created automatically. |
data | The edge data. |
to_exists | Whether the to node exists before insertion. If this is set to true, the from node graph will not be updated. Please be sure before setting this to true. |
|
inline |
|
inline |
void dg::DGraphBase< EdgeT, IDT >::insertNode | ( | IDT | id | ) |
Insert a node.
If the node id exists before, its edge connections will be cleared. If the node id does not exist, it will be created.
id | The node id. |
|
protected |
Insert a node.
id | The node id. |
cn | The edge connections. |
nodes_exist | Whether the node exists before insertion. |
void dg::DGraphBase< EdgeT, IDT >::insertNode | ( | IDT | id, |
const std::map< IDT, EdgeT > & | map, | ||
bool | nodes_exist = false |
||
) |
Insert a node with edge data.
If the node id exists before, its edge connections will be cleared. If the node id does not exist, it will be created.
If nodes_exist
is set to true, the node graph will not be updated. Otherwise (the default behavior) the node graph will be updated by checking all connected edges.
nodes_exist
does not mean whether the node with id exists. It specifies whether the connected nodes exist. id | The node id. |
map | The edge data map. |
nodes_exist | Whether all nodes in edge connections exist before insertion (default as false). You should be sure when setting this to true. Otherwise there can be unexpected behaviors. |
|
inline |
Insert subgraph into the main graph.
This is an alias for merge();
dg | The subgraph. |
|
inline |
Insert a subgraph at a node.
This is equivalent to remove a node and insert a subgraph.
dg | The subgraph. |
id | The node id. |
|
inline |
Check whether to node are connected.
from_id | The from node id. |
to_id | The to node id. |
true | There exists a path. |
false | There does not exist a path. |
|
inline |
Obtain the maximum weight and its corresponding path between two nodes.
This finds the maximum weight path with the Djkstra algorithm. So all weights should be non-negative.
Example use:
from_id | The from node id. |
to_id | The to node id. |
|
inline |
Obtain the maximum weight and its corresponding path between two nodes.
This finds the maximum weight path with the Djkstra algorithm. So all weights should be non-negative.
Example use:
Ret | The weight type, i.e. Lambda function return type. |
Fn | The Lambda expression type. |
Args | The Lambda expression argument types. |
from_id | The from node id. |
to_id | The to node id. |
func | The Lambda expression. |
args | The Lambda expression arguments. |
|
inline |
Obtain the maximum weight and its corresponding path between two nodes.
This finds the maximum weight path with the Djkstra algorithm. So all weights should be non-negative.
Ret | The weight type, i.e. std::function return type. |
Args | std::function argument types. |
from_id | The from node id. |
to_id | The to node id. |
func | The std::function . |
args | The std::function arguments. |
|
inline |
Merge a graph into the main graph.
dg | The graph to be merged. |
|
inlineprivate |
Obtain the minimum/maximum weight and its corresponding path between two nodes.
Ret | The weight type, i.e. Lambda function return type. |
Args | std::function argument types. |
min | Whether to find the minimum or maximum weight. |
from_id | The from node id. |
to_id | The to node id. |
func | The std::function . |
args | The std::function arguments. |
|
inline |
Obtain the minimum weight and its corresponding path between two nodes.
This finds the minimum weight path with the Djkstra algorithm. So all weights should be non-negative.
Example see maxWeightPath().
from_id | The from node id. |
to_id | The to node id. |
|
inline |
Obtain the minimum weight and its corresponding path between two nodes.
This finds the minimum weight path with the Djkstra algorithm. So all weights should be non-negative.
Example see maxWeightPath().
Ret | The weight type, i.e. Lambda function return type. |
Fn | The Lambda expression type. |
Args | The Lambda expression argument types. |
from_id | The from node id. |
to_id | The to node id. |
func | The Lambda expression. |
args | The Lambda expression arguments. |
|
inline |
Obtain the minimum weight and its corresponding path between two nodes.
This finds the minimum weight path with the Djkstra algorithm. So all weights should be non-negative.
Ret | The weight type, i.e. std::function return type. |
Args | std::function argument types. |
from_id | The from node id. |
to_id | The to node id. |
func | The std::function . |
args | The std::function arguments. |
std::vector< IDT > dg::DGraphBase< EdgeT, IDT >::nodesID |
Get all nodes IS in the graph.
|
inlinenoexcept |
The total number of edges in the graph.
|
inlinenoexcept |
The total number of nodes in the graph.
|
inline |
Check if two graphs are different.
dg | Another graph. |
true | The two graphs are different. |
false | The two graphs are identical. |
|
inline |
Get the edge data reference between two nodes.
from_id | The from node id. |
to_id | The to node id. |
|
inline |
Get the edge data between two nodes.
from_id | The from node id. |
to_id | The to node id. |
|
inline |
Merge two graphs.
This is an alias for merge().
dg | Another graph. |
|
inline |
Check if two graphs are identical.
All nodes and edges should be the same.
dg | Another graph. |
true | The two graphs are identical. |
false | The two graphs are different. |
void dg::DGraphBase< EdgeT, IDT >::removeNode | ( | IDT | id, |
bool | keep_edge = false |
||
) |
Remove the node from the graph.
dg::node_not_exists | Removes a non-existent node. Use removeNodeIfExists() to avoid the exception. |
id | The node id. |
keep_edge | Whether keeps the connected edges after removing the node (default as false). |
|
private |
Remove edges connected to a node.
id | The node id. |
void dg::DGraphBase< EdgeT, IDT >::removeNodeIfExists | ( | IDT | id, |
bool | keep_edge = false |
||
) |
Remove the node from the graph if it exists.
Nothing will be done if the node does not exist.
id | The node id. |
keep_edge | Whether keeps the connected edges after removing the node (default as false). |
|
inlinenoexcept |
The total number of nodes in the graph.
This is the alias for numNodes().
|
inline |
Check if the graph is strictly valid.
A graph is strictly valid if:
true | |
false |
|
private |
the main graph