问题
I want to generate all 3-regular graphs with given number of vertices to check if some property applies to all of them or not. checking the property is easy but first I have to generate the graphs efficiently.
Can somebody please help me Generate these graphs (as adjacency matrix) or give me a file containing such graphs. Number of vertices are less than 24.
Thank you
回答1:
It is possible to read scd file into python as binary and convert data in same way it is done in readscd.c file. Here is an example:
import numpy
def convert(filename, n, k=3):
num_edges = n*k/2
f = open(filename, "r")
values = numpy.fromfile(f, dtype=numpy.uint8)
read_values = 0
code = []
while read_values < len(values):
# dekomp(file,code)
samebits = values.item(read_values)
read_values += 1
readbits = num_edges - samebits
code = code[:samebits] + list(values[read_values:read_values+readbits])
read_values += readbits
# codetonlist(code,l)
graph = numpy.zeros((n, n), dtype=numpy.uint8)
v = 0
count = [0] * n
for w in code:
w -= 1 # We are indexing from 0
while(count[v] == k):
v += 1
# edge (v, w)
graph.itemset((v, w), 1)
graph.itemset((w, v), 1)
count[v] += 1
count[w] += 1
yield graph
if __name__ == '__main__':
import sys
filename = sys.argv[1]
nk = filename.split('.')[0].split('_')
for g in convert(filename, int(nk[0]), int(nk[1])):
print g
File 18_3_3.scd is processed in few seconds. Printing took few minutes.
来源:https://stackoverflow.com/questions/41355405/how-to-generate-all-3-regular-graphs-given-number-of-vertices-in-python