Update the graph using nodes/edges/graphs as input. Like dict.update, this method takes a graph as input, adding the graph's nodes and edges to this graph. It can also take two inputs: edges and nodes. Finally it can take either edges or nodes. To specify only nodes
(self, edges=None, nodes=None)
| 1112 | |
| 1113 | @clear_mutation_cache |
| 1114 | def update(self, edges=None, nodes=None): |
| 1115 | """Update the graph using nodes/edges/graphs as input. |
| 1116 | |
| 1117 | Like dict.update, this method takes a graph as input, adding the |
| 1118 | graph's nodes and edges to this graph. It can also take two inputs: |
| 1119 | edges and nodes. Finally it can take either edges or nodes. |
| 1120 | To specify only nodes the keyword `nodes` must be used. |
| 1121 | |
| 1122 | The collections of edges and nodes are treated similarly to |
| 1123 | the add_edges_from/add_nodes_from methods. When iterated, they |
| 1124 | should yield 2-tuples (u, v) or 3-tuples (u, v, datadict). |
| 1125 | |
| 1126 | Parameters |
| 1127 | ---------- |
| 1128 | edges : Graph object, collection of edges, or None |
| 1129 | The first parameter can be a graph or some edges. If it has |
| 1130 | attributes `nodes` and `edges`, then it is taken to be a |
| 1131 | Graph-like object and those attributes are used as collections |
| 1132 | of nodes and edges to be added to the graph. |
| 1133 | If the first parameter does not have those attributes, it is |
| 1134 | treated as a collection of edges and added to the graph. |
| 1135 | If the first argument is None, no edges are added. |
| 1136 | nodes : collection of nodes, or None |
| 1137 | The second parameter is treated as a collection of nodes |
| 1138 | to be added to the graph unless it is None. |
| 1139 | If `edges is None` and `nodes is None` an exception is raised. |
| 1140 | If the first parameter is a Graph, then `nodes` is ignored. |
| 1141 | |
| 1142 | Examples |
| 1143 | -------- |
| 1144 | >>> G = nx.path_graph(5) |
| 1145 | >>> G.update(nx.complete_graph(range(4, 10))) |
| 1146 | >>> from itertools import combinations |
| 1147 | >>> edges = ( |
| 1148 | ... (u, v, {"power": u * v}) |
| 1149 | ... for u, v in combinations(range(10, 20), 2) |
| 1150 | ... if u * v < 225 |
| 1151 | ... ) |
| 1152 | >>> nodes = [1000] # for singleton, use a container |
| 1153 | >>> G.update(edges, nodes) |
| 1154 | |
| 1155 | See Also |
| 1156 | -------- |
| 1157 | add_edges_from: add multiple edges to a graph |
| 1158 | add_nodes_from: add multiple nodes to a graph |
| 1159 | """ |
| 1160 | if edges is not None: |
| 1161 | if nodes is not None: |
| 1162 | self.add_nodes_from(nodes) |
| 1163 | self.add_edges_from(edges) |
| 1164 | else: |
| 1165 | try: |
| 1166 | graph_nodes = edges.nodes |
| 1167 | graph_edges = edges.edges |
| 1168 | except AttributeError: |
| 1169 | self.add_edges_from(edges) |
| 1170 | else: # edges is Graph-like |
| 1171 | self.add_nodes_from(graph_nodes.data()) |