NetworkX

Previous topic

networkx.LabeledDiGraph.remove_nodes_from

Next topic

networkx.LabeledDiGraph.add_edges_from

Quick search

networkx.LabeledDiGraph.add_edge

LabeledDiGraph.add_edge(u, v, data=1)

Add an edge between u and v with optional data.

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

Parameters:

u,v : nodes

Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.

data : Python object

Edge data (or labels or objects) can be entered via the optional argument data which defaults to 1.

Some NetworkX algorithms are designed for weighted graphs for which the edge data must be a number. These may behave unpredictably for edge data that isn’t a number.

See also

Parallel

Notes

Adding an edge that already exists overwrites the edgedata.

Examples

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

>>> G=nx.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_edges_from( [(1,2)] ) # add edges from iterable container

Associate the data myedge to the edge (1,2).

>>> myedge=1.3
>>> G.add_edge(1, 2, myedge)