text
stringlengths
0
267k
------
NetworkXError
If the graph is directed or has parallel edges
Examples
--------
>>> G = nx.Graph([(0, 1)])
>>> nx.generate_graph6(G)
'>>graph6<<A_'
See Also
--------
read_graph6, parse_graph6, write_graph6
Notes
-----
The format does not support edge or node labels, parallel edges or
self loops. If self loops are present they are silently ignored.
References
----------
.. [1] Graph6 specification
<http://users.cecs.anu.edu.au/~bdm/data/formats.html>
"""
if nodes is not None:
G = G.subgraph(nodes)
H = nx.convert_node_labels_to_integers(G)
ns = sorted(H.nodes())
def bits():
for (i,j) in [(i,j) for j in range(1,n) for i in range(j)]:
yield G.has_edge(ns[i],ns[j])
n = G.order()
data = n_to_data(n)
d = 0
flush = False
for i, b in zip(range(n * n), bits()):
d |= b << (5 - (i % 6))
flush = True
if i % 6 == 5:
data.append(d)
d = 0
flush = False
if flush:
data.append(d)
string_data = data_to_graph6(data)
if header:
string_data = '>>graph6<<' + string_data
return string_data
@open_file(1, mode='wt')
def write_graph6(G, path, nodes = None, header=True):
"""Write a simple undirected graph to path in graph6 format.
Parameters
----------
G : Graph (undirected)
path : file or string
File or filename to write.
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
Raises
------
NetworkXError
If the graph is directed or has parallel edges
Examples
--------
>>> G = nx.Graph([(0, 1)])
>>> nx.write_graph6(G, 'test.g6')
See Also
--------
generate_graph6, parse_graph6, read_graph6
Notes
-----
The format does not support edge or node labels, parallel edges or
self loops. If self loops are present they are silently ignored.
References
----------
.. [1] Graph6 specification
<http://users.cecs.anu.edu.au/~bdm/data/formats.html>
"""
path.write(generate_graph6(G, nodes=nodes, header=header))
path.write('\n')
# helper functions