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_for_edge, v_for_edge, key=None, **attr)
| 109 | return key |
| 110 | |
| 111 | def add_edge(self, u_for_edge, v_for_edge, key=None, **attr): |
| 112 | """Add an edge between u and v. |
| 113 | |
| 114 | The nodes u and v will be automatically added if they are |
| 115 | not already in the graph. |
| 116 | |
| 117 | Edge attributes can be specified with keywords or by directly |
| 118 | accessing the edge's attribute dictionary. See examples below. |
| 119 | |
| 120 | Parameters |
| 121 | ---------- |
| 122 | u_for_edge, v_for_edge : nodes |
| 123 | Nodes can be, for example, strings or numbers. |
| 124 | Nodes must be hashable (and not None) Python objects. |
| 125 | key : hashable identifier, optional (default=lowest unused integer) |
| 126 | Used to distinguish multiedges between a pair of nodes. |
| 127 | attr : keyword arguments, optional |
| 128 | Edge data (or labels or objects) can be assigned using |
| 129 | keyword arguments. |
| 130 | |
| 131 | Returns |
| 132 | ------- |
| 133 | The edge key assigned to the edge. |
| 134 | |
| 135 | See Also |
| 136 | -------- |
| 137 | add_edges_from : add a collection of edges |
| 138 | |
| 139 | Notes |
| 140 | ----- |
| 141 | To replace/update edge data, use the optional key argument |
| 142 | to identify a unique edge. Otherwise a new edge will be created. |
| 143 | |
| 144 | EasyGraph algorithms designed for weighted graphs cannot use |
| 145 | multigraphs directly because it is not clear how to handle |
| 146 | multiedge weights. Convert to Graph using edge attribute |
| 147 | 'weight' to enable weighted graph algorithms. |
| 148 | |
| 149 | Default keys are generated using the method `new_edge_key()`. |
| 150 | This method can be overridden by subclassing the base class and |
| 151 | providing a custom `new_edge_key()` method. |
| 152 | |
| 153 | Examples |
| 154 | -------- |
| 155 | The following all add the edge e=(1, 2) to graph G: |
| 156 | |
| 157 | >>> G = eg.MultiGraph() |
| 158 | >>> e = (1, 2) |
| 159 | >>> ekey = G.add_edge(1, 2) # explicit two-node form |
| 160 | >>> G.add_edge(*e) # single edge as tuple of two nodes |
| 161 | 1 |
| 162 | >>> G.add_edges_from([(1, 2)]) # add edges from iterable container |
| 163 | [2] |
| 164 | |
| 165 | Associate data to edges using keywords: |
| 166 | |
| 167 | >>> ekey = G.add_edge(1, 2, weight=3) |
| 168 | >>> ekey = G.add_edge(1, 2, key=0, weight=4) # update data for key=0 |