3.2. Graph – Undirected graphs with self loops

3.2.1. Graph()

networkx.Graph(data=None, **attr)[source]

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) Python objects with optional key/value attributes.

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

Parameters:

data : input graph

Data to initialize graph. If data=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.

attr : keyword arguments, optional (default= no attributes)

Attributes to add to graph as key=value pairs.

Examples

Create a graph subclass that tracks the order nodes are added.

>>> from collections import OrderedDict
>>> class OrderedNodeGraph(nx.Graph):
...     node_dict_factory=OrderedDict
>>> G=OrderedNodeGraph()
>>> G.add_nodes_from( (2,1) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1), (1,1)) )
>>> list(G.edges())
[(2, 1), (2, 2), (1, 1)]

Create a graph object that tracks the order nodes are added and for each node track the order that neighbors are added.

>>> class OrderedGraph(nx.Graph):
...    node_dict_factory = OrderedDict
...    adjlist_dict_factory = OrderedDict
>>> G = OrderedGraph()
>>> G.add_nodes_from( (2,1) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1), (1,1)) )
>>> list(G.edges())
[(2, 2), (2, 1), (1, 1)]

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(nx.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)
>>> list(G.edges(data= True))
[(1, 2, {'weight': 1})]
>>> G.add_edge(2,2)
>>> G[2][1] is G[2][2]
True

3.2.2. Adding and removing nodes and edges

Graph.__init__([data]) Initialize a graph with edges, name, graph attributes.
Graph.add_node(n[, attr_dict]) Add a single node n and update node attributes.
Graph.add_nodes_from(nodes, **attr) Add multiple nodes.
Graph.remove_node(n) Remove node n.
Graph.remove_nodes_from(nodes) Remove multiple nodes.
Graph.add_edge(u, v[, attr_dict]) Add an edge between u and v.
Graph.add_edges_from(ebunch[, attr_dict]) Add all the edges in ebunch.
Graph.add_weighted_edges_from(ebunch[, weight]) Add all the edges in ebunch as weighted edges with specified weights.
Graph.remove_edge(u, v) Remove the edge between u and v.
Graph.remove_edges_from(ebunch) Remove all edges specified in ebunch.
Graph.clear() Remove all nodes and edges from the graph.

3.2.3. Iterating over nodes and edges

Graph.nodes([data, default]) Returns an iterator over the nodes.
Graph.__iter__() Iterate over the nodes.
Graph.edges([nbunch, data, default]) Return an iterator over the edges.
Graph.get_edge_data(u, v[, default]) Return the attribute dictionary associated with edge (u,v).
Graph.neighbors(n) Return an iterator over all neighbors of node n.
Graph.__getitem__(n) Return a dict of neighbors of node n.
Graph.adjacency() Return an iterator over (node, adjacency dict) tuples for all nodes.
Graph.nbunch_iter([nbunch]) Return an iterator over nodes contained in nbunch that are also in the graph.

3.2.4. Information about graph structure

Graph.has_node(n) Return True if the graph contains the node n.
Graph.__contains__(n) Return True if n is a node, False otherwise.
Graph.has_edge(u, v) Return True if the edge (u,v) is in the graph.
Graph.order() Return the number of nodes in the graph.
Graph.number_of_nodes() Return the number of nodes in the graph.
Graph.__len__() Return the number of nodes.
Graph.degree([nbunch, weight]) Return an iterator for (node, degree) or degree for single node.
Graph.size([weight]) Return the number of edges or total of all edge weights.
Graph.number_of_edges([u, v]) Return the number of edges between two nodes.
Graph.nodes_with_selfloops() Returns an iterator over nodes with self loops.
Graph.selfloop_edges([data, default]) Returns an iterator over selfloop edges.
Graph.number_of_selfloops() Return the number of selfloop edges.

3.2.5. Making copies and subgraphs

Graph.copy([with_data]) Return a copy of the graph.
Graph.to_undirected() Return an undirected copy of the graph.
Graph.to_directed() Return a directed representation of the graph.
Graph.subgraph(nbunch) Return the subgraph induced on nodes in nbunch.
Graph.edge_subgraph(edges) Returns the subgraph induced by the specified edges.