§
    bŠtjÊ(  ã                   ó6  — d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 g d¢Zd„ Z ej        dd¬	¦  «        d
„ ¦   «         Zdd„Z e
dd¬¦  «         ej        dd¬	¦  «        d„ ¦   «         ¦   «         Z e	d¦  «         e
dd¬¦  «        dd„¦   «         ¦   «         ZdS )aP  Functions 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_sparse6Úto_sparse6_bytesÚwrite_sparse6c              #   óH  ‡‡K  — t          | ¦  «        }|dk    rt          d¦  «        ‚|rdV — dV — t          |¦  «        D ].}t                               t          |dz   ¦  «        ¦  «        V — Œ/dŠd‰z  |k     r‰dz  Šd‰z  |k     °ˆfd„}t          d„ |                      ¦   «         D ¦   «         ¦  «        }g Šd	}|D ]é\  }}	||k    r4‰                     d	¦  «         ‰ 	                     ||	¦  «        ¦  «         Œ?||dz   k    r9|dz  }‰                     d¦  «         ‰ 	                     ||	¦  «        ¦  «         Œ�|}‰                     d¦  «         ‰ 	                     ||¦  «        ¦  «         ‰                     d	¦  «         ‰ 	                     ||	¦  «        ¦  «         Œê‰d
k     ri|d‰z  k    r`t          ‰¦  «         d
z  ‰k    rI||dz
  k     r@‰                     d	¦  «         ‰ 	                    dgt          ‰¦  «         d
z  z  ¦  «         n*‰ 	                    dgt          ‰¦  «         d
z  z  ¦  «         ˆfd„t          d	t          ‰¦  «        d
¦  «        D ¦   «         }
|
D ].}t                               t          |dz   ¦  «        ¦  «        V — Œ/dV — dS )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                 ó>   •‡ — ˆˆ fd„t          ‰¦  «        D ¦   «         S )zBig endian k-bit encoding of xc                 ó4   •— g | ]}‰d ‰d z
  |z
  z  z  rd nd‘ŒS )r   r   © )Ú.0ÚiÚkÚxs     €€úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/readwrite/sparse6.pyú
<listcomp>z8_generate_sparse6_bytes.<locals>.enc.<locals>.<listcomp>=   s4   ø€ ÐEÐEÐE°q�a˜!  A¡¨¡	Ñ*Ñ*Ð2��°ÐEÐEÐEó    )Úrange)r   r   s   `€r   Úencz$_generate_sparse6_bytes.<locals>.enc;   s'   øø€ àEÐEÐEÐEÐE½EÀ!¹H¼HÐEÑEÔEÐEr   c              3   ó\   K  — | ]'\  }}t          ||¦  «        t          ||¦  «        fV — Œ(d S )N)ÚmaxÚmin)r   ÚuÚvs      r   ú	<genexpr>z*_generate_sparse6_bytes.<locals>.<genexpr>?   s;   è è € Ð@Ð@©d¨a°•C˜˜1‘I”I�s 1 a™yœyÐ)Ð@Ð@Ð@Ð@Ð@Ð@r   r   é   c                 óÂ   •— g | ][}‰|d z            dz  ‰|dz            dz  z   ‰|dz            dz  z   ‰|dz            dz  z   ‰|dz            dz  z   ‰|dz            d z  z   ‘Œ\S )r   é   r   é   é   é   r   )r   r   Úbitss     €r   r   z+_generate_sparse6_bytes.<locals>.<listcomp>Z   s§   ø€ ð ð ð ð ð 
ˆa�!‰eŒ˜Ñ	Ø��A‘Œ;˜!Ññ	à��A‘Œ;˜!Ññ	ð ��A‘Œ;˜!Ññ	ð ��A‘Œ;˜!Ññ		ð
 ��A‘Œ;˜!Ññ	ðð ð r   ó   
