
    h7                        S r / SQrSSKrSSKJr  SS jr\" SSS	9SS
 j5       r\R                  " SSS9 SS j5       r	\" SSS	9\R                  " SSS9       SS j5       5       r
SS jr\R                  " SSS9     SS j5       rg)aM  
**********
Edge Lists
**********
Read and write NetworkX graphs as edge lists.

The multi-line adjacency list format is useful for graphs with nodes
that can be meaningfully represented as strings.  With the edgelist
format simple edge data can be stored but node or graph data is not.
There is no way of representing isolated nodes unless the node has a
self-loop edge.

Format
------
You can read or write three formats of edge lists with these functions.

Node pairs with no data::

 1 2

Python dictionary as data::

 1 2 {'weight':7, 'color':'green'}

Arbitrary data::

 1 2 7 green
)generate_edgelistwrite_edgelistparse_edgelistread_edgelistread_weighted_edgelistwrite_weighted_edgelist    N)	open_fileTc              #     ^#    USL aH  U R                  SS9 H4  u  p4mX4[        T5      4nUR                  [        [        U5      5      v   M6     gUSL a=  U R                  SS9 H)  u  p4X44nUR                  [        [        U5      5      v   M+     gU R                  SS9 HF  u  p4mX4/n UR                  U4S jU 5       5        UR                  [        [        U5      5      v   MH     g! [         a     N2f = f7f)a  Generate a single line of the graph G in edge list format.

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

delimiter : string, optional
   Separator for node labels

data : bool or list of keys
   If False generate no edge data.  If True use a dictionary
   representation of edge data.  If a list of keys use a list of data
   values corresponding to the keys.

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

Examples
--------
>>> G = nx.lollipop_graph(4, 3)
>>> G[1][2]["weight"] = 3
>>> G[3][4]["capacity"] = 12
>>> for line in nx.generate_edgelist(G, data=False):
...     print(line)
0 1
0 2
0 3
1 2
1 3
2 3
3 4
4 5
5 6

>>> for line in nx.generate_edgelist(G):
...     print(line)
0 1 {}
0 2 {}
0 3 {}
1 2 {'weight': 3}
1 3 {}
2 3 {}
3 4 {'capacity': 12}
4 5 {}
5 6 {}

>>> for line in nx.generate_edgelist(G, data=["weight"]):
...     print(line)
0 1
0 2
0 3
1 2 3
1 3
2 3
3 4
4 5
5 6

See Also
--------
write_adjlist, read_adjlist
T)dataFc              3   .   >#    U  H
  nTU   v   M     g 7fN ).0kds     M/var/www/html/env/lib/python3.13/site-packages/networkx/readwrite/edgelist.py	<genexpr>$generate_edgelist.<locals>.<genexpr>x   s     ,t!1ts   N)edgesdictjoinmapstrextendKeyError)G	delimiterr   uver   s         @r   r   r   +   s     B t|wwDw)GA!d1gA..S!-- * 
GGG'DAA..S!-- ( wwDw)GA!A,t,, ..S!-- *  s*   B(C=,C-&C=-
C:7C=9C::C=   wb)modec                 t    [        XU5       H(  nUS-  nUR                  UR                  U5      5        M*     g)a  Write graph as a list of edges.

Parameters
----------
G : graph
   A NetworkX graph
path : file or string
   File or filename to write. If a file is provided, it must be
   opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
   The character used to indicate the start of a comment
delimiter : string, optional
   The string used to separate values.  The default is whitespace.
data : bool or list, optional
   If False write no edge data.
   If True write a string representation of the edge data dictionary..
   If a list (or other iterable) is provided, write the  keys specified
   in the list.
encoding: string, optional
   Specify which encoding to use when writing file.

Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_edgelist(G, "test.edgelist")
>>> G = nx.path_graph(4)
>>> fh = open("test.edgelist", "wb")
>>> nx.write_edgelist(G, fh)
>>> nx.write_edgelist(G, "test.edgelist.gz")
>>> nx.write_edgelist(G, "test.edgelist.gz", data=False)

>>> G = nx.Graph()
>>> G.add_edge(1, 2, weight=7, color="red")
>>> nx.write_edgelist(G, "test.edgelist", data=False)
>>> nx.write_edgelist(G, "test.edgelist", data=["color"])
>>> nx.write_edgelist(G, "test.edgelist", data=["color", "weight"])

See Also
--------
read_edgelist
write_weighted_edgelist

N)r   writeencode)r   pathcommentsr   r   encodinglines          r   r   r   ~   s4    Z "!5

