
    h                         S r SSKrSS/r\R                  " SS9 S
SS.S jj5       r\R                  " SS9SS	 j5       rg)z2
Adjacency matrix and incidence matrix of graphs.
    Nincidence_matrixadjacency_matrixweight)
edge_attrsdtypec          	      $   SSK nUc  [        U 5      nUcG  U R                  5       (       a  [        U R                  SS95      nO[        U R                  5       5      nUR                  R                  [        U5      [        U5      4US9n[        U5       VV	s0 s H  u  pX_M	     n
nn	[        U5       H  u  pUSS u  pX:X  a  M   X   nX   nUc  S
nOLU R                  5       (       a   US   nX   U   U   R                  US
5      nOX   U   R                  US
5      nU(       a  U* XU4'   UUUU4'   M  UXU4'   UUUU4'   M     UR                  S5      $ s  sn	nf ! [         a#  n[        R                  " SU SU S	35      UeSnAff = f)a  Returns incidence matrix of G.

The incidence matrix assigns each row to a node and each column to an edge.
For a standard incidence matrix a 1 appears wherever a row's node is
incident on the column's edge.  For an oriented incidence matrix each
edge is assigned an orientation (arbitrarily for undirected and aligning to
direction for directed).  A -1 appears for the source (tail) of an edge and
1 for the destination (head) of the edge.  The elements are zero otherwise.

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

nodelist : list, optional   (default= all nodes in G)
   The rows are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

edgelist : list, optional (default= all edges in G)
   The columns are ordered according to the edges in edgelist.
   If edgelist is None, then the ordering is produced by G.edges().

oriented: bool, optional (default=False)
   If True, matrix elements are +1 or -1 for the head or tail node
   respectively of each edge.  If False, +1 occurs at both nodes.

weight : string or None, optional (default=None)
   The edge data key used to provide each value in the matrix.
   If None, then each edge has weight 1.  Edge weights, if used,
   should be positive so that the orientation can provide the sign.

dtype : a NumPy dtype or None (default=None)
    The dtype of the output sparse array. This type should be a compatible
    type of the weight argument, eg. if weight would return a float this
    argument should also be a float.
    If None, then the default for SciPy is used.

Returns
-------
A : SciPy sparse array
  The incidence matrix of G.

Notes
-----
For MultiGraph/MultiDiGraph, the edges in edgelist should be
(u,v,key) 3-tuples.

"Networks are the best discrete model for so many problems in
applied mathematics" [1]_.

References
----------
.. [1] Gil Strang, Network applications: A = incidence matrix,
   http://videolectures.net/mit18085f07_strang_lec03/
r   NT)keysr      znode z or z  in edgelist but not in nodelist   csc)scipylistis_multigraphedgessparse	lil_arraylen	enumerateKeyErrornxNetworkXErrorgetasformat)Gnodelistedgelistorientedr   r   spAinode
node_indexeieuvuivierrwtekeys                       M/var/www/html/env/lib/python3.13/site-packages/networkx/linalg/graphmatrix.pyr   r   
   s   v 7??AGGG./HAGGIH
		S]CM:%HA)28)<=)<ga$')<J=8$2A6	BB
 >B  tT!WT]&&vq1T!W[[+A"fIAb"fIA"fIAb"fI1 %2 ::e5 >  	""s$qc!AB	s   E	E""
F,F

Fc                 ,    [         R                  " XX#S9$ )ak  Returns adjacency matrix of `G`.

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

nodelist : list, optional
   The rows and columns are ordered according to the nodes in `nodelist`.
   If ``nodelist=None`` (the default), then the ordering is produced by
   ``G.nodes()``.

dtype : NumPy data-type, optional
    The desired data-type for the array.
    If `None`, then the NumPy default is used.

weight : string or None, optional (default='weight')
   The edge data key used to provide each value in the matrix.
   If None, then each edge has weight 1.

Returns
-------
A : SciPy sparse array
  Adjacency matrix representation of G.

Notes
-----
For directed graphs, entry ``i, j`` corresponds to an edge from ``i`` to ``j``.

If you want a pure Python adjacency matrix representation try
:func:`~networkx.convert.to_dict_of_dicts` which will return a
dictionary-of-dictionaries format that can be addressed as a
sparse matrix.

For multigraphs with parallel edges the weights are summed.
See :func:`networkx.convert_matrix.to_numpy_array` for other options.

The convention used for self-loop edges in graphs is to assign the
diagonal matrix entry value to the edge weight attribute
(or the number 1 if the edge has no weight attribute).  If the
alternate convention of doubling the edge weight is desired the
resulting SciPy sparse array can be modified as follows::

    >>> G = nx.Graph([(1, 1)])
    >>> A = nx.adjacency_matrix(G)
    >>> A.toarray()
    array([[1]])
    >>> A.setdiag(A.diagonal() * 2)
    >>> A.toarray()
    array([[2]])

See Also
--------
to_numpy_array
to_scipy_sparse_array
to_dict_of_dicts
adjacency_spectrum
)r   r   r   )r   to_scipy_sparse_array)r   r   r   r   s       r-   r   r   l   s    x ##AUU    )NNFN)NNr   )__doc__networkxr   __all___dispatchabler   r    r0   r-   <module>r6      si    1
2 X&<@^KO^ '^B X&;V ';Vr0   