MCPcopy
hub / github.com/networkx/networkx / add_edges_from

Method add_edges_from

networkx/classes/graph.py:986–1065  ·  view source on GitHub ↗

Add all the edges in ebunch_to_add. Parameters ---------- ebunch_to_add : container of edges Each edge given in the container will be added to the graph. The edges must be given as 2-tuples (u, v) or 3-tuples (u, v, d) where d is a diction

(self, ebunch_to_add, **attr)

Source from the content-addressed store, hash-verified

984 nx._clear_cache(self)
985
986 def add_edges_from(self, ebunch_to_add, **attr):
987 """Add all the edges in ebunch_to_add.
988
989 Parameters
990 ----------
991 ebunch_to_add : container of edges
992 Each edge given in the container will be added to the
993 graph. The edges must be given as 2-tuples (u, v) or
994 3-tuples (u, v, d) where d is a dictionary containing edge data.
995 attr : keyword arguments, optional
996 Edge data (or labels or objects) can be assigned using
997 keyword arguments.
998
999 See Also
1000 --------
1001 add_edge : add a single edge
1002 add_weighted_edges_from : convenient way to add weighted edges
1003
1004 Notes
1005 -----
1006 Adding the same edge twice has no effect but any edge data
1007 will be updated when each duplicate edge is added.
1008
1009 Edge attributes specified in an ebunch take precedence over
1010 attributes specified via keyword arguments.
1011
1012 When adding edges from an iterator over the graph you are changing,
1013 a `RuntimeError` can be raised with message:
1014 `RuntimeError: dictionary changed size during iteration`. This
1015 happens when the graph's underlying dictionary is modified during
1016 iteration. To avoid this error, evaluate the iterator into a separate
1017 object, e.g. by using `list(iterator_of_edges)`, and pass this
1018 object to `G.add_edges_from`.
1019
1020 Examples
1021 --------
1022 >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
1023 >>> G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples
1024 >>> e = zip(range(0, 3), range(1, 4))
1025 >>> G.add_edges_from(e) # Add the path graph 0-1-2-3
1026
1027 Associate data to edges
1028
1029 >>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
1030 >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898")
1031
1032 Evaluate an iterator over a graph if using it to modify the same graph
1033
1034 >>> G = nx.Graph([(1, 2), (2, 3), (3, 4)])
1035 >>> # Grow graph by one new node, adding edges to all existing nodes.
1036 >>> # wrong way - will raise RuntimeError
1037 >>> # G.add_edges_from(((5, n) for n in G.nodes))
1038 >>> # correct way - note that there will be no self-edge for node 5
1039 >>> G.add_edges_from(list((5, n) for n in G.nodes))
1040 """
1041 for e in ebunch_to_add:
1042 ne = len(e)
1043 if ne == 3:

Callers 15

updateMethod · 0.95
test_add_edges_fromMethod · 0.95
test_add_edges_fromMethod · 0.95
test_none_nodeMethod · 0.95
test_str_unnamedMethod · 0.95
test_str_namedMethod · 0.95
test_edge_attr2Method · 0.95
test_edge_attr3Method · 0.95
test_add_edges_fromMethod · 0.95
test_add_edges_fromMethod · 0.95

Calls 4

NetworkXErrorClass · 0.90
getMethod · 0.45
updateMethod · 0.45

Tested by 15

test_add_edges_fromMethod · 0.76
test_add_edges_fromMethod · 0.76
test_none_nodeMethod · 0.76
test_str_unnamedMethod · 0.76
test_str_namedMethod · 0.76
test_edge_attr2Method · 0.76
test_edge_attr3Method · 0.76
test_add_edges_fromMethod · 0.76
test_add_edges_fromMethod · 0.76
test_pydot_issue_7581Function · 0.76