DiGraph.add_edge(u_of_edge, v_of_edge, **attr)

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 directly accessing the edge’s attribute dictionary. See examples below.

Parameters
• u_of_edge (nodes) – Nodes can be integer or string objects.

• v_of_edge (nodes) – Nodes can be integer or string objects.

• attr (keyword arguments, optional) – Edge data (or labels or objects) can be assigned using keyword arguments.

add_edges_from

Notes

Many algorithms designed for weighted graphs use an edge attribute (by default weight) to hold a numerical value.

Examples

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

>>> G = nx.Graph()   # or DiGraph
>>> 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)