K i( dZddlZddlmZddlmZmZddlm Z m Z gdZ dZ ejdd d Zdd Ze dd ejdd dZe de dd ddZy)aPFunctions for reading and writing graphs in the *sparse6* format. The *sparse6* file format is a space-efficient format for large sparse graphs. For small graphs or large dense graphs, use the *graph6* file format. For more information, see the `sparse6`_ homepage. .. _sparse6: https://users.cecs.anu.edu.au/~bdm/data/formats.html N) NetworkXError) data_to_n n_to_data)not_implemented_for open_file)from_sparse6_bytes read_sparse6to_sparse6_bytes write_sparse6c# Kt|}|dk\r td|rddt|D]%}tj t |dz'd d z|kr dz d z|kr fd}t d|jD}g}d }|D]\} } | |k(r)|jd |j|| 4| |dzk(r.|dz }|jd|j|| j| }|jd|j|| |jd |j||  d krV|d zk(rNt| d z k\r<||dz kr4|jd |jdgt| d zzn"|jdgt| d zztd t|d D cgc]I} || d zd z|| dzd zz|| d zdzz|| dzd zz|| d zdzz|| d zd zzK} } | D]%}tj t |dz'dycc} ww)a%Yield bytes in the sparse6 encoding of a graph. `G` is an undirected simple graph. `nodes` is the list of nodes for which the node-induced subgraph will be encoded; if `nodes` is the list of all nodes in the graph, the entire graph will be encoded. `header` is a Boolean that specifies whether to generate the header ``b'>>sparse6<<'`` before the remaining data. This function generates `bytes` objects in the following order: 1. the header (if requested), 2. the encoding of the number of nodes, 3. each character, one-at-a-time, in the encoding of the requested node-induced subgraph, 4. a newline character. This function raises :exc:`ValueError` if the graph is too large for the graph6 format (that is, greater than ``2 ** 36`` nodes). l@z?sparse6 is only defined if number of nodes is less than 2 ** 36 >>sparse6<<:?c`tDcgc]}|ddz |z zzrdndc}Scc}w)zBig endian k-bit encoding of xrr)range)xiks `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/readwrite/sparse6.pyencz$_generate_sparse6_bytes..enc;s4z*_generate_sparse6_bytes..?s%@daC1Is1ay)@s%'r N) len ValueErrorrstrencodechrsortededgesappendextendr)Gnodesheaderndrr+bitscurvrrrdatars @r_generate_sparse6_bytesr6s* AAEz M   J q\&jjQV%%& A q&1* Q q&1*F @aggi@ @E D D  1 9 KKN KKA  $(] AID KKN KKA D KKN KKA  KKN KKA    1uqAvc$iZ1$4#:tq1u~ A QCSYJ!+,- QCSYJ!+,-q#d)Q'   a!e  A;!   A;!   A;!   A;!    A;!    D &jjQV%%& K sA0I64E>I62AI16I6T)graphs returns_graphc2 |jdr|dd}|jds td|ddDcgc]}|dz  }}t|\} d d z|kr dz d z|kr fd}d }tj}|j t |d }|D]I\}} |dk(r|dz }| |k\s||k\rn0| |kDr| }$|j| |rd }|j| |K|stj|}|Scc}w) aVRead 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.from_sparse6_bytes(b":A_") >>> sorted(G.edges()) [(0, 1), (0, 1), (0, 1)] See Also -------- read_sparse6, write_sparse6 References ---------- .. [1] Sparse6 specification r Nrz!Expected leading colon in sparse6rrc36Kt}d}d} |dkr t|}d}|dz}||z dz}|d|zdz z}|}|kr! t|}d}|dz|z}|dz }|kr!||z z }|z }||fg#t$rYywxYw#t$rYywxYww)z6Returns stream of pairs b[i], x[i] for sparse6 format.Nrrr)iternext StopIteration)chunksr2dLenbrxLenr5rs r parseDataz%from_sparse6_bytes..parseDatasd axV A AIDdaAa4i1_%AD(V A!VqL (dQhA!8DQ$J-%%sQB A;!B B B'B; BBBB BBBBrFT) startswithrrnx MultiGraphadd_nodes_fromrhas_edgeadd_edgeGraph) stringccharsr1rCrr. multigraphrArr5rs @@rrris7D(   T "?@@#ABZ (QV (E (GAt A q&1* Q q&1*< A AU1XJ 1 6 FA 6Q!V  UAzz!Q! JJq!    HHQK Hq )s Dc||j|}tj|d}djt |||S)aConvert an undirected graph to bytes in sparse6 format. 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 '>>sparse6<<' bytes to head of data. Raises ------ NetworkXNotImplemented If the graph is directed. ValueError If the graph has at least ``2 ** 36`` nodes; the sparse6 format is only defined for graphs of order less than ``2 ** 36``. Examples -------- >>> nx.to_sparse6_bytes(nx.path_graph(2)) b'>>sparse6<<:An\n' See Also -------- to_sparse6_bytes, read_sparse6, write_sparse6_bytes Notes ----- The returned bytes end with a newline character. The format does not support edge or node labels. References ---------- .. [1] Graph6 specification r*ordering)subgraphrEconvert_node_labels_to_integersjoinr6)r.r/r0s rr r sDX  JJu  **1x@A 88+Auf= >>rRrb)modecg}|D]8}|j}t|s|jt|:t|dk(r|dS|S)aBRead an undirected graph in sparse6 format from path. Parameters ---------- path : file or string Filename or file handle to read. Filenames ending in .gz or .bz2 will be decompressed. Returns ------- G : Graph/Multigraph or list of Graphs/MultiGraphs If the file contains multiple lines then a list of graphs is returned Raises ------ NetworkXError If the string is unable to be parsed in sparse6 format Examples -------- You can read a sparse6 file by giving the path to the file:: >>> import tempfile >>> with tempfile.NamedTemporaryFile(delete=False) as f: ... _ = f.write(b">>sparse6<<:An\n") ... _ = f.seek(0) ... G = nx.read_sparse6(f.name) >>> list(G.edges()) [(0, 1)] You can also read a sparse6 file by giving an open file-like object:: >>> import tempfile >>> with tempfile.NamedTemporaryFile() as f: ... _ = f.write(b">>sparse6<<:An\n") ... _ = f.seek(0) ... G = nx.read_sparse6(f) >>> list(G.edges()) [(0, 1)] See Also -------- read_sparse6, from_sparse6_bytes References ---------- .. [1] Sparse6 specification rr)stripr%r,r)pathglistlines rr r s[j E/zz|4y  '-. /  5zQQx rRdirectedrwbc||j|}tj|d}t|||D]}|j |y)aXWrite graph G to given path in sparse6 format. Parameters ---------- G : Graph (undirected) path : file or string File or filename to write. Filenames ending in .gz or .bz2 will be compressed. 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 '>>sparse6<<' string to head of data Raises ------ NetworkXError If the graph is directed Examples -------- You can write a sparse6 file by giving the path to the file:: >>> import tempfile >>> with tempfile.NamedTemporaryFile(delete=False) as f: ... nx.write_sparse6(nx.path_graph(2), f.name) ... print(f.read()) b'>>sparse6<<:An\n' You can also write a sparse6 file by giving an open file-like object:: >>> with tempfile.NamedTemporaryFile() as f: ... nx.write_sparse6(nx.path_graph(2), f) ... _ = f.seek(0) ... print(f.read()) b'>>sparse6<<:An\n' See Also -------- read_sparse6, from_sparse6_bytes Notes ----- The format does not support edge or node labels. References ---------- .. [1] Sparse6 specification Nr*rP)rSrErTr6write)r.rZr/r0rAs rr r >sNr  JJu  **1x@A $Qv 6 1 rR)NT)__doc__networkxrEnetworkx.exceptionrnetworkx.readwrite.graph6rrnetworkx.utilsrr__all__r6 _dispatchablerr r r rRrris ,:9 UOdT2^ 3^ B/?d 14T2<3<~Z  14;!;rR