From 93e840746b1ecce48ab9151bbbf1b08efb3222fb Mon Sep 17 00:00:00 2001 From: hanpham32 Date: Thu, 12 Dec 2024 00:51:32 -0800 Subject: [PATCH] fix: graph6 and digraph6 labelings Before, we were iterating through list of vertices incorrectly --- src/graph_utils.py | 7 ++----- src/labeling.py | 51 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/graph_utils.py b/src/graph_utils.py index 795d7f5..5dc42c9 100644 --- a/src/graph_utils.py +++ b/src/graph_utils.py @@ -174,16 +174,13 @@ def print_labelg(self): with open(labels_file_output, "r") as file: # Clear previous contents of the labelg output file - labelg_output_file = os.path.join( - output_dir, "labelg_output.txt" - ) + labelg_output_file = os.path.join(output_dir, "labelg_output.txt") with open(labelg_output_file, "w") as labelg_file: labelg_file.write("") for line in file: line = line.strip() - if self.graph_type == GraphType.DIRECTED: - line = "&" + line + result = subprocess.run( [label_g], input=line + "\n", diff --git a/src/labeling.py b/src/labeling.py index 7d242a9..8169c5e 100644 --- a/src/labeling.py +++ b/src/labeling.py @@ -5,18 +5,34 @@ def graph6(graph: nx.Graph) -> str: """ Convert a subgraph into graph6 format. + + Parameters: + graph (nx.Graph): A NetworkX graph. + + Returns: + str: The graph6 encoded string. """ # Step 1: Compute N(n), the graph size character - graph_size = graph.order() - N = chr(graph_size + 63) + graph_size = graph.order() # number of nodes in the graph + vertices = list(graph.nodes()) + + if graph_size == 0: + return "" # empty graph + elif graph_size == 1: + return "" # single-node graph + + N = chr(graph_size + 63) # add 63 to graph_size # Step 2: Compute R(x). Create bit vector from the upper triangle of the # adjacency matrix # For undirected: read upper triangle of the matrix, column by column bit_vector = [] - for c in range(graph_size): - for r in range(c): - bit_vector.append(1 if graph.has_edge(r, c) else 0) + for r in vertices: + for c in vertices: + if (graph.has_edge(r, c)): + bit_vector.append(1) + else: + bit_vector.append(0) # Step 3: Pad bit vector with zeros to make its length a multiple of 6 while len(bit_vector) % 6 != 0: @@ -34,18 +50,34 @@ def graph6(graph: nx.Graph) -> str: def digraph6(graph: nx.DiGraph) -> str: """ Convert a directed subgraph into digraph6 format. + + Parameters: + graph (nx.Graph): A NetworkX graph. + + Returns: + str: The digraph6 encoded string. """ # Step 1: Compute N(n), the graph size character graph_size = graph.order() + vertices = list(graph.nodes) + + if graph_size == 0: + return "" # empty graph + elif graph_size == 1: + return "" # single-node graph + N = chr(graph_size + 63) # Step 2: Compute R(x). Create bit vector from the upper triangle of the # adjacency matrix # For directed: read the matrix row by row bit_vector = [] - for r in range(graph_size): - for c in range(graph_size): - bit_vector.append(1 if graph.has_edge(r, c) else 0) + for r in vertices: + for c in vertices: + if (graph.has_edge(r, c)): + bit_vector.append(1) + else: + bit_vector.append(0) # Step 3: Pad bit vector with zeros to make its length a multiple of 6 while len(bit_vector) % 6 != 0: @@ -57,8 +89,9 @@ def digraph6(graph: nx.DiGraph) -> str: group = bit_vector[i : i + 6] group_value = sum((bit << (5 - idx)) for idx, bit in enumerate(group)) R += chr(group_value + 63) + print(R) - return N + R + return chr(38) + N + R def get_graph_label(nx_graph: nx.Graph, graph_type: GraphType) -> str: