Skip to content

Commit

Permalink
fix: graph6 and digraph6 labelings
Browse files Browse the repository at this point in the history
Before, we were iterating through list of vertices incorrectly
  • Loading branch information
hanpham32 committed Dec 12, 2024
1 parent 7b93dcb commit 93e8407
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 42 additions & 9 deletions src/labeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 93e8407

Please sign in to comment.