N)ÚlenÚ
ValueErrorr   ÚstrÚencodeÚchrÚsortedÚedgesÚappendÚextendr   )ÚGÚnodesÚheaderÚnÚdr   r1   Úcurvr!   r    Údatar)   r   s              @@r   Ú_generate_sparse6_bytesr;      sÿ  øøè è € õ* 	ˆA‰Œ€AØˆE‚z€zÝØMñ
ô 
ð 	
ð ð ØÐÐÐØ
€J€J€JÝ�q‰\Œ\ð &ð &ˆÝ�jŠj�˜Q ™V™œÑ%Ô%Ð%Ð%Ð%Ð%à	€AØ
ˆq‰&�1Š*ˆ*Ø	ˆQ‰ˆð ˆq‰&�1Š*ˆ*ðFð Fð Fð Fð Fõ Ð@Ð@°a·g²g±i´iÐ@Ñ@Ô@Ñ@Ô@€EØ€DØ€DØð  ð  ‰ˆˆ1Ø�Š9ˆ9Ø�KŠK˜‰NŒNˆNØ�KŠK˜˜˜A™œÑÔÐÐØ�$˜‘(Š]ˆ]Ø�A‰IˆDØ�KŠK˜‰NŒNˆNØ�KŠK˜˜˜A™œÑÔÐÐàˆDØ�KŠK˜‰NŒNˆNØ�KŠK˜˜˜A™œÑÔÐØ�KŠK˜‰NŒNˆNØ�KŠK˜˜˜A™œÑÔÐÐØˆ1‚u€u��q˜A‘v’�¥c¨$¡i¤i Z°1Ñ$4¸Ò#:Ð#:¸tÀqÈ1Áuº~¸~ð
 	�Š�A‰ŒˆØ�Š�Q�C�S ™YœY˜J¨!Ñ+Ñ,Ñ-Ô-Ð-Ð-à�Š�Q�C�S ™YœY˜J¨!Ñ+Ñ,Ñ-Ô-Ð-ðð ð ð õ �q�#˜d™)œ) QÑ'Ô'ðñ ô €Dð ð &ð &ˆÝ�jŠj�˜Q ™V™œÑ%Ô%Ð%Ð%Ð%Ð%Ø
€K€K€K€K€Kr   T)ÚgraphsÚreturns_graphc                 óx  ‡	‡
— |                       d¦  «        r
| dd…         } |                       d¦  «        st          d¦  «        ‚d„ | dd…         D ¦   «         }t          |¦  «        \  }Š	dŠ
d‰
z  |k     r‰
dz  Š
d‰
z  |k     °ˆ	ˆ
fd„}d	}t          j        ¦   «         }|                     t          |¦  «        ¦  «         d
} |¦   «         D ]U\  }}|dk    r|dz  }||k    s||k    r n8||k    r|}Œ'|                     ||¦  «        rd}|                     ||¦  «         ŒV|st          j	        |¦  «        }|S )aV  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.from_sparse6_bytes(b":A_")
    >>> sorted(G.edges())
    [(0, 1), (0, 1), (0, 1)]

    See Also
    --------
    read_sparse6, write_sparse6

    References
    ----------
    .. [1] Sparse6 specification
           <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

    r   é   Nr   z!Expected leading colon in sparse6c                 ó   — g | ]}|d z
  ‘ŒS )r   r   )r   Úcs     r   r   z&from_sparse6_bytes.<locals>.<listcomp>�   s   € Ð(Ð(Ð(˜ˆQ�‰VÐ(Ð(Ð(r   r   c               3   óZ  •K  — t          ‰¦  «        } d}d}	 |dk     r$	 t          | ¦  «        }n# t          $ r Y dS w xY wd}|dz  }||z	  dz  }|d|z  dz
  z  }|}|‰k     r7	 t          | ¦  «        }n# t          $ r Y dS w xY wd}|dz  |z   }|dz  }|‰k     °7||‰z
  z	  }|‰z
  }||fV — Œ•)z6Returns stream of pairs b[i], x[i] for sparse6 format.Nr   r   r#   )ÚiterÚnextÚStopIteration)Úchunksr8   ÚdLenÚbr   ÚxLenr:   r   s         €€r   Ú	parseDataz%from_sparse6_bytes.<locals>.parseData–   s  øè è € å�d‘”ˆØˆØˆð	Ø�aŠxˆxðÝ˜V™œ�A�AøÝ$ð ð ð Ø�F�Fðøøøà�Ø�A‰IˆDØ�d‘˜a‘ˆAà�a˜4‘i 1‘_Ñ%ˆAØˆDØ˜’(�(ðÝ˜V™œ�A�AøÝ$ð ð ð Ø�F�Fðøøøà�Ø˜!‘V˜q‘L�Ø˜‘	�ð ˜’(�(ð �d˜Q‘h‘ˆAØ˜!‘8ˆDØ�Q�$ˆJˆJˆJð-	s   Ÿ/ ¯
=¼=Á#A3 Á3
BÂ Br   FT)
Ú
startswithr   r   ÚnxÚ
MultiGraphÚadd_nodes_fromr   Úhas_edgeÚadd_edgeÚGraph)ÚstringÚcharsr7   rJ   r!   r4   Ú
multigraphrH   r   r:   r   s            @@r   r   r   i   s’  øø€ ðD ×Ò˜Ñ(Ô(ð Ø˜˜˜”ˆØ×Ò˜TÑ"Ô"ð AÝÐ?Ñ@Ô@Ð@à(Ð(˜V A B BœZÐ(Ñ(Ô(€EÝ˜ÑÔ�G€A€tØ	€AØ
ˆq‰&�1Š*ˆ*Ø	ˆQ‰ˆð ˆq‰&�1Š*ˆ*ðð ð ð ð ð ð< 	
€Aå
Œ‰Œ€AØ×Ò•U˜1‘X”XÑÔÐà€JØ�	‘”ð ð ‰ˆˆ1Ø�Š6ˆ6Ø�‰FˆAà�Š6ˆ6�Q˜!’V�VØˆEØ�ŠUˆUØˆAˆAà�zŠz˜!˜QÑÔð "Ø!�
Ø�JŠJ�q˜!ÑÔÐÐØð ÝŒH�Q‰KŒKˆØ€Hr   c                 ó¤   — |�|                       |¦  «        } t          j        | d¬¦  «        } d                     t	          | ||¦  «        ¦  «        S )a÷  Convert 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
           <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

    Nr0   ©Úorderingr   )ÚsubgraphrL   Úconvert_node_labels_to_integersÚjoinr;   )r4   r5   r6   s      r   r
   r
   Ë   sP   € ðX ÐØ�JŠJ�uÑÔˆÝ
Ô*¨1°xÐ@Ñ@Ô@€AØ�8Š8Õ+¨A¨u°fÑ=Ô=Ñ>Ô>Ð>r   Úrb)Úmodec                 óÖ   — g }| D ]H}|                      ¦   «         }t          |¦  «        sŒ&|                     t          |¦  «        ¦  «         ŒIt          |¦  «        dk    r|d         S |S )aB  Read 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
           <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

    r   r   )Ústripr+   r2   r   )ÚpathÚglistÚlines      r   r	   r	   ý   sr   € ðj €EØð /ð /ˆØ�zŠz‰|Œ|ˆÝ�4‰yŒyð 	ØØ�ŠÕ'¨Ñ-Ô-Ñ.Ô.Ð.Ð.Ý
ˆ5�z„z�Q‚€Ø�QŒxˆàˆr   Údirectedr   Úwbc                 ó²   — |�|                       |¦  «        } t          j        | d¬¦  «        } t          | ||¦  «        D ]}|                     |¦  «         ŒdS )aX  Write 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
           <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

    Nr0   rV   )rX   rL   rY   r;   Úwrite)r4   r_   r5   r6   rH   s        r   r   r   >  sg   € ðr ÐØ�JŠJ�uÑÔˆÝ
Ô*¨1°xÐ@Ñ@Ô@€AÝ$ Q¨¨vÑ6Ô6ð ð ˆØ�
Š
�1‰Œˆˆðð r   )NT)Ú__doc__ÚnetworkxrL   Únetworkx.exceptionr   Únetworkx.readwrite.graph6r   r   Únetworkx.utilsr   r   Ú__all__r;   Ú_dispatchabler   r
   r	   r   r   r   r   ú<module>rm      s]  ðð
ð 
ð Ð Ð Ð Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9à
UÐ
UÐ
U€ðOð Oð Oðd €Ô˜¨TÐ2Ñ2Ô2ð^ð ^ñ 3Ô2ð^ðB/?ð /?ð /?ð /?ðd €ˆ1�4ÐÑÔØ€Ô˜¨TÐ2Ñ2Ô2ð<ð <ñ 3Ô2ñ Ôð<ð~ Ð�ZÑ Ô Ø
€ˆ1�4ÐÑÔð;ð ;ð ;ñ Ôñ !Ô ð;ð ;ð ;r   