
    wg%,                         d Z g dZddlZddlmZ ddZ edd      dd	       Z ej                  dd
      	 dd       Z	 edd       ej                  dd
      	 	 	 	 	 	 dd              Z
y)a  
*************************
Multi-line Adjacency List
*************************
Read and write NetworkX graphs as multi-line adjacency lists.

The multi-line adjacency list format is useful for graphs with
nodes that can be meaningfully represented as strings.  With this format
simple edge data can be stored but node or graph data is not.

Format
------
The first label in a line is the source node label followed by the node degree
d.  The next d lines are target node labels and optional edge data.
That pattern repeats for all nodes in the graph.

The graph with edges a-b, a-c, d-e can be represented as the following
adjacency list (anything following the # in a line is a comment)::

     # example.multiline-adjlist
     a 2
     b
     c
     d 1
     e
)generate_multiline_adjlistwrite_multiline_adjlistparse_multiline_adjlistread_multiline_adjlist    N)	open_filec           
   #   *  K   | j                         rJ| j                         r| j                         D ]  \  }}|j                         D cg c]!  \  }}|j                         D ]	  \  }}||f # }}}}}t	        |      }	t        |      |z   t        |	      z    |D ]1  \  }}
|
t        |       t        |      |z   t        |
      z    3  y| j                         D ]p  \  }}t	        |      }	t        |      |z   t        |	      z    |j                         D ]1  \  }}
|
t        |       t        |      |z   t        |
      z    3 r y| j                         rt               }| j                         D ]  \  }}|j                         D cg c]%  \  }}||vr|j                         D ]	  \  }}||f ' }}}}}t	        |      }	t        |      |z   t        |	      z    |D ]1  \  }}
|
t        |       t        |      |z   t        |
      z    3 |j                  |        yt               }| j                         D ]  \  }}|j                         D 
cg c]  \  }}
||vs||
f }}}
t	        |      }	t        |      |z   t        |	      z    |D ]1  \  }}
|
t        |       t        |      |z   t        |
      z    3 |j                  |        yc c}}}}w c c}}}}w c c}
}w w)a  Generate a single line of the graph G in multiline adjacency list format.

    Parameters
    ----------
    G : NetworkX graph

    delimiter : string, optional
       Separator for node labels

    Returns
    -------
    lines : string
        Lines of data in multiline adjlist format.

    Examples
    --------
    >>> G = nx.lollipop_graph(4, 3)
    >>> for line in nx.generate_multiline_adjlist(G):
    ...     print(line)
    0 3
    1 {}
    2 {}
    3 {}
    1 2
    2 {}
    3 {}
    2 1
    3 {}
    3 1
    4 {}
    4 1
    5 {}
    5 1
    6 {}
    6 0

    See Also
    --------
    write_multiline_adjlist, read_multiline_adjlist
    N)is_directedis_multigraph	adjacencyitemslenstrsetadd)G	delimitersnbrsudatadictkeydata	nbr_edgesdegdseens               i/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/readwrite/multiline_adjlist.pyr   r   '   s    R 	}}??;;= :4 (,zz| #8%-^^%5 "T I	 
 )n!fy(3s833% :DAqy!!f!!fy03q699	:: ;;= :4$i!fy(3s833 JJL :DAqy!!f!!fy03q699	:: ??5D;;= 4 (,zz| #8}%-^^%5	 "T I	  )n!fy(3s833% :DAqy!!f!!fy03q699	:
   5D;;= 	404

N1aVN	N)n!fy(3s833% :DAqy!!f!!fy03q699	:
 	S2" Os3   AL&K=5D.L#*LB)L6LL	B
L   wb)modec                 |   ddl }ddl}|dj                  |j                        z   }| d|z   d|j	                  |j                                dz   |z   d| j                   dz   }|j                  |j                  |             t        | |      D ]'  }	|	dz  }	|j                  |	j                  |             ) y)a  Write the graph G in multiline adjacency list format to path

    Parameters
    ----------
    G : NetworkX graph

    path : string or file
       Filename or file handle to write to.
       Filenames ending in .gz or .bz2 will be compressed.

    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels

    encoding : string, optional
       Text encoding.

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> nx.write_multiline_adjlist(G, "test.adjlist")

    The path can be a file handle or a string with the name of the file. If a
    file handle is provided, it has to be opened in 'wb' mode.

    >>> fh = open("test.adjlist", "wb")
    >>> nx.write_multiline_adjlist(G, fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_multiline_adjlist(G, "test.adjlist.gz")

    See Also
    --------
    read_multiline_adjlist
    r   N 
z GMT )
systimejoinargvasctimegmtimenamewriteencoder   )
r   pathr   commentsencodingr$   r%   pargsheader	multilines
             r   r   r      s    P sxx))E'
	$,,t{{}-.b
1	2 	 affXR.		  	JJv}}X&'/9= /	T	

9##H-./    T)graphsreturns_graphc           	         ddl m} t        j                  d|      }| D ]1  }|j	                  |      }	|	dk\  r|d|	 }|s"	 |j                  d      j                  |      \  }
}t        |      }|		  ||
      }
|j                  |
       t        |      D ]  }	 	 t        |       }|j	                  |      }	|	dk\  r|d|	 }|rn+|j                  d      j                  |      }t        |      }|d
k  r`|j                  d      }dj!                  |      }|		  ||      }|	 d ||      i}n		  ||      } |j"                  |
|fi |  4 |S # t        $ r}t        d| d      |d}~ww xY w# t        $ r}t        d|
 d|       |d}~ww xY w# t        $ r}d	|
 d}t        |      |d}~ww xY w# t        $ r}t        d| d|       |d}~ww xY w# t        $ r}t        d| d|       |d}~ww xY w#  i }Y xY w)aB  Parse lines of a multiline adjacency list representation of a graph.

    Parameters
    ----------
    lines : list or iterator of strings
        Input data in multiline adjlist format

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    nodetype : Python type, optional
       Convert nodes to this type.

    edgetype : Python type, optional
       Convert edges to this type.

    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels.  The default is whitespace.

    Returns
    -------
    G: NetworkX graph
        The graph corresponding to the lines in multiline adjacency list format.

    Examples
    --------
    >>> lines = [
    ...     "1 2",
    ...     "2 {'weight':3, 'name': 'Frodo'}",
    ...     "3 {}",
    ...     "2 1",
    ...     "5 {'weight':6, 'name': 'Saruman'}",
    ... ]
    >>> G = nx.parse_multiline_adjlist(iter(lines), nodetype=int)
    >>> list(G)
    [1, 2, 3, 5]

    r   )literal_evalNr#   z(Failed to read node and degree on line ()zFailed to convert node (z
) to type z"Failed to find neighbor for node (r    weightzFailed to convert edge data ()astr7   nxempty_graphfindrstripsplitintBaseException	TypeErroradd_noderangenextStopIterationr   popr&   add_edge)linesr.   r   create_usingnodetypeedgetyper7   r   linepr   r   errimsgvlistnumbvr   edgedatas                       r   r   r      s   Z !
q,'A 8)IIh68D	Y{{4(..y9HQc(C QK
 	


1s %	)A2;D IIh'68D  KK%++I6Eu:Dax		!A775>D# A
 # ((4.9H"+D1H AJJq!(x(K%	)'8)t Hc  	YFtfANOUXX	Y
 ! .qcH:F % 2>qcCC#C.c12" % #21#ZzJ % #7vZzR"!Hs   .E3E7FF='
G 3H	E4E//E47	F FF	F:#F55F:=	GGG 	H )G;;H H	rbc                 <    fd| D        }t        ||||||      S )a  Read graph in multi-line adjacency list format from path.

    Parameters
    ----------
    path : string or file
       Filename or file handle to read.
       Filenames ending in .gz or .bz2 will be uncompressed.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    nodetype : Python type, optional
       Convert nodes to this type.

    edgetype : Python type, optional
       Convert edge data to this type.

    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels.  The default is whitespace.

    Returns
    -------
    G: NetworkX graph

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> nx.write_multiline_adjlist(G, "test.adjlist")
    >>> G = nx.read_multiline_adjlist("test.adjlist")

    The path can be a file or a string with the name of the file. If a
    file s provided, it has to be opened in 'rb' mode.

    >>> fh = open("test.adjlist", "rb")
    >>> G = nx.read_multiline_adjlist(fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_multiline_adjlist(G, "test.adjlist.gz")
    >>> G = nx.read_multiline_adjlist("test.adjlist.gz")

    The optional nodetype is a function to convert node strings to nodetype.

    For example

    >>> G = nx.read_multiline_adjlist("test.adjlist", nodetype=int)

    will attempt to convert all nodes to integer type.

    The optional edgetype is a function to convert edge data strings to
    edgetype.

    >>> G = nx.read_multiline_adjlist("test.adjlist")

    The optional create_using parameter is a NetworkX graph container.
    The default is Graph(), an undirected graph.  To read the data as
    a directed graph use

    >>> G = nx.read_multiline_adjlist("test.adjlist", create_using=nx.DiGraph)

    Notes
    -----
    This format does not store graph, node, or edge data.

    See Also
    --------
    write_multiline_adjlist
    c              3   @   K   | ]  }|j                          y w)N)decode).0rN   r/   s     r   	<genexpr>z)read_multiline_adjlist.<locals>.<genexpr>  s     4tT[["4s   )r.   r   rK   rL   rM   )r   )r-   r.   r   rK   rL   rM   r/   rJ   s         ` r   r   r   /  s/    d 5t4E"! r3   )r"   )r"   #utf-8)r]   NNNN)r]   NNNNr^   )__doc____all__networkxr<   networkx.utilsr   r   r   _dispatchabler   r    r3   r   <module>re      s   6  $^B 146/ 6/r T2TXi 3iX 14T2 X 3 Xr3   