
    h>                     6   S r SSKrSSKJrJrJr  SSKr/ SQrSS jr	\R                  SS j5       r\R                  " SSS9SS	 j5       rSS
 jr\R                  " SSS9SS j5       r\R                  " SS9SS j5       r\R                  " SSS9SS j5       rg)a  Functions to convert NetworkX graphs to and from other formats.

The preferred way of converting data to a NetworkX graph is through the
graph constructor.  The constructor calls the to_networkx_graph() function
which attempts to guess the input type and convert it automatically.

Examples
--------
Create a graph with a single edge from a dictionary of dictionaries

>>> d = {0: {1: 1}}  # dict-of-dicts single edge (0,1)
>>> G = nx.Graph(d)

See Also
--------
nx_agraph, nx_pydot
    N)
Collection	GeneratorIterator)to_networkx_graphfrom_dict_of_dictsto_dict_of_dictsfrom_dict_of_liststo_dict_of_listsfrom_edgelistto_edgelistc                    [        U S5      (       a   [        U R                  UU R                  5       S9nUR                  R                  U R                  5        U R                  R                  5        H#  u  pEUR                  U   R                  U5        M%     U$ [        U [        5      (       a   [        XUS9$ [        U ["        [$        -  [        R&                  R(                  -  [*        -  5      (       a
   [-        XS9$ [        U S
5      (       a   [        R.                  R1                  XS9$  SSKn	[        X	R4                  5      (       aL  U R6                  S   U R6                  S   :X  a   [        R8                  " XS9$  [        R:                  " U SUS9$   SSKn[        XR@                  5      (       a   [        RB                  " XS9$   SSK"n[        U S5      (       a   [        RF                  " XS9$  [        U [H        [J        -  [*        -  5      (       a
   [-        XS9$ [        R                  " S5      e! [         a  n[        R                  " S5      UeSnAff = f! [         a\  nUSL a%  [        R                  " S[        U5       SU 35      e [        XS9s SnA$ ! [         a  n[!        S	5      UeSnAff = fSnAff = f!    GN= f! [         a  n[        R                  " S5      UeSnAff = f! [         a  nSn
[        R                  " U
5      UeSnAff = f! [         a  nSn
[        R                  " U
5      UeSnAff = f! [<         a     GNf = f! [         a  n[        R                  " S5      UeSnAff = f! [<         a     GNf = f! [         a  n[        R                  " S5      UeSnAff = f! [<         a     GNf = f! [         a  n[        R                  " S5      UeSnAff = f)a  Make a NetworkX graph from a known data structure.

The preferred way to call this is automatically
from the class constructor

>>> d = {0: {1: {"weight": 1}}}  # dict-of-dicts single edge (0,1)
>>> G = nx.Graph(d)

instead of the equivalent

>>> G = nx.from_dict_of_dicts(d)

Parameters
----------
data : object to be converted

    Current known types are:
     any NetworkX graph
     dict-of-dicts
     dict-of-lists
     container (e.g. set, list, tuple) of edges
     iterator (e.g. itertools.chain) that produces edges
     generator of edges
     Pandas DataFrame (row per edge)
     2D numpy array
     scipy sparse array
     pygraphviz agraph

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

multigraph_input : bool (default False)
    If True and  data is a dict_of_dicts,
    try to create a multigraph assuming dict_of_dict_of_lists.
    If data and create_using are both multigraphs then create
    a multigraph from a multigraph.

adj)create_usingmultigraph_inputz&Input is not a correct NetworkX graph.NTz$converting multigraph_input raised:
z: )r   zInput is not known type.	is_strictz(Input is not a correct pygraphviz graph.r      z9Input is not a correct Pandas DataFrame adjacency matrix.)	edge_attrr   z2Input is not a correct Pandas DataFrame edge-list.z1Failed to interpret array as an adjacency matrix.formatz/Input is not a correct scipy sparse array type.zInput is not a valid edge listz.Input is not a known data type for conversion.)&hasattrr   r   is_multigraphgraphupdatenodesitems_node	ExceptionnxNetworkXError
isinstancedicttyper	   	TypeErrorlisttuplereportviewsEdgeViewABCr   r   	nx_agraphfrom_agraphpandas	DataFrameshapefrom_pandas_adjacencyfrom_pandas_edgelistImportErrornumpyndarrayfrom_numpy_arrayscipyfrom_scipy_sparse_arrayr   r   )datar   r   resultndderrerr1err2pdmsgnpr2   s                B/var/www/html/env/lib/python3.13/site-packages/networkx/convert.pyr   r   #   s   P tU	V')!%!3!3!5F LL

+
 ))+Q&&r* ,M
 $	F%BR  $ur~~'A'AAHLMM	 AA
 t[!!	X<<++D+LL
dLL))zz!}

1-933DTT
922<  *&dJJ''**4KK (4""11$RR # $
Y.9::	N AA 

K
LL{  	V""#KLRUU	V  	F4'&&;DJ<r$P F)$JJ F :;EF	F	  	X""#MNTWW	X ! 9UC**3/S89 ! 9NC**3/S89    &&G    &&E    	N""#CD#M	Ns>  B
I 4	I. =K K 6>M 5L 
L3 "N M/ O -N) &O# 
I+I&&I+.
K8*K#J1+K1
K;KKKKK
L)L  L
L0L++L00M 3
M=MMM 
M,+M,/
N9NNN 
N&%N&)
O3O

OO 
O O #
P	-PP	c                     Uc  U n0 nU H.  nU R                  U5       Vs/ s H  oDU;   d  M
  UPM     snX#'   M0     U$ s  snf )zReturns adjacency representation of graph as a dictionary of lists.

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

nodelist : list
   Use only nodes specified in nodelist

Notes
-----
Completely ignores edge data for MultiGraph and MultiDiGraph.

)	neighbors)Gnodelistdr6   nbrs        r>   r
   r
      sK    " 
A {{1~A~~A H Bs   	>>T)graphsreturns_graphc                 z   [         R                  " SU5      nUR                  U 5        UR                  5       (       aW  UR	                  5       (       dB  0 nU R                  5        H*  u  pEU H  nXc;  d  M
  UR                  XF5        M     SX4'   M,     U$ UR                  S U R                  5        5       5        U$ )a  Returns a graph from a dictionary of lists.

Parameters
----------
d : dictionary of lists
  A dictionary of lists adjacency representation.

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

Examples
--------
>>> dol = {0: [1]}  # single edge (0,1)
>>> G = nx.from_dict_of_lists(dol)

or

>>> G = nx.Graph(dol)  # use Graph constructor

r   r   c              3   @   #    U  H  u  pU  H  o1U4v   M
     M     g 7fN ).0nodenbrlistrD   s       r>   	<genexpr>%from_dict_of_lists.<locals>.<genexpr>   s     Ki]T7CC[7[is   )r   empty_graphadd_nodes_fromr   is_directedr   add_edgeadd_edges_from)rC   r   rA   seenrL   rM   rD   s          r>   r	   r	      s    , 	q,'AQ WWYMD?JJt)  DJ	 ' H 	
KaggiK	
 H    c                   ^ 0 nTc_  Uc-  U R                  5        H  u  pEUR                  5       X4'   M     U$ U R                  5        H  u  pEUR                  XR5      X4'   M      U$ Uc;  T H3  n0 X4'   U4S jX   R                  5        5        H  u  pgXsU   U'   M     M5     U$ T H#  n0 X4'   U4S jX    5        H
  nX#U   U'   M     M%     U$ )a
  Returns adjacency representation of graph as a dictionary of dictionaries.

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

nodelist : list
   Use only nodes specified in nodelist

edge_data : scalar, optional
   If provided, the value of the dictionary will be set to `edge_data` for
   all edges. Usual values could be `1` or `True`. If `edge_data` is
   `None` (the default), the edgedata in `G` is used, resulting in a
   dict-of-dict-of-dicts. If `G` is a MultiGraph, the result will be a
   dict-of-dict-of-dict-of-dicts. See Notes for an approach to customize
   handling edge data. `edge_data` should *not* be a container.

Returns
-------
dod : dict
   A nested dictionary representation of `G`. Note that the level of
   nesting depends on the type of `G` and the value of `edge_data`
   (see Examples).

See Also
--------
from_dict_of_dicts, to_dict_of_lists

Notes
-----
For a more custom approach to handling edge data, try::

    dod = {
        n: {nbr: custom(n, nbr, dd) for nbr, dd in nbrdict.items()}
        for n, nbrdict in G.adj.items()
    }

where `custom` returns the desired edge data for each edge between `n` and
`nbr`, given existing edge data `dd`.

Examples
--------
>>> G = nx.path_graph(3)
>>> nx.to_dict_of_dicts(G)
{0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}}}

Edge data is preserved by default (``edge_data=None``), resulting
in dict-of-dict-of-dicts where the innermost dictionary contains the
edge data:

>>> G = nx.Graph()
>>> G.add_edges_from(
...     [
...         (0, 1, {"weight": 1.0}),
...         (1, 2, {"weight": 2.0}),
...         (2, 0, {"weight": 1.0}),
...     ]
... )
>>> d = nx.to_dict_of_dicts(G)
>>> d  # doctest: +SKIP
{0: {1: {'weight': 1.0}, 2: {'weight': 1.0}},
 1: {0: {'weight': 1.0}, 2: {'weight': 2.0}},
 2: {1: {'weight': 2.0}, 0: {'weight': 1.0}}}
>>> d[1][2]["weight"]
2.0

If `edge_data` is not `None`, edge data in the original graph (if any) is
replaced:

>>> d = nx.to_dict_of_dicts(G, edge_data=1)
>>> d
{0: {1: 1, 2: 1}, 1: {0: 1, 2: 1}, 2: {1: 1, 0: 1}}
>>> d[1][2]
1

This also applies to MultiGraphs: edge data is preserved by default:

>>> G = nx.MultiGraph()
>>> G.add_edge(0, 1, key="a", weight=1.0)
'a'
>>> G.add_edge(0, 1, key="b", weight=5.0)
'b'
>>> d = nx.to_dict_of_dicts(G)
>>> d  # doctest: +SKIP
{0: {1: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}},
 1: {0: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}}}
>>> d[0][1]["b"]["weight"]
5.0

But multi edge data is lost if `edge_data` is not `None`:

>>> d = nx.to_dict_of_dicts(G, edge_data=10)
>>> d
{0: {1: 10}, 1: {0: 10}}
c              3   >   >#    U  H  u  pUT;   d  M  X4v   M     g 7frI   rJ   )rK   vr4   rB   s      r>   rN   #to_dict_of_dicts.<locals>.<genexpr>k  s     Wgah		s   
c              3   6   >#    U  H  oT;   d  M
  Uv   M     g 7frI   rJ   )rK   rY   rB   s     r>   rN   rZ   p  s     ;T(]!!Ts   		)	adjacencycopyfromkeysr   )rA   rB   	edge_datadodunbrdictrY   r4   s    `      r>   r   r      s    B Ckkm
  ,  J  kkm
g9 , J WWGA $F1I  X  J	 ;QT;A )F1I <  JrV   c                 T  ^^	 [         R                  " SU5      nUR                  U 5        U(       Ga  UR                  5       (       af  UR	                  5       (       a(  UR                  S U R                  5        5       5        U$ UR                  S U R                  5        5       5         U$ UR	                  5       (       a  [        5       nU R                  5        Hg  u  mnUR                  5        HM  u  m	nTT	4U;  d  M  UR                  UU	4S jUR                  5        5       5        UR                  T	T45        MO     Mi     U$ [        5       nU R                  5        Hg  u  mnUR                  5        HM  u  m	nTT	4U;  d  M  UR                  UU	4S jUR                  5        5       5        UR                  T	T45        MO     Mi      U$ UR	                  5       (       a  UR                  5       (       d  [        5       nU R                  5        Hf  u  mnUR                  5        HL  u  m	nTT	4U;  a+  UR                  TT	SS9  UT   T	   S   R                  U5        UR                  T	T45        MN     Mh     U$ UR                  S U R                  5        5       5        U$ )a  Returns a graph from a dictionary of dictionaries.

Parameters
----------
d : dictionary of dictionaries
  A dictionary of dictionaries adjacency representation.

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

multigraph_input : bool (default False)
   When True, the dict `d` is assumed
   to be a dict-of-dict-of-dict-of-dict structure keyed by
   node to neighbor to edge keys to edge data for multi-edges.
   Otherwise this routine assumes dict-of-dict-of-dict keyed by
   node to neighbor to edge data.

Examples
--------
>>> dod = {0: {1: {"weight": 1}}}  # single edge (0,1)
>>> G = nx.from_dict_of_dicts(dod)

or

>>> G = nx.Graph(dod)  # use Graph constructor

r   c              3      #    U  H?  u  pUR                  5         H%  u  p4UR                  5         H  u  pVXXV4v   M     M'     MA     g 7frI   r   rK   ra   nbrsrY   datadictkeyr4   s          r>   rN   %from_dict_of_dicts.<locals>.<genexpr>  sK      !#,'+zz|%-^^%5	 3% &6 &'3 &#,   AA	c              3      #    U  H?  u  pUR                  5         H%  u  p4UR                  5         H  u  pVXU4v   M     M'     MA     g 7frI   re   rf   s          r>   rN   rj     sJ      !#,'+zz|%-^^%5	 4L &6 !'3 !#,rk   c              3   2   >#    U  H  u  pTTX4v   M     g 7frI   rJ   rK   ri   r4   ra   rY   s      r>   rN   rj     s      -CSicAs 1CS   c              3   2   >#    U  H  u  pTTU4v   M     g 7frI   rJ   rn   s      r>   rN   rj     s      ->NAt>Nro   )ri   c              3   b   #    U  H%  u  pUR                  5         H  u  p4XU4v   M     M'     g 7frI   re   )rK   ra   rg   rY   r4   s        r>   rN   rj     s&     SY'!djjl71!lYs   -/)r   rP   rQ   rR   r   rT   r   setaddrS   r   )
rC   r   r   rA   rU   rg   rh   r4   ra   rY   s
           @@r>   r   r   u  sP   : 	q,'AQ==??     !#$779! b HU    !#$779! T HG   u wwyGAt'+zz|8q6-,, -CK>>CS-  !HHaV, (4  )B H3 u wwyGAt'+zz|8q6-,, ->Fnn>N-  !HHaV, (4  )0 H ??Q]]__ 5D7794#zz|GAt1vT)

1aQ
/!Q
))$/HHaV$	  , % H SQWWYS HrV   )preserve_edge_attrsc                 F    Uc  U R                  SS9$ U R                  USS9$ )zReturns a list of edges in the graph.

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

nodelist : list
   Use only nodes specified in nodelist

T)r4   )edges)rA   rB   s     r>   r   r     s.     wwDw!!778$7''rV   c                 V    [         R                  " SU5      nUR                  U 5        U$ )a  Returns a graph from a list of edges.

Parameters
----------
edgelist : list or iterator
  Edge tuples

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

Examples
--------
>>> edgelist = [(0, 1)]  # single edge (0,1)
>>> G = nx.from_edgelist(edgelist)

or

>>> G = nx.Graph(edgelist)  # use Graph constructor

r   )r   rP   rT   )edgelistr   rA   s      r>   r   r     s'    , 	q,'AXHrV   )NFrI   )NN)__doc__warningscollections.abcr   r   r   networkxr   __all__r   _dispatchabler
   r	   r   r   r   r   rJ   rV   r>   <module>r      s   $  ; ; UMp  2 T2% 3%Ptn T2S 3Sl d+( ,(" T2 3rV   