3.4. MultiGraph - Undirected graphs with self loops and parallel edges

3.4.1. MultiGraph()

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

An undirected graph class that can store multiedges.

Multiedges are multiple edges between two nodes. Each edge can hold optional data or attributes.

A MultiGraph holds undirected edges. Self loops are allowed.

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 multigraph subclass that tracks the order nodes are added.

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

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

>>> class OrderedGraph(nx.MultiGraph):
...    node_dict_factory = OrderedDict
...    adjlist_dict_factory = OrderedDict
...    edge_key_dict_factory = OrderedDict
>>> G = OrderedGraph()
>>> G.add_nodes_from( (2,1) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1,2,{'weight':0.1}), (2,1,1,{'weight':0.2}), (1,1)) )
>>> list(G.edges(keys=True))
[(2, 2, 0), (2, 1, 2), (2, 1, 1), (1, 1, 0)]

3.4.2. Adding and removing nodes and edges

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

3.4.3. Iterating over nodes and edges

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

3.4.4. Information about graph structure

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

3.4.5. Making copies and subgraphs

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