def parse_sparse6(string): """Read an undirected graph in sparse6 format from string. Parameters ---------- string : string Data in sparse6 format Returns ------- G : Graph Raises ------ NetworkXError If the string is unable to be parsed in sparse6 format Examples -------- >>> G = nx.parse_sparse6(':A_') >>> sorted(G.edges()) [(0, 1), (0, 1), (0, 1)] See Also -------- generate_sparse6, read_sparse6, write_sparse6 References ---------- Sparse6 specification: http://cs.anu.edu.au/~bdm/data/formats.txt """ if string.startswith('>>sparse6<<'): string = string[11:] if not string.startswith(':'): raise NetworkXError('Expected leading colon in sparse6') n, data = data_to_n(graph6_to_data(string[1:])) k = 1 while 1 << k < n: k += 1 def parseData(): """Return stream of pairs b[i], x[i] for sparse6 format.""" chunks = iter(data) d = None # partial data word dLen = 0 # how many unparsed bits are left in d while 1: if dLen < 1: d = next(chunks) dLen = 6 dLen -= 1 b = (d >> dLen) & 1 # grab top remaining bit x = d & ((1 << dLen) - 1) # partially built up value of x xLen = dLen # how many bits included so far in x while xLen < k: # now grab full chunks until we have enough d = next(chunks) dLen = 6 x = (x << 6) + d xLen += 6 x = (x >> (xLen - k)) # shift back the extra bits dLen = xLen - k yield b, x v = 0 G = nx.MultiGraph() G.add_nodes_from(range(n)) multigraph = False for b, x in parseData(): if b == 1: v += 1 # padding with ones can cause overlarge number here if x >= n or v >= n: break elif x > v: v = x else: if G.has_edge(x, v): multigraph = True G.add_edge(x, v) if not multigraph: G = nx.Graph(G) return G
def parse_sparse6(string): """Read an undirected graph in sparse6 format from string. Parameters ---------- string : string Data in sparse6 format Returns ------- G : Graph Raises ------ NetworkXError If the string is unable to be parsed in sparse6 format Examples -------- >>> G = nx.parse_sparse6(':A_') >>> sorted(G.edges()) [(0, 1), (0, 1), (0, 1)] See Also -------- generate_sparse6, read_sparse6, write_sparse6 References ---------- Sparse6 specification: http://cs.anu.edu.au/~bdm/data/formats.txt """ if string.startswith('>>sparse6<<'): string = string[11:] if not string.startswith(':'): raise NetworkXError('Expected leading colon in sparse6') n, data = data_to_n(graph6_to_data(string[1:])) k = 1 while 1<<k < n: k += 1 def parseData(): """Return stream of pairs b[i], x[i] for sparse6 format.""" chunks = iter(data) d = None # partial data word dLen = 0 # how many unparsed bits are left in d while 1: if dLen < 1: d = next(chunks) dLen = 6 dLen -= 1 b = (d>>dLen) & 1 # grab top remaining bit x = d & ((1<<dLen)-1) # partially built up value of x xLen = dLen # how many bits included so far in x while xLen < k: # now grab full chunks until we have enough d = next(chunks) dLen = 6 x = (x<<6) + d xLen += 6 x = (x >> (xLen - k)) # shift back the extra bits dLen = xLen - k yield b,x v = 0 G = nx.MultiGraph() G.add_nodes_from(range(n)) multigraph = False for b,x in parseData(): if b == 1: v += 1 # padding with ones can cause overlarge number here if x >= n or v >= n: break elif x > v: v = x else: if G.has_edge(x,v): multigraph = True G.add_edge(x,v) if not multigraph: G = nx.Graph(G) return G
def test_data_sparse6_data_conversion(self): for data in [[], [0], [63], [63, 63], [0] * 42, [0, 1, 62, 42, 3, 11, 0, 11]]: assert_equal(g6.graph6_to_data(g6.data_to_graph6(data)), data) assert_equal(len(g6.data_to_graph6(data)), len(data))
def test_data_sparse6_data_conversion(self): for data in [[], [0], [63], [63, 63], [0]*42, [0, 1, 62, 42, 3, 11, 0, 11]]: assert_equal(g6.graph6_to_data(g6.data_to_graph6(data)), data) assert_equal(len(g6.data_to_graph6(data)), len(data))