4;;x() 6    )graphsreturns_graphc                    SSK Jn  [        R                  " SU5      nU  GHn  nUb%  UR	                  U5      n	U	S:  a  USU	 nU(       d  M,  UR                  S5      R                  U5      n
[        U
5      S:  a  M]  U
R                  S5      nU
R                  S5      nU
nUb   U" U5      nU" U5      n[        U5      S:X  d  US
L a  0 nOUSL aJ   US:X  a  SR                  U5      nOSR                  U5      n[        U" UR                  5       5      5      nO`[        U5      [        U5      :w  a  [        SU SU S35      e0 n[        X]5       H%  u  u  nnn U" U5      nUR!                  UU05        M'     UR"                  " X40 UD6  GMq     U$ ! [         a  n[        SU SU SU S	35      UeSnAff = f! [         a  n[        SU S35      UeSnAff = f! [         a  n[        SU SU SU S	35      UeSnAff = f)a  Parse lines of an edge list representation of a graph.

Parameters
----------
lines : list or iterator of strings
    Input data in edgelist format
comments : string, optional
   Marker for comment lines. Default is `'#'`. To specify that no character
   should be treated as a comment, use ``comments=None``.
delimiter : string, optional
   Separator for node labels. Default is `None`, meaning any whitespace.
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. Default is `None`, meaning no conversion is
   performed.
data : bool or list of (label,type) tuples
   If `False` generate no edge data or if `True` use a dictionary
   representation of edge data or a list tuples specifying dictionary
   key names and types for edge data.

Returns
-------
G: NetworkX Graph
    The graph corresponding to lines

Examples
--------
Edgelist with no data:

>>> lines = ["1 2", "2 3", "3 4"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges())
[(1, 2), (2, 3), (3, 4)]

Edgelist with data in Python dictionary representation:

>>> lines = ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]

Edgelist with data in a list:

>>> lines = ["1 2 3", "2 3 27", "3 4 3.0"]
>>> G = nx.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]

See Also
--------
read_weighted_edgelist
r   )literal_evalNr%      zFailed to convert nodes ,z	 to type .FT zFailed to convert edge data (z) to dictionary.z
Edge data z and data_keys z are not the same lengthzFailed to convert z data )astr0   nxempty_graphfindrstripsplitlenpop	Exception	TypeErrorr   r   strip
IndexErrorzipupdateadd_edge)linesr)   r   create_usingnodetyper   r0   r   r+   psr   r   r   erredgedataedgedata_stredge_key	edge_type
edge_values                       r   r   r      sJ   ~ !
q,'A		(#AAvBQxKK##I.q6A:EE!HEE!HQKQK q6Q;$%-HT\	##&88A;L#&88A;L\-?-?-A BC 1vT"  ?4&8PQ  H58\1%9z!*:!6J : 67 6B 	


1$8$m n HK  .qc1#YxjJ   3A36FG ! #,XJfZL I##,+Q0 sI   %FAF=G
F:F55F:=
GGG
H)G??Hrbc           	      8   ^ U4S jU  5       n[        UUUUUUS9$ )a  Read a graph from a list of edges.

Parameters
----------
path : file or string
   File or filename to read. If a file is provided, it must be
   opened in 'rb' mode.
   Filenames ending in .gz or .bz2 will be uncompressed.
comments : string, optional
   The character used to indicate the start of a comment. To specify that
   no character should be treated as a comment, use ``comments=None``.
delimiter : string, optional
   The string used to separate values.  The default is whitespace.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
   Graph type to create. If graph instance, then cleared before populated.
nodetype : int, float, str, Python type, optional
   Convert node data from strings to specified type
data : bool or list of (label,type) tuples
   Tuples specifying dictionary key names and types for edge data
