Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

Graph.add_edge(u, v, attr_dict=None, **attr)[source]

Add an edge between u and v.

The nodes u and v will be automatically added if they are not already in the graph.

Edge attributes can be specified with keywords or by providing a dictionary with key/value pairs. See examples below.

Parameters: v (u,) – Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects. attr_dict (dictionary, optional (default= no attributes)) – Dictionary of edge attributes. Key/value pairs will update existing data associated with the edge. attr (keyword arguments, optional) – Edge data (or labels or objects) can be assigned using keyword arguments.

add_edges_from()

Notes

Many NetworkX algorithms designed for weighted graphs use as the edge weight a numerical value assigned to a keyword which by default is ‘weight’.

Examples

The following all add the edge e=(1,2) to graph G:

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> e = (1,2)
>>> G.add_edge(1, 2)           # explicit two-node form
>>> G.add_edge(*e)             # single edge as tuple of two nodes

>>> G.add_edge(1, 2, weight=3)