Ask Your Question
0

How to save a list of graphs into a graph6 format file?

asked 2024-01-05 08:42:53 +0100

licheng gravatar image

updated 2024-01-05 08:44:03 +0100

The following is the code for finding connected graphs with 8 vertices, 10 edges, and a matching number of 3.

gen = graphs.nauty_geng("8 10 -c ")
L=[]
for g in gen:
    if len(g.matching()) ==3:
       L.append(g)

I want to save these graphs in Graph6 format into a file. I know Mathematica has a convenient command for this.

Export["somefile.g6",graphList,"Graph6"]

Does SageMath have a similar command? I can currently only write it like following. I wonder if there is a simpler solution.

file_path = "matching_graphs.g6"
with open(file_path, 'w') as file:
    for G in L:
        file.write(G.graph6_string() + '\n')
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-01-05 13:36:13 +0100

rburing gravatar image

That's already pretty short. Here is an alternative one-liner using graphs_list.to_graph6 and print:

sage: print(graphs_list.to_graph6(L), file=open('matching_graphs.g6', 'w'), end='')

This builds a big string in memory though, so it wouldn't be suitable for gigantic lists.

We could also mimic Mathematica by defining a somewhat generic export function:

def export(filename, object_list, method_name):
    with open(filename, 'w') as f:
        for obj in object_list:
            f.write(attrcall(method_name)(obj) + '\n')

Then we can do:

sage: export('matching_graphs.g6', L, 'graph6_string')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-01-05 08:42:53 +0100

Seen: 419 times

Last updated: Jan 05