如何在python中给定顶点数的情况下生成所有3个正则图 [英] how to generate all 3 regular graphs given number of vertices in python
问题描述
我想生成具有给定顶点数的所有3个正则图,以检查是否有某些属性适用于所有顶点.检查该属性很容易,但是首先我必须高效地生成图形.
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.
有人可以帮助我生成这些图(作为邻接矩阵)还是给我一个包含这些图的文件.顶点数少于24.
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.
谢谢
推荐答案
可以将scd文件作为二进制读入python,并以与readscd.c文件相同的方式转换数据.这是一个示例:
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
文件18_3_3.scd在几秒钟内被处理.打印花了几分钟.
File 18_3_3.scd is processed in few seconds. Printing took few minutes.
这篇关于如何在python中给定顶点数的情况下生成所有3个正则图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!