Struct object

Inheritance Relationships

Base Type

Derived Types

Struct Documentation

struct object : public py::dict<const char*, boost::any>

Base class for undirected graphs.

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 with optional key/value attributes. By convention None is not used as a node.

Edges are represented as links between nodes with optional key/value attributes.

Parameters

node_container : input graph (optional, default: None) Data to initialize graph. If None (default) an empty graph is created. The data can be any format that is supported by the to_networkx_graph() function, currently including edge list, dict of dicts, dict of lists, NetworkX graph, NumPy matrix or 2d ndarray, SciPy sparse matrix, or PyGraphviz graph.

See Also

DiGraph MultiGraph MultiDiGraph OrderedGraph

Examples

Create an empty graph structure (a “null graph”) with 5 nodes and no edges.

auto v = std::vector{3, 4, 2, 8}; auto G = xn::Graph(v);

auto va = py::dict{{3, 0.1}, {4, 0.5}, {2, 0.2}}; auto G = xn::Graph(va);

auto r = py::range(100); auto G = xn::Graph(r);

G can be grown in several ways.

Nodes:**

Add one node at a time:

G.add_node(1)

Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph).

G.add_nodes_from([2, 3]) G.add_nodes_from(range(100, 110)) H = xn::path_graph(10) G.add_nodes_from(H)

In addition to strings and integers any hashable C++ object (except None) can represent a node, e.g. a customized node object, or even another Graph.

G.add_node(H)

Edges:**

G can also be grown by adding edges.

Add one edge,

G.add_edge(1, 2);

a list of edges,

G.add_edges_from([(1, 2), (1, 3)]);

or a collection of edges,

G.add_edges_from(H.edges());

If some edges connect nodes not yet in the graph, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.

Attributes:**

Each graph can hold key/value attribute pairs in an associated attribute dictionary (the keys must be hashable). By default these are empty, but can be added or changed using direct manipulation of the attribute dictionaries named graph, node and edge respectively.

G.graph[“day”] = boost::any(“Friday”);

{‘day’: ‘Friday’}

Subclasses (Advanced):**

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.

Each of these three dicts can be replaced in a subclass by a user defined dict-like object. In general, the dict-like features should be maintained but extra features can be added. To replace one of the dicts create a new graph class by changing the class(!) variable holding the factory for that dict-like structure. The variable names are node_dict_factory, node_attr_dict_factory, adjlist_inner_dict_factory, adjlist_outer_dict_factory, edge_attr_dict_factory and graph_attr_dict_factory.

node_dict_factory : function, (default: dict) Factory function to be used to create the dict containing node attributes, keyed by node id. It should require no arguments and return a dict-like object

node_attr_dict_factory: function, (default: dict) Factory function to be used to create the node attribute dict which holds attribute values keyed by attribute name. It should require no arguments and return a dict-like object

adjlist_outer_dict_factory : function, (default: dict) Factory function to be used to create the outer-most dict in the data structure that holds adjacency info keyed by node. It should require no arguments and return a dict-like object.

adjlist_inner_dict_factory : function, (default: dict) Factory function to be used to create the adjacency list dict which holds edge data keyed by neighbor. It should require no arguments and return a dict-like object

edge_attr_dict_factory : function, (default: dict) Factory function to be used to create the edge attribute dict which holds attribute values keyed by attribute name. It should require no arguments and return a dict-like object.

graph_attr_dict_factory : function, (default: dict) Factory function to be used to create the graph attribute dict which holds attribute values keyed by attribute name. It should require no arguments and return a dict-like object.

Typically, if your extension doesn’t impact the data structure all methods will inherit without issue except: to_directed/to_undirected. By default these methods create a DiGraph/Graph class and you probably want them to create your extension of a DiGraph/Graph. To facilitate this we define two class variables that you can set in your subclass.

to_directed_class : callable, (default: DiGraph or MultiDiGraph) Class to create a new graph structure in the to_directed method. If None, a NetworkX class (DiGraph or MultiDiGraph) is used.

to_undirected_class : callable, (default: Graph or MultiGraph) Class to create a new graph structure in the to_undirected method. If None, a NetworkX class (Graph or MultiGraph) is used.

Examples

Create a low memory graph class that effectively disallows edge attributes by using a single attribute dict for all edges. This reduces the memory used, but you lose edge attributes.

class ThinGraph(xn::Graph):

… all_edge_dict = {‘weight’: 1} … def single_edge_dict(self): … return self.all_edge_dict … edge_attr_dict_factory = single_edge_dict

G = ThinGraph() G.add_edge(2, 1) G[2][1]

{‘weight’: 1}

G.add_edge(2, 2) G[2][1] is G[2][2]

True

Please see :mod:~networkx.classes.ordered for more examples of creating graph subclasses by overwriting the base class dict with a dictionary-like object.

Subclassed by xn::Graph< nodeview_t, adjlist_t >, xn::Graph< __nodeview_t, adjlist_t >