diff --git a/src/esu.py b/src/esu.py index 7e7d7b8..2368f1b 100644 --- a/src/esu.py +++ b/src/esu.py @@ -87,3 +87,6 @@ def get_right_neighbors(self, node): def get_subgraph_list(self): return self.subgraph_list + + def number_of_subgraphs(self): + return len(self.subgraph.list) diff --git a/src/graph_utils.py b/src/graph_utils.py index 795d7f5..300ca03 100644 --- a/src/graph_utils.py +++ b/src/graph_utils.py @@ -18,6 +18,7 @@ import src.labeling as labeling import subprocess from src.types import GraphType +from collections import defaultdict class Graph: @@ -153,10 +154,13 @@ def print_labelg(self): if not os.path.exists(output_dir): os.makedirs(output_dir) + label_counter = defaultdict(int) + # Convert to graph6 type with open(labels_file_output, "w") as file: for subgraph in self.esu.get_subgraph_list(): label = labeling.get_graph_label(subgraph, self.graph_type) + label_counter[label] += 1 label = label + "\n" file.writelines(label) @@ -174,16 +178,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", @@ -207,9 +208,11 @@ def print_labelg(self): st.error(result.stderr) # after running all the inputs through labelg program, display the entire file - with open(labelg_output_file, "r") as labelg_file: - st.subheader("Labelg Output") - st.text(labelg_file.read()) + # with open(labelg_output_file, "r") as labelg_file: + st.subheader("Labelg Output") + print(label_counter) + for label, count in label_counter.items(): + st.text(f"{label}: {count}") except subprocess.CalledProcessError as e: st.write("error running labelg:") diff --git a/src/labeling.py b/src/labeling.py index 7d242a9..64e5a30 100644 --- a/src/labeling.py +++ b/src/labeling.py @@ -5,22 +5,42 @@ 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()) + n = len(vertices) + + 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) + adj_matrix = [[0 for _ in range(n)] for _ in range(n)] + for r in range(len(adj_matrix)): + for c in range(len(adj_matrix[r])): + if graph.has_edge(vertices[r], vertices[c]): + adj_matrix[r][c] = 1 + for col in range(len(adj_matrix[0])): + for row in range(col): + bit_vector.append(adj_matrix[row][col]) # Step 3: Pad bit vector with zeros to make its length a multiple of 6 while len(bit_vector) % 6 != 0: bit_vector.append(0) + print(bit_vector) # Step 4: Convert each group of 6 bits into an ASCII character for encoding R = "" @@ -34,18 +54,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 +93,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: