XNetwork 1.7.5; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory > Class Template Reference

Undirected graph with arbitrary node types. More...

#include <graph.hpp>

Inheritance diagram for xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >:
Inheritance graph
[legend]

Public Types

using nodeview_t = _nodeview_t
 
using Node = typename nodeview_t::value_type
 
using adjlist_inner_dict_factory = adjlist_t
 
using key_type = typename adjlist_t::key_type
 
using value_type = typename adjlist_t::value_type
 
using edge_t = std::pair< Node, Node >
 
using node_t = Node
 

Public Member Functions

 Graph (const nodeview_t &Nodes)
 Construct a graph from a node container.
 
 Graph (uint32_t num_nodes)
 Construct a graph with a given number of integer nodes.
 
auto adj () const
 Get the adjacency mapping of the graph (const version)
 
auto adj ()
 Get the adjacency mapping of the graph (non-const version)
 
auto _nodes_nbrs () const
 Iterate over nodes and their neighbors.
 
auto begin () const
 Begin iterator over nodes.
 
auto end () const
 End iterator over nodes.
 
auto contains (const Node &node) const -> bool
 Check if a node is in the graph.
 
auto operator[] (const Node &node) const -> const auto &
 Access adjacency dict of a node (const version)
 
auto at (const Node &node) const -> const auto &
 Access adjacency dict of a node with bounds check (const version)
 
auto operator[] (const Node &node) -> auto &
 Access adjacency dict of a node (non-const version)
 
auto nodes ()
 Get a NodeView of the graph.
 
auto number_of_nodes () const -> size_t
 Get the number of nodes in the graph.
 
auto order () const
 Get the number of nodes (same as number_of_nodes)
 
auto size () const -> size_t
 Get the number of nodes (same as number_of_nodes)
 
auto number_of_edges () const -> size_t
 Get the number of edges in the graph.
 
auto edges () const -> std::vector< edge_t >
 Return a vector of all edges as (u, v) pairs.
 
auto has_node (const Node &node) const -> bool
 Check if the graph contains a node.
 
template<typename U = key_type>
auto add_edge (const Node &node_u, const Node &node_v) -> typename std::enable_if< std::is_same< U, value_type >::value >::type
 Add an edge between two nodes (for simple key type, SFINAE)
 
template<typename U = key_type>
auto add_edge (const Node &node_u, const Node &node_v) -> typename std::enable_if<!std::is_same< U, value_type >::value >::type
 Add an edge between two nodes (for complex key type, SFINAE)
 
template<typename T >
auto add_edge (const Node &node_u, const Node &node_v, const T &data)
 Add an edge with attached data.
 
template<typename C1 >
auto add_edges_from (const C1 &edges)
 Add edges from a container of edge pairs.
 
template<typename C1 , typename C2 >
auto add_edges_from (const C1 &edges, const C2 &data)
 Add edges from a container with associated data.
 
auto has_edge (const Node &node_u, const Node &node_v) const -> bool
 Check if an edge exists between two nodes.
 
auto degree (const Node &node) const
 Get the degree of a node.
 
auto clear ()
 Remove all nodes and edges from the graph.
 
template<typename F >
auto for_each_edge (F &&func) const -> void
 Apply a callable to each edge without materializing a vector.
 
auto is_multigraph () const
 Check if the graph is a multigraph.
 
auto is_directed () const
 Check if the graph is directed.
 

Static Public Member Functions

static auto end_points (edge_t &e) -> edge_t &
 For compatible with BGL adaptor.
 
static auto end_points (const edge_t &e) -> const edge_t &
 For compatible with BGL adaptor.
 

Public Attributes

size_t _num_of_edges = 0
 Number of edges in the graph (cached)
 
nodeview_t _node
 Container holding all nodes in the graph.
 
adjlist_outer_dict_factory _adj
 Adjacency dictionary keyed by node.
 

Detailed Description

template<typename _nodeview_t, typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
class xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >

Undirected graph with arbitrary node types.

A Graph stores nodes and edges with optional data or attributes. Graphs hold undirected edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) C++ objects.

The Graph class uses a container-of-container-of-container data structure. The outer dict (node_dict) holds adjacency information keyed by node. The next dict (adjlist_dict) represents the adjacency information and holds edge data keyed by neighbor. The inner dict (edge_attr_dict) represents the edge data and holds edge attribute values keyed by attribute names.

Template Parameters
_nodeview_tThe node container type
adjlist_tThe adjacency list type (default: py::set of node values)
adjlist_outer_dict_factoryThe outer dict factory (default: py::dict)

Member Typedef Documentation

◆ adjlist_inner_dict_factory

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::adjlist_inner_dict_factory = adjlist_t

◆ edge_t

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::edge_t = std::pair<Node, Node>

◆ key_type

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::key_type = typename adjlist_t::key_type

◆ Node

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::Node = typename nodeview_t::value_type

◆ node_t

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::node_t = Node

◆ nodeview_t

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::nodeview_t = _nodeview_t

◆ value_type

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
using xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::value_type = typename adjlist_t::value_type

Constructor & Destructor Documentation

◆ Graph() [1/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::Graph ( const nodeview_t Nodes)
inlineexplicit

Construct a graph from a node container.

Parameters
[in]NodesContainer of nodes to initialize the graph

◆ Graph() [2/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::Graph ( uint32_t  num_nodes)
inlineexplicit

Construct a graph with a given number of integer nodes.

Parameters
[in]num_nodesNumber of nodes (0 to num_nodes-1)

Member Function Documentation

◆ _nodes_nbrs()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::_nodes_nbrs ( ) const
inline

Iterate over nodes and their neighbors.

Returns
An iterable of (node, adjacency dict) pairs

◆ add_edge() [1/3]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename U = key_type>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::add_edge ( const Node node_u,
const Node node_v 
) -> typename std::enable_if<std::is_same<U, value_type>::value>::type
inline

Add an edge between two nodes (for simple key type, SFINAE)

Template Parameters
UKey type parameter for SFINAE dispatch
Parameters
[in]node_uFirst endpoint of the edge
[in]node_vSecond endpoint of the edge

◆ add_edge() [2/3]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename U = key_type>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::add_edge ( const Node node_u,
const Node node_v 
) -> typename std::enable_if<!std::is_same<U, value_type>::value>::type
inline

Add an edge between two nodes (for complex key type, SFINAE)

Template Parameters
UKey type parameter for SFINAE dispatch
Parameters
[in]node_uFirst endpoint of the edge
[in]node_vSecond endpoint of the edge

◆ add_edge() [3/3]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename T >
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::add_edge ( const Node node_u,
const Node node_v,
const T data 
)
inline

Add an edge with attached data.

Template Parameters
TType of edge data
Parameters
[in]node_uFirst endpoint
[in]node_vSecond endpoint
[in]dataData to associate with the edge

◆ add_edges_from() [1/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename C1 >
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::add_edges_from ( const C1 edges)
inline

Add edges from a container of edge pairs.

Template Parameters
C1Container type for edges
Parameters
[in]edgesContainer of edge pairs

◆ add_edges_from() [2/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename C1 , typename C2 >
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::add_edges_from ( const C1 edges,
const C2 data 
)
inline

Add edges from a container with associated data.

Template Parameters
C1Container type for edges
C2Container type for edge data
Parameters
[in]edgesContainer of edge pairs
[in]dataContainer of edge data values

◆ adj() [1/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::adj ( )
inline

Get the adjacency mapping of the graph (non-const version)

Returns
AdjacencyView of the internal adjacency structure

◆ adj() [2/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::adj ( ) const
inline

Get the adjacency mapping of the graph (const version)

Returns
AdjacencyView of the internal adjacency structure

◆ at()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::at ( const Node node) const -> const auto&
inline

Access adjacency dict of a node with bounds check (const version)

Parameters
[in]nodeThe node to look up
Returns
Const reference to the adjacency dict of the node

◆ begin()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::begin ( ) const
inline

Begin iterator over nodes.

String identifier of the graph.

This graph attribute appears : the attribute dict gra.graph keyed by the string "name". as well as an attribute (technically a property) gra.name. This is entirely user controlled.

Returns
Iterator to the first node

◆ clear()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::clear ( )
inline

Remove all nodes and edges from the graph.

◆ contains()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::contains ( const Node node) const -> bool
inline

Check if a node is in the graph.

Parameters
[in]nodeThe node to check
Returns
true if the node exists, false otherwise

◆ degree()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::degree ( const Node node) const
inline

Get the degree of a node.

Parameters
[in]nodeThe node to get the degree for
Returns
The number of edges incident to the node

◆ edges()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::edges ( ) const -> std::vector<edge_t>
inline

Return a vector of all edges as (u, v) pairs.

Each undirected edge is reported once with u < v.

Returns
Vector of (u, v) pairs representing all edges

◆ end()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::end ( ) const
inline

End iterator over nodes.

Returns
Iterator past the last node

◆ end_points() [1/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
static auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::end_points ( const edge_t e) -> const edge_t&
inlinestatic

For compatible with BGL adaptor.

Parameters
[in]e
Returns
edge_t&

◆ end_points() [2/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
static auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::end_points ( edge_t e) -> edge_t&
inlinestatic

For compatible with BGL adaptor.

Parameters
[in]e
Returns
edge_t&

◆ for_each_edge()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
template<typename F >
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::for_each_edge ( F &&  func) const -> void
inline

Apply a callable to each edge without materializing a vector.

More efficient than edges() when you only need to iterate once, as it avoids allocating the result vector. Each edge (u, v) is yielded once with u < v.

Template Parameters
FCallable void(const Node&, const Node&) or similar
Parameters
[in]funcCallable invoked for each edge

◆ has_edge()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::has_edge ( const Node node_u,
const Node node_v 
) const -> bool
inline

Check if an edge exists between two nodes.

Parameters
[in]node_uFirst endpoint
[in]node_vSecond endpoint
Returns
true if the edge exists, false otherwise

◆ has_node()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::has_node ( const Node node) const -> bool
inline

Check if the graph contains a node.

Parameters
[in]nodeThe node to check
Returns
true if the node exists, false otherwise

◆ is_directed()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::is_directed ( ) const
inline

Check if the graph is directed.

Returns
false (this is an undirected graph)

◆ is_multigraph()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::is_multigraph ( ) const
inline

Check if the graph is a multigraph.

Returns
false (this is a simple graph)

◆ nodes()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::nodes ( )
inline

Get a NodeView of the graph.

Returns
NodeView providing set-like operations and data lookup

◆ number_of_edges()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::number_of_edges ( ) const -> size_t
inline

Get the number of edges in the graph.

Returns
Number of edges (cached value, O(1))

◆ number_of_nodes()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::number_of_nodes ( ) const -> size_t
inline

Get the number of nodes in the graph.

Returns
Number of nodes

◆ operator[]() [1/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::operator[] ( const Node node) -> auto&
inline

Access adjacency dict of a node (non-const version)

Parameters
[in]nodeThe node to look up
Returns
Reference to the adjacency dict of the node

◆ operator[]() [2/2]

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::operator[] ( const Node node) const -> const auto&
inline

Access adjacency dict of a node (const version)

Parameters
[in]nodeThe node to look up
Returns
Const reference to the adjacency dict of the node

◆ order()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::order ( ) const
inline

Get the number of nodes (same as number_of_nodes)

Returns
Number of nodes

◆ size()

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
auto xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::size ( ) const -> size_t
inline

Get the number of nodes (same as number_of_nodes)

Returns
Number of nodes

Member Data Documentation

◆ _adj

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
adjlist_outer_dict_factory xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::_adj

Adjacency dictionary keyed by node.

◆ _node

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
nodeview_t xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::_node

Container holding all nodes in the graph.

◆ _num_of_edges

template<typename _nodeview_t , typename adjlist_t = py::set<Value_type<_nodeview_t>>, typename adjlist_outer_dict_factory = py::dict<Value_type<_nodeview_t>, adjlist_t>>
size_t xnetwork::Graph< _nodeview_t, adjlist_t, adjlist_outer_dict_factory >::_num_of_edges = 0

Number of edges in the graph (cached)


The documentation for this class was generated from the following file: