def canonicalize(self, graph): # Relabel nodes for canonicalization original_labels = list(graph.nodes()) original_to_relabeled = {x: i for i, x in enumerate(original_labels)} relabeled_to_original = {i: x for i, x in enumerate(original_labels)} relabeled_graph = nx.relabel_nodes(graph, original_to_relabeled) nauty_graph = Graph(len(relabeled_graph), directed=False) # Adjacency dictionary for relabeled graph adjacency_dict = { n: list(nbrs) for n, nbrs in relabeled_graph.adjacency() } nauty_graph.set_adjacency_dict(adjacency_dict) canonical_labels = canonical_labeling(nauty_graph) # Break ties with nauty canonical_labels = { k: canonical_labels[k] for k in range(len(relabeled_graph)) } # Switch back to original labels canonical_labels = { relabeled_to_original[i]: canonical_labels[i] for i in range(len(relabeled_graph)) } return canonical_labels
def canonicalizes(self, subgraph): st = time.time() #wl_subgraph_normalized=self.wl_normalization(subgraph)['labeled_graph'] #g_relabel=convert_node_labels_to_integers(wl_subgraph_normalized) g_relabel = convert_node_labels_to_integers(subgraph) labeled_graph = nx.Graph(g_relabel) nauty_graph = Graph(len(g_relabel.nodes()), directed=False) nauty_graph.set_adjacency_dict( {n: list(nbrdict) for n, nbrdict in g_relabel.adjacency()}) labels_dict = nx.get_node_attributes(g_relabel, 'labeling') canonical_labeling_dict = { k: canonical_labeling(nauty_graph)[k] for k in range(len(g_relabel.nodes())) } new_ordered_dict = self.rank_label_wrt_dict(g_relabel, labels_dict, canonical_labeling_dict) nx.set_node_attributes(labeled_graph, new_ordered_dict, 'labeling') ed = time.time() self.all_times['canonicalizes'].append(ed - st) return labeled_graph
def nauty_labeling_produce(self,subgraph): relabel_subgraph = nx.convert_node_labels_to_integers(subgraph) nauty_graph = Graph(len(list(relabel_subgraph.nodes)),directed=True) nauty_graph.set_adjacency_dict({n:list(ndict) for n,ndict in relabel_subgraph.adjacency()}) nauty_mapping = dict() subgraph_nodes = list(subgraph.nodes()) nauty_mapping_list = canonical_labeling(nauty_graph) #key subgraph_nodes[i] is node;value nauty_mapping_list[i] is the order in canonical_labeling nauty_mapping = {subgraph_nodes[i]:nauty_mapping_list[i] for i in range(len(nauty_mapping_list))} nx.set_node_attributes(subgraph,nauty_mapping,'nauty_labeling') return subgraph
def nauty_graph_automorphism(graph: nx.Graph): """ Graph canonicalization funtion, meant to break timebreakers of the non-injective ranking function :param graph: subgraph to be canonicalized :return: canonicalized subgraph """ # convert labels to integers to give nauty the node partitions required graph_int_labeled = convert_node_labels_to_integers(graph) canonicalized_graph = nx.Graph(graph_int_labeled) # get canonicalized graph using nauty nauty = Graph(len(graph_int_labeled.nodes()), directed=False) nauty.set_adjacency_dict({node: list(nbr) for node, nbr in graph_int_labeled.adjacency()}) labels_dict = nx.get_node_attributes(graph_int_labeled, 'labeling') canonical_labeling_order = {k: canonical_labeling(nauty)[k] for k in range(len(graph_int_labeled.nodes()))} canonical_order = relabel_graph(graph_int_labeled, labels_dict, canonical_labeling_order) nx.set_node_attributes(canonicalized_graph, canonical_order, 'labeling') return canonicalized_graph