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

Method to_index_node_graph

easygraph/classes/directed_graph.py:1140–1188  ·  view source on GitHub ↗

Returns a deep copy of graph, with each node switched to its index. Considering that the nodes of your graph may be any possible hashable Python object, you can get an isomorphic graph of the original one, with each node switched to its index. Parameters ----------

(self, begin_index=0)

Source from the content-addressed store, hash-verified

1138 return self.nodes_subgraph(from_nodes=neighbors_of_center)
1139
1140 def to_index_node_graph(self, begin_index=0):
1141 """Returns a deep copy of graph, with each node switched to its index.
1142
1143 Considering that the nodes of your graph may be any possible hashable Python object,
1144 you can get an isomorphic graph of the original one, with each node switched to its index.
1145
1146 Parameters
1147 ----------
1148 begin_index : int
1149 The begin index of the index graph.
1150
1151 Returns
1152 -------
1153 G : easygraph.Graph
1154 Deep copy of graph, with each node switched to its index.
1155
1156 index_of_node : dict
1157 Index of node
1158
1159 node_of_index : dict
1160 Node of index
1161
1162 Examples
1163 --------
1164 The following method returns this isomorphic graph and index-to-node dictionary
1165 as well as node-to-index dictionary.
1166
1167 >>> G = eg.Graph()
1168 >>> G.add_edges([
1169 ... ('Jack', 'Maria'),
1170 ... ('Maria', 'Andy'),
1171 ... ('Jack', 'Tom')
1172 ... ])
1173 >>> G_index_graph, index_of_node, node_of_index = G.to_index_node_graph()
1174
1175 """
1176 G = self.__class__()
1177 G.graph.update(self.graph)
1178 index_of_node = dict()
1179 node_of_index = dict()
1180 for index, (node, node_attr) in enumerate(self._node.items()):
1181 G.add_node(index + begin_index, **node_attr)
1182 index_of_node[node] = index + begin_index
1183 node_of_index[index + begin_index] = node
1184 for u, nbrs in self._adj.items():
1185 for v, edge_data in nbrs.items():
1186 G.add_edge(index_of_node[u], index_of_node[v], **edge_data)
1187
1188 return G, index_of_node, node_of_index
1189
1190 def cpp(self):
1191 G = DiGraphC()

Callers

nothing calls this directly

Calls 3

updateMethod · 0.80
add_nodeMethod · 0.45
add_edgeMethod · 0.45

Tested by

no test coverage detected