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
(self, u_of_edge, v_of_edge, **attr)
| 915 | return False |
| 916 | |
| 917 | def add_edge(self, u_of_edge, v_of_edge, **attr): |
| 918 | """Add an edge between u and v. |
| 919 | |
| 920 | The nodes u and v will be automatically added if they are |
| 921 | not already in the graph. |
| 922 | |
| 923 | Edge attributes can be specified with keywords or by directly |
| 924 | accessing the edge's attribute dictionary. See examples below. |
| 925 | |
| 926 | Parameters |
| 927 | ---------- |
| 928 | u_of_edge, v_of_edge : nodes |
| 929 | Nodes can be, for example, strings or numbers. |
| 930 | Nodes must be hashable (and not None) Python objects. |
| 931 | attr : keyword arguments, optional |
| 932 | Edge data (or labels or objects) can be assigned using |
| 933 | keyword arguments. |
| 934 | |
| 935 | See Also |
| 936 | -------- |
| 937 | add_edges_from : add a collection of edges |
| 938 | |
| 939 | Notes |
| 940 | ----- |
| 941 | Adding an edge that already exists updates the edge data. |
| 942 | |
| 943 | Many NetworkX algorithms designed for weighted graphs use |
| 944 | an edge attribute (by default `weight`) to hold a numerical value. |
| 945 | |
| 946 | Examples |
| 947 | -------- |
| 948 | The following all add the edge e=(1, 2) to graph G: |
| 949 | |
| 950 | >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 951 | >>> e = (1, 2) |
| 952 | >>> G.add_edge(1, 2) # explicit two-node form |
| 953 | >>> G.add_edge(*e) # single edge as tuple of two nodes |
| 954 | >>> G.add_edges_from([(1, 2)]) # add edges from iterable container |
| 955 | |
| 956 | Associate data to edges using keywords: |
| 957 | |
| 958 | >>> G.add_edge(1, 2, weight=3) |
| 959 | >>> G.add_edge(1, 3, weight=7, capacity=15, length=342.7) |
| 960 | |
| 961 | For non-string attribute keys, use subscript notation. |
| 962 | |
| 963 | >>> G.add_edge(1, 2) |
| 964 | >>> G[1][2].update({0: 5}) |
| 965 | >>> G.edges[1, 2].update({0: 5}) |
| 966 | """ |
| 967 | u, v = u_of_edge, v_of_edge |
| 968 | # add nodes |
| 969 | if u not in self._node: |
| 970 | if u is None: |
| 971 | raise ValueError("None cannot be a node") |
| 972 | self._adj[u] = self.adjlist_inner_dict_factory() |
| 973 | self._node[u] = self.node_attr_dict_factory() |
| 974 | if v not in self._node: |