text
stringlengths 0
267k
|
---|
generate_graph6, read_graph6, write_graph6 |
References |
---------- |
.. [1] Graph6 specification |
<http://users.cecs.anu.edu.au/~bdm/data/formats.html> |
""" |
def bits(): |
"""Return sequence of individual bits from 6-bit-per-value |
list of data values.""" |
for d in data: |
for i in [5,4,3,2,1,0]: |
yield (d>>i)&1 |
if string.startswith('>>graph6<<'): |
string = string[10:] |
data = graph6_to_data(string) |
n, data = data_to_n(data) |
nd = (n*(n-1)//2 + 5) // 6 |
if len(data) != nd: |
raise NetworkXError(\ |
'Expected %d bits but got %d in graph6' % (n*(n-1)//2, len(data)*6)) |
G=nx.Graph() |
G.add_nodes_from(range(n)) |
for (i,j),b in zip([(i,j) for j in range(1,n) for i in range(j)], bits()): |
if b: |
G.add_edge(i,j) |
return G |
@open_file(0,mode='rt') |
def read_graph6(path): |
"""Read simple undirected graphs in graph6 format from path. |
Parameters |
---------- |
path : file or string |
File or filename to write. |
Returns |
------- |
G : Graph or list of Graphs |
If the file contains multiple lines then a list of graphs is returned |
Raises |
------ |
NetworkXError |
If the string is unable to be parsed in graph6 format |
Examples |
-------- |
>>> nx.write_graph6(nx.Graph([(0,1)]), 'test.g6') |
>>> G = nx.read_graph6('test.g6') |
>>> sorted(G.edges()) |
[(0, 1)] |
See Also |
-------- |
generate_graph6, parse_graph6, write_graph6 |
References |
---------- |
.. [1] Graph6 specification |
<http://users.cecs.anu.edu.au/~bdm/data/formats.html> |
""" |
glist = [] |
for line in path: |
line = line.strip() |
if not len(line): |
continue |
glist.append(parse_graph6(line)) |
if len(glist) == 1: |
return glist[0] |
else: |
return glist |
@not_implemented_for('directed','multigraph') |
def generate_graph6(G, nodes = None, header=True): |
"""Generate graph6 format string from a simple undirected graph. |
Parameters |
---------- |
G : Graph (undirected) |
nodes: list or iterable |
Nodes are labeled 0...n-1 in the order provided. If None the ordering |
given by G.nodes() is used. |
header: bool |
If True add '>>graph6<<' string to head of data |
Returns |
------- |
s : string |
String in graph6 format |
Raises |