MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / add_edges_from

Method add_edges_from

easygraph/classes/graph.py:915–977  ·  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

913 self._clear_cache()
914
915 def add_edges_from(self, ebunch_to_add, **attr):
916 """Add all the edges in ebunch_to_add.
917
918 Parameters
919 ----------
920 ebunch_to_add : container of edges
921 Each edge given in the container will be added to the
922 graph. The edges must be given as 2-tuples (u, v) or
923 3-tuples (u, v, d) where d is a dictionary containing edge data.
924 attr : keyword arguments, optional
925 Edge data (or labels or objects) can be assigned using
926 keyword arguments.
927
928 See Also
929 --------
930 add_edge : add a single edge
931 add_weighted_edges_from : convenient way to add weighted edges
932
933 Notes
934 -----
935 Adding the same edge twice has no effect but any edge data
936 will be updated when each duplicate edge is added.
937
938 Edge attributes specified in an ebunch take precedence over
939 attributes specified via keyword arguments.
940
941 Examples
942 --------
943 >>> G = eg.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
944 >>> G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples
945 >>> e = zip(range(0, 3), range(1, 4))
946 >>> G.add_edges_from(e) # Add the path graph 0-1-2-3
947
948 Associate data to edges
949
950 >>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
951 >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898")
952 """
953 for e in ebunch_to_add:
954 ne = len(e)
955 if ne == 3:
956 u, v, dd = e
957 elif ne == 2:
958 u, v = e
959 dd = {} # doesn't need edge_attr_dict_factory
960 else:
961 raise EasyGraphError(f"Edge tuple {e} must be a 2-tuple or 3-tuple.")
962 if u not in self._node:
963 if u is None:
964 raise ValueError("None cannot be a node")
965 self._adj[u] = self.adjlist_inner_dict_factory()
966 self._node[u] = self.node_attr_dict_factory()
967 if v not in self._node:
968 if v is None:
969 raise ValueError("None cannot be a node")
970 self._adj[v] = self.adjlist_inner_dict_factory()
971 self._node[v] = self.node_attr_dict_factory()
972 datadict = self._adj[u].get(v, self.edge_attr_dict_factory())

Callers 15

test_k_coreFunction · 0.95
test_write_edgelist_1Method · 0.95
test_write_edgelist_2Method · 0.95
from_dict_of_listsFunction · 0.45
from_dict_of_dictsFunction · 0.45
from_edgelistFunction · 0.45
_relabel_copyFunction · 0.45
from_numpy_arrayFunction · 0.45
from_pandas_edgelistFunction · 0.45

Calls 3

_clear_cacheMethod · 0.95
EasyGraphErrorClass · 0.90
updateMethod · 0.80

Tested by 15

test_k_coreFunction · 0.76
test_write_edgelist_1Method · 0.76
test_write_edgelist_2Method · 0.76
setup_classMethod · 0.36
setup_classMethod · 0.36