edgetype : int, float, str, Python type, optional OBSOLETE
   Convert edge data from strings to specified type and use as 'weight'
encoding: string, optional
   Specify which encoding to use when reading file.

Returns
-------
G : graph
   A networkx Graph or other type specified with create_using

Examples
--------
>>> nx.write_edgelist(nx.path_graph(4), "test.edgelist")
>>> G = nx.read_edgelist("test.edgelist")

>>> fh = open("test.edgelist", "rb")
>>> G = nx.read_edgelist(fh)
>>> fh.close()

>>> G = nx.read_edgelist("test.edgelist", nodetype=int)
>>> G = nx.read_edgelist("test.edgelist", create_using=nx.DiGraph)

Edgelist with data in a list:

>>> textline = "1 2 3"
>>> fh = open("test.edgelist", "w")
>>> d = fh.write(textline)
>>> fh.close()
>>> G = nx.read_edgelist("test.edgelist", nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0})]

See parse_edgelist() for more examples of formatting.

See Also
--------
parse_edgelist
write_edgelist

Notes
-----
Since nodes must be hashable, the function nodetype must return hashable
types (e.g. int, float, str, frozenset - or tuples of those, etc.)
c              3   t   >#    U  H-  n[        U[        5      (       a  UOUR                  T5      v   M/     g 7fr   )
isinstancer   decode)r   r+   r*   s     r   r    read_edgelist.<locals>.<genexpr>y  s-     WRV$Zc**TH0EERVs   58)r)   r   rE   rF   r   )r   )	r(   r)   r   rE   rF   r   edgetyper*   rD   s	          ` r   r   r   ,  s1    Z XRVWE! r,   c           	           [        U UUUSUS9  g)a  Write graph G as a list of edges with numeric weights.

Parameters
----------
G : graph
   A NetworkX graph
path : file or string
   File or filename to write. If a file is provided, it must be
   opened in 'wb' mode.
   Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
   The character used to indicate the start of a comment
delimiter : string, optional
   The string used to separate values.  The default is whitespace.
encoding: string, optional
   Specify which encoding to use when writing file.

Examples
--------
>>> G = nx.Graph()
>>> G.add_edge(1, 2, weight=7)
>>> nx.write_weighted_edgelist(G, "test.weighted.edgelist")

See Also
--------
read_edgelist
write_edgelist
read_weighted_edgelist
)weight)r)   r   r   r*   N)r   )r   r(   r)   r   r*   s        r   r   r     s    < 	r,   c           
      .    [        U UUUUS[        44US9$ )a  Read a graph as list of edges with numeric weights.

Parameters
----------
path : file or string
   File or filename to read. If a file is provided, it must be
   opened in 'rb' mode.
   Filenames ending in .gz or .bz2 will be uncompressed.
comments : string, optional
   The character used to indicate the start of a comment.
delimiter : string, optional
   The string used to separate values.  The default is whitespace.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
   Graph type to create. If graph instance, then cleared before populated.
nodetype : int, float, str, Python type, optional
   Convert node data from strings to specified type
encoding: string, optional
   Specify which encoding to use when reading file.

Returns
-------
G : graph
   A networkx Graph or other type specified with create_using

Notes
-----
Since nodes must be hashable, the function nodetype must return hashable
types (e.g. int, float, str, frozenset - or tuples of those, etc.)

Example edgelist file format.

With numeric edge data::

 # read with
 # >>> G=nx.read_weighted_edgelist(fh)
 # source target data
 a b 1
 a c 3.14159
 d e 42

See Also
--------
write_weighted_edgelist
rW   )r)   r   rE   rF   r   r*   )r   float)r(   r)   r   rE   rF   r*   s         r   r   r     s/    j !! r,   )r4   T)#r4   Tutf-8)rZ   NNNT)rZ   NNNTNr[   )rZ   r4   r[   )rZ   NNNr[   )__doc____all__networkxr6   networkx.utilsr	   r   r   _dispatchabler   r   r   r   r   r,   r   <module>ra      s   :  $P.f 14.* .*b T2PTx 3xv 14T2 	S 3 Sl%P T2 < 3<r,   