0

I would like to convert all my .dimacs graphs to .g6. I wrote a Python script to do this, but when I open my graph.g6, I realize that the string is not in the correct format. How can I properly convert it to the g6 format please?

For example, the content of my graph.g6 looks like this :[u?GOO??GH?K?_??_OO??@?C?@O??_?@?_O@G??Q???_?@O?@?_C?O?_I?_??O?W

import networkx as nx


def load_graph_from_dimacs(filename):
    """
    Charge un graphe depuis un fichier DIMACS.
    """
    graph = nx.Graph()

    with open(filename, "r") as f:
        for line in f:
            line = line.strip()
            if line.startswith("c") or line == "":
                continue
            if line.startswith("p"):
                parts = line.split()
                num_nodes = int(parts[2])
                graph.add_nodes_from(range(num_nodes))
            elif line.startswith("e"):
                parts = line.split()
                u, v = int(parts[1]) - 1, int(parts[2]) - 1
                graph.add_edge(u, v)

    return graph


def save_graph_as_g6(graph, output_file):
    """
    Sauvegarde un graphe en format Graph6 et affiche la chaîne générée.
    """
    g6_string = nx.to_graph6_bytes(graph, header=False).decode() 
    print("Graph6 généré :", g6_string)

    with open(output_file, "w") as f:
        f.write(g6_string)


if __name__ == "__main__":
    filename = "dimacs_files/regular/regular_28_16_1.dimacs"
    output_file = "graph.g6"

    graph = load_graph_from_dimacs(filename)

    print(f"Nombre de sommets : {graph.number_of_nodes()}")
    print(f"Nombre d'arêtes : {graph.number_of_edges()}")

    save_graph_as_g6(graph, output_file)

    print(f"Conversion réussie : {filename} → {output_file}")

Dimacs file :

p edge 50 49
e 1 29
e 1 30
e 1 11
e 2 10
e 2 19
e 3 37
e 4 41
e 5 45
e 5 42
e 6 15
e 6 39
e 7 20
e 7 37
e 7 9
e 8 28
e 9 22
e 9 44
e 9 23
e 10 32
e 11 27
e 12 36
e 13 19
e 14 23
e 15 30
e 16 38
e 16 44
e 17 42
e 17 31
e 18 43
e 19 46
e 20 47
e 21 34
e 22 28
e 22 29
e 24 28
e 25 42
e 25 50
e 26 34
e 26 49
e 26 36
e 29 48
e 31 35
e 32 48
e 33 42
e 34 41
e 34 43
e 35 47
e 40 43
e 41 48

I tried to convert a graph in .dimacs format to a .g6 format using a Python script with NetworkX. The script works by first reading the .dimacs file, constructing a graph from it, and then writing that graph to a .g6 file using NetworkX's write_graph6() function.

My expectation was that the .g6 file would contain a correctly formatted string that represents the graph in Graph6 format. This format should be a short, readable string, which can be used with graph libraries or visualized correctly.

However, what actually happened was that the resulting .g6 file contained an unreadable string with strange characters like [u?GOO??GH?K?_??_OO??@?C?@O??_?@?_O@G??Q???_?@O?@?_C?O?_I?_??O?W. This suggests that there is some encoding or conversion issue during the writing process. The string is not in the expected Graph6 format, and it seems corrupted or improperly encoded.

4
  • Try write_graph6() Commented Feb 26 at 15:35
  • Hello JonSG, What do you mean ? Where I need to try this function ? Commented Feb 26 at 18:00
  • 1
    Because I tested this suggestion but i have the same problem pastebin.com/2Ume61Hz Commented Feb 26 at 18:06
  • Bummer. I would edit the question and include your pastebin code and the result it produced as well as the result you seek then. Sorry that suggestion did not help. Commented Feb 26 at 19:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.