
    h                        S r SSKJr  SSKJr  SSKrSSKJrJ	r	  / SQr
S rS4S jrS5S	 jrS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS6S jr S4S jr!S4S jr"SS.S  jr#S4S! jr$S4S" jr%SS#.S$ jr&S% r'S& r(S' r)\" S(5      S) 5       r*S7S+ jr+\RX                  " S*S,9S7S- j5       r-S. r.S/ r/S8S0 jr0S1 r1S2 r2S3 r3g)9z=Functional interface to graph methods and assorted utilities.    )Counter)chainN)not_implemented_forpairwise)'nodesedgesdegreedegree_histogram	neighborsnumber_of_nodesnumber_of_edgesdensityis_directedfreeze	is_frozensubgraphinduced_subgraphedge_subgraphrestricted_viewto_directedto_undirectedadd_staradd_path	add_cyclecreate_empty_copyset_node_attributesget_node_attributesremove_node_attributesset_edge_attributesget_edge_attributesremove_edge_attributesall_neighborsnon_neighbors	non_edgescommon_neighborsis_weightedis_negatively_weightedis_emptyselfloop_edgesnodes_with_selfloopsnumber_of_selfloopspath_weightis_pathc                 "    U R                  5       $ )zsReturns a NodeView over the graph nodes.

This function wraps the :func:`G.nodes <networkx.Graph.nodes>` property.
)r   Gs    K/var/www/html/env/lib/python3.13/site-packages/networkx/classes/function.pyr   r   4   s    
 779    c                 $    U R                  U5      $ )zReturns an edge view of edges incident to nodes in nbunch.

Return all edges if nbunch is unspecified or nbunch=None.

For digraphs, edges=out_edges

This function wraps the :func:`G.edges <networkx.Graph.edges>` property.
)r   r0   nbunchs     r1   r   r   <   s     776?r2   c                 $    U R                  X5      $ )zReturns a degree view of single node or of nbunch of nodes.
If nbunch is omitted, then return degrees of *all* nodes.

This function wraps the :func:`G.degree <networkx.Graph.degree>` property.
)r	   )r0   r5   weights      r1   r	   r	   H   s     88F##r2   c                 $    U R                  U5      $ )zReturns an iterator over all neighbors of node n.

This function wraps the :func:`G.neighbors <networkx.Graph.neighbors>` function.
)r   )r0   ns     r1   r   r   Q   s    
 ;;q>r2   c                 "    U R                  5       $ )zReturns the number of nodes in the graph.

This function wraps the :func:`G.number_of_nodes <networkx.Graph.number_of_nodes>` function.
)r   r/   s    r1   r   r   Y       
 r2   c                 "    U R                  5       $ )zReturns the number of edges in the graph.

This function wraps the :func:`G.number_of_edges <networkx.Graph.number_of_edges>` function.
)r   r/   s    r1   r   r   a   r;   r2   c                     [        U 5      n[        U 5      nUS:X  d  US::  a  gX!US-
  -  -  nU R                  5       (       d  US-  nU$ )a  Returns the density of a graph.

The density for undirected graphs is

.. math::

   d = \frac{2m}{n(n-1)},

and for directed graphs is

.. math::

   d = \frac{m}{n(n-1)},

where `n` is the number of nodes and `m`  is the number of edges in `G`.

Notes
-----
The density is 0 for a graph without edges and 1 for a complete graph.
The density of multigraphs can be higher than 1.

Self loops are counted in the total number of edges so graphs with self
loops can have density higher than 1.
r         )r   r   r   )r0   r9   mds       r1   r   r   i   sQ    2 	AAAva	!a%[A==??	QHr2   c                     [        S U R                  5        5       5      n[        U(       a  [        U5      S-   OS5       Vs/ s H  o!R	                  US5      PM     sn$ s  snf )a>  Returns a list of the frequency of each degree value.

Parameters
----------
G : Networkx graph
   A graph

Returns
-------
hist : list
   A list of frequencies of degrees.
   The degree values are the index in the list.

Notes
-----
Note: the bins are width one, hence len(list) can be large
(Order(number_of_edges))
c              3   *   #    U  H	  u  pUv   M     g 7fN ).0r9   rA   s      r1   	<genexpr>#degree_histogram.<locals>.<genexpr>   s     .:41Q:s   r>   r   )r   r	   rangemaxget)r0   countsis      r1   r
   r
      sN    & .188:..F&+vCK!O1&MN&MJJq!&MNNNs   A!c                 "    U R                  5       $ )z!Return True if graph is directed.)r   r/   s    r1   r   r      s    ==?r2   c                  .    [         R                  " S5      e)zCDummy method for raising errors when trying to modify frozen graphszFrozen graph can't be modified)nxNetworkXError)argskwargss     r1   frozenrT      s    


;
<<r2   c                    [         U l        [         U l        [         U l        [         U l        [         U l        [         U l        [         U l        [         U l        [         U l	        [         U l
        [         U l        SU l         U $ )at  Modify graph to prevent further change by adding or removing
nodes or edges.

Node and edge data can still be modified.

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

Examples
--------
>>> G = nx.path_graph(4)
>>> G = nx.freeze(G)
>>> try:
...     G.add_edge(4, 5)
... except nx.NetworkXError as err:
...     print(str(err))
Frozen graph can't be modified

Notes
-----
To "unfreeze" a graph you must make a copy by creating a new graph object:

>>> graph = nx.path_graph(4)
>>> frozen_graph = nx.freeze(graph)
>>> unfrozen_graph = nx.Graph(frozen_graph)
>>> nx.is_frozen(unfrozen_graph)
False

See Also
--------
is_frozen
T)rT   add_nodeadd_nodes_fromremove_noderemove_nodes_fromadd_edgeadd_edges_fromadd_weighted_edges_fromremove_edgeremove_edges_fromclearclear_edgesr/   s    r1   r   r      sc    F AJAAM AAJA &AAM AAGAMAHHr2   c                 <     U R                   $ ! [         a     gf = f)zoReturns True if graph is frozen.

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

See Also
--------
freeze
F)rT   AttributeErrorr/   s    r1   r   r      s"    xx s    
c                    ^ [        U5      n [        U5      mU R                  T5        U4S jU 5       nU R                  " U40 UD6  g! [         a     gf = f)a  Add a star to Graph G_to_add_to.

The first node in `nodes_for_star` is the middle of the star.
It is connected to all other nodes.

Parameters
----------
G_to_add_to : graph
    A NetworkX graph
nodes_for_star : iterable container
    A container of nodes.
attr : keyword arguments, optional (default= no attributes)
    Attributes to add to every edge in star.

See Also
--------
add_path, add_cycle

Examples
--------
>>> G = nx.Graph()
>>> nx.add_star(G, [0, 1, 2, 3])
>>> nx.add_star(G, [10, 11, 12], weight=2)
Nc              3   ,   >#    U  H	  nTU4v   M     g 7frD   rE   )rF   r9   vs     r1   rG   add_star.<locals>.<genexpr>  s     #UaVU   )iternextStopIterationrV   r[   )G_to_add_tonodes_for_starattrnlistr   re   s        @r1   r   r      s^    2  EK #U#Eu--	  s   A
 

AAc                     [        U5      n [        U5      nU R                  U5        U R                  " [        [        U4U5      5      40 UD6  g! [         a     gf = f)a  Add a path to the Graph G_to_add_to.

Parameters
----------
G_to_add_to : graph
    A NetworkX graph
nodes_for_path : iterable container
    A container of nodes.  A path will be constructed from
    the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
    Attributes to add to every edge in path.

See Also
--------
add_star, add_cycle

Examples
--------
>>> G = nx.Graph()
>>> nx.add_path(G, [0, 1, 2, 3])
>>> nx.add_path(G, [10, 11, 12], weight=7)
Nrh   ri   rj   rV   r[   r   r   )rk   nodes_for_pathrm   rn   
first_nodes        r1   r   r     sa    .  E%[
 $xzmU(CDMM  s   A 
AAc                     [        U5      n [        U5      nU R                  U5        U R                  " [        [        U4U5      SS940 UD6  g! [         a     gf = f)a(  Add a cycle to the Graph G_to_add_to.

Parameters
----------
G_to_add_to : graph
    A NetworkX graph
nodes_for_cycle: iterable container
    A container of nodes.  A cycle will be constructed from
    the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
    Attributes to add to every edge in cycle.

See Also
--------
add_path, add_star

Examples
--------
>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> nx.add_cycle(G, [0, 1, 2, 3])
>>> nx.add_cycle(G, [10, 11, 12], weight=7)
NT)cyclicrp   )rk   nodes_for_cyclerm   rn   rr   s        r1   r   r   4  si    . !E%[
 $
}e,T:>B  s   A 
AAc                 $    U R                  U5      $ )a  Returns the subgraph induced on nodes in nbunch.

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

nbunch : list, iterable
   A container of nodes that will be iterated through once (thus
   it should be an iterator or be iterable).  Each element of the
   container should be a valid node type: any hashable type except
   None.  If nbunch is None, return all edges data in the graph.
   Nodes in nbunch that are not in the graph will be (quietly)
   ignored.

Notes
-----
subgraph(G) calls G.subgraph()
)r   r4   s     r1   r   r   V  s    ( ::fr2   c                     [         R                  R                  U R                  U5      5      n[         R                  " XS9$ )aY  Returns a SubGraph view of `G` showing only nodes in nbunch.

The induced subgraph of a graph on a set of nodes N is the
graph with nodes N and edges from G which have both ends in N.

Parameters
----------
G : NetworkX Graph
nbunch : node, container of nodes or None (for all nodes)

Returns
-------
subgraph : SubGraph View
    A read-only view of the subgraph in `G` induced by the nodes.
    Changes to the graph `G` will be reflected in the view.

Notes
-----
To create a mutable subgraph with its own copies of nodes
edges and attributes use `subgraph.copy()` or `Graph(subgraph)`

For an inplace reduction of a graph to a subgraph you can remove nodes:
`G.remove_nodes_from(n in G if n not in set(nbunch))`

If you are going to compute subgraphs of your subgraphs you could
end up with a chain of views that can be very slow once the chain
has about 15 views in it. If they are all induced subgraphs, you
can short-cut the chain by making them all subgraphs of the original
graph. The graph class method `G.subgraph` does this when `G` is
a subgraph. In contrast, this function allows you to choose to build
chains or not, as you wish. The returned subgraph is a view on `G`.

Examples
--------
>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> H = nx.induced_subgraph(G, [0, 1, 3])
>>> list(H.edges)
[(0, 1)]
>>> list(H.nodes)
[0, 1, 3]
)filter_node)rP   filters
show_nodesnbunch_itersubgraph_view)r0   r5   induced_nodess      r1   r   r   m  s3    T JJ))!--*?@MA99r2   c                    [         R                  n[        U5      n[        5       nU H  nUR                  USS 5        M     UR	                  U5      nU R                  5       (       a9  U R                  5       (       a  UR                  U5      nOJUR                  U5      nO8U R                  5       (       a  UR                  U5      nOUR                  U5      n[         R                  " XUS9$ )aM  Returns a view of the subgraph induced by the specified edges.

The induced subgraph contains each edge in `edges` and each
node incident to any of those edges.

Parameters
----------
G : NetworkX Graph
edges : iterable
    An iterable of edges. Edges not present in `G` are ignored.

Returns
-------
subgraph : SubGraph View
    A read-only edge-induced subgraph of `G`.
    Changes to `G` are reflected in the view.

Notes
-----
To create a mutable subgraph with its own copies of nodes
edges and attributes use `subgraph.copy()` or `Graph(subgraph)`

If you create a subgraph of a subgraph recursively you can end up
with a chain of subgraphs that becomes very slow with about 15
nested subgraph views. Luckily the edge_subgraph filter nests
nicely so you can use the original graph as G in this function
to avoid chains. We do not rule out chains programmatically so
that odd cases like an `edge_subgraph` of a `restricted_view`
can be created.

Examples
--------
>>> G = nx.path_graph(5)
>>> H = G.edge_subgraph([(0, 1), (3, 4)])
>>> list(H.nodes)
[0, 1, 3, 4]
>>> list(H.edges)
[(0, 1), (3, 4)]
Nr?   rx   filter_edge)rP   ry   setupdaterz   is_multigraphr   show_multidiedgesshow_multiedgesshow_diedges
show_edgesr|   )r0   r   nxfr   er}   induced_edgess          r1   r   r     s    P **CJEEEQrU NN5)M==??11%8M//6M==??,,U3MNN51MAmTTr2   c                 z   [         R                  nUR                  U5      nU R                  5       (       a9  U R	                  5       (       a  UR                  U5      nOJUR                  U5      nO8U R	                  5       (       a  UR                  U5      nOUR                  U5      n[         R                  " XUS9$ )a  Returns a view of `G` with hidden nodes and edges.

The resulting subgraph filters out node `nodes` and edges `edges`.
Filtered out nodes also filter out any of their edges.

Parameters
----------
G : NetworkX Graph
nodes : iterable
    An iterable of nodes. Nodes not present in `G` are ignored.
edges : iterable
    An iterable of edges. Edges not present in `G` are ignored.

Returns
-------
subgraph : SubGraph View
    A read-only restricted view of `G` filtering out nodes and edges.
    Changes to `G` are reflected in the view.

Notes
-----
To create a mutable subgraph with its own copies of nodes
edges and attributes use `subgraph.copy()` or `Graph(subgraph)`

If you create a subgraph of a subgraph recursively you may end up
with a chain of subgraph views. Such chains can get quite slow
for lengths near 15. To avoid long chains, try to make your subgraph
based on the original graph.  We do not rule out chains programmatically
so that odd cases like an `edge_subgraph` of a `restricted_view`
can be created.

Examples
--------
>>> G = nx.path_graph(5)
>>> H = nx.restricted_view(G, [0], [(1, 2), (3, 4)])
>>> list(H.nodes)
[1, 2, 3, 4]
>>> list(H.edges)
[(2, 3)]
r   )
rP   ry   
hide_nodesr   r   hide_multidiedgeshide_multiedgeshide_diedges
hide_edgesr|   )r0   r   r   r   r   r   s         r1   r   r     s    R **C&J==??..u5J,,U3J==??))%0J.JA:NNr2   c                      U R                  SS9$ )zReturns a directed view of the graph `graph`.

Identical to graph.to_directed(as_view=True)
Note that graph.to_directed defaults to `as_view=False`
while this function always provides a view.
Tas_view)r   graphs    r1   r   r     s     T**r2   c                      U R                  SS9$ )zReturns an undirected view of the graph `graph`.

Identical to graph.to_undirected(as_view=True)
Note that graph.to_undirected defaults to `as_view=False`
while this function always provides a view.
Tr   )r   r   s    r1   r   r     s     t,,r2   c                     U R                  5       nUR                  U R                  US95        U(       a%  UR                  R	                  U R                  5        U$ )zReturns a copy of the graph G with all of the edges removed.

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

with_data :  bool (default=True)
   Propagate Graph and Nodes data to the new graph.

See Also
--------
empty_graph

data)	__class__rW   r   r   r   )r0   	with_dataHs      r1   r   r   "  sD      	
AQWW)W,-	qwwHr2   c                    Ub0   UR                  5        H  u  p4 X   U R                  U   U'   M     O8UR                  5        H$  u  p5 U R                  U   R	                  U5        M&     [
        R                  " U 5        g! [         a     M{  f = f! [         a    U  H  nXR                  U   U'   M      NNf = f! [         a     M  f = f)aQ	  Sets node attributes from a given value or dictionary of values.

.. Warning:: The call order of arguments `values` and `name`
    switched between v1.x & v2.x.

Parameters
----------
G : NetworkX Graph

values : scalar value, dict-like
    What the node attribute should be set to.  If `values` is
    not a dictionary, then it is treated as a single attribute value
    that is then applied to every node in `G`.  This means that if
    you provide a mutable object, like a list, updates to that object
    will be reflected in the node attribute for every node.
    The attribute name will be `name`.

    If `values` is a dict or a dict of dict, it should be keyed
    by node to either an attribute value or a dict of attribute key/value
    pairs used to update the node's attributes.

name : string (optional, default=None)
    Name of the node attribute to set if values is a scalar.

Examples
--------
After computing some property of the nodes of a graph, you may want
to assign a node attribute to store the value of that property for
each node::

    >>> G = nx.path_graph(3)
    >>> bb = nx.betweenness_centrality(G)
    >>> isinstance(bb, dict)
    True
    >>> nx.set_node_attributes(G, bb, "betweenness")
    >>> G.nodes[1]["betweenness"]
    1.0

If you provide a list as the second argument, updates to the list
will be reflected in the node attribute for each node::

    >>> G = nx.path_graph(3)
    >>> labels = []
    >>> nx.set_node_attributes(G, labels, "labels")
    >>> labels.append("foo")
    >>> G.nodes[0]["labels"]
    ['foo']
    >>> G.nodes[1]["labels"]
    ['foo']
    >>> G.nodes[2]["labels"]
    ['foo']

If you provide a dictionary of dictionaries as the second argument,
the outer dictionary is assumed to be keyed by node to an inner
dictionary of node attributes for that node::

    >>> G = nx.path_graph(3)
    >>> attrs = {0: {"attr1": 20, "attr2": "nothing"}, 1: {"attr2": 3}}
    >>> nx.set_node_attributes(G, attrs)
    >>> G.nodes[0]["attr1"]
    20
    >>> G.nodes[0]["attr2"]
    'nothing'
    >>> G.nodes[1]["attr2"]
    3
    >>> G.nodes[2]
    {}

Note that if the dictionary contains nodes that are not in `G`, the
values are silently ignored::

    >>> G = nx.Graph()
    >>> G.add_node(0)
    >>> nx.set_node_attributes(G, {0: "red", 1: "blue"}, name="color")
    >>> G.nodes[0]["color"]
    'red'
    >>> 1 in G.nodes
    False

N)itemsr   KeyErrorrb   r   rP   _clear_cache)r0   valuesnamer9   re   rA   s         r1   r   r   9  s    d 	*'-yAGGAJt$ ' LLNDA
!!!$ #
 OOA    	*#)
4  	*  sE   B BB 
B>
BB BB $B;:B;>
CCc           	         Ub>  U R                   R                  5        VVs0 s H  u  p4X4R                  X5      _M     snn$ U R                   R                  5        VVs0 s H  u  p4X;   d  M  X4U   _M     snn$ s  snnf s  snnf )ar  Get node attributes from graph

Parameters
----------
G : NetworkX Graph

name : string
   Attribute name

default: object (default=None)
   Default value of the node attribute if there is no value set for that
   node in graph. If `None` then nodes without this attribute are not
   included in the returned dict.

Returns
-------
Dictionary of attributes keyed by node.

Examples
--------
>>> G = nx.Graph()
>>> G.add_nodes_from([1, 2, 3], color="red")
>>> color = nx.get_node_attributes(G, "color")
>>> color[1]
'red'
>>> G.add_node(4)
>>> color = nx.get_node_attributes(G, "color", default="yellow")
>>> color[4]
'yellow'
)r   r   rK   )r0   r   defaultr9   rA   s        r1   r   r     sm    > 45GGMMODODA55''ODD#$77==?@?41diJAwJ?@@ E@s   A;B.	B)r5   c                    Uc  U R                  5       nU H%  nU R                  SS9 H  u  pEXA;   d  M   XS	 M     M'     g! [         a     M%  f = f)a  Remove node attributes from all nodes in the graph.

Parameters
----------
G : NetworkX Graph

*attr_names : List of Strings
    The attribute names to remove from the graph.

nbunch : List of Nodes
    Remove the node attributes only from the nodes in this list.

Examples
--------
>>> G = nx.Graph()
>>> G.add_nodes_from([1, 2, 3], color="blue")
>>> nx.get_node_attributes(G, "color")
{1: 'blue', 2: 'blue', 3: 'blue'}
>>> nx.remove_node_attributes(G, "color")
>>> nx.get_node_attributes(G, "color")
{}
NTr   )r   r   )r0   r5   
attr_namesrm   r9   rA   s         r1   r   r     sX    0 ~GGG&DA{ ' 
   s   A  
AAc                 >   Ub}   U R                  5       (       a5  UR                  5        H   u  u  p4pV X`R                  U   U   U   U'   M"     OUR                  5        H  u  u  p4n X`R                  U   U   U'   M     OU R                  5       (       aB  UR                  5        H-  u  u  p4pX U R                  U   U   U   R                  U5        M/     O>UR                  5        H*  u  u  p4n U R                  U   U   R                  U5        M,     [        R                  " U 5        g! [         a     GM  f = f! [         a     M  f = f! [         a     U R                  SS9 H
  u  p4nXU'   M      Ncf = f! [         a     M  f = f! [         a     M  f = f)a  Sets edge attributes from a given value or dictionary of values.

.. Warning:: The call order of arguments `values` and `name`
    switched between v1.x & v2.x.

Parameters
----------
G : NetworkX Graph

values : scalar value, dict-like
    What the edge attribute should be set to.  If `values` is
    not a dictionary, then it is treated as a single attribute value
    that is then applied to every edge in `G`.  This means that if
    you provide a mutable object, like a list, updates to that object
    will be reflected in the edge attribute for each edge.  The attribute
    name will be `name`.

    If `values` is a dict or a dict of dict, it should be keyed
    by edge tuple to either an attribute value or a dict of attribute
    key/value pairs used to update the edge's attributes.
    For multigraphs, the edge tuples must be of the form ``(u, v, key)``,
    where `u` and `v` are nodes and `key` is the edge key.
    For non-multigraphs, the keys must be tuples of the form ``(u, v)``.

name : string (optional, default=None)
    Name of the edge attribute to set if values is a scalar.

Examples
--------
After computing some property of the edges of a graph, you may want
to assign a edge attribute to store the value of that property for
each edge::

    >>> G = nx.path_graph(3)
    >>> bb = nx.edge_betweenness_centrality(G, normalized=False)
    >>> nx.set_edge_attributes(G, bb, "betweenness")
    >>> G.edges[1, 2]["betweenness"]
    2.0

If you provide a list as the second argument, updates to the list
will be reflected in the edge attribute for each edge::

    >>> labels = []
    >>> nx.set_edge_attributes(G, labels, "labels")
    >>> labels.append("foo")
    >>> G.edges[0, 1]["labels"]
    ['foo']
    >>> G.edges[1, 2]["labels"]
    ['foo']

If you provide a dictionary of dictionaries as the second argument,
the entire dictionary will be used to update edge attributes::

    >>> G = nx.path_graph(3)
    >>> attrs = {(0, 1): {"attr1": 20, "attr2": "nothing"}, (1, 2): {"attr2": 3}}
    >>> nx.set_edge_attributes(G, attrs)
    >>> G[0][1]["attr1"]
    20
    >>> G[0][1]["attr2"]
    'nothing'
    >>> G[1][2]["attr2"]
    3

The attributes of one Graph can be used to set those of another.

    >>> H = nx.path_graph(3)
    >>> nx.set_edge_attributes(H, G.edges)

Note that if the dict contains edges that are not in `G`, they are
silently ignored::

    >>> G = nx.Graph([(0, 1)])
    >>> nx.set_edge_attributes(G, {(1, 2): {"weight": 2.0}})
    >>> (1, 2) in G.edges()
    False

For multigraphs, the `values` dict is expected to be keyed by 3-tuples
including the edge key::

    >>> MG = nx.MultiGraph()
    >>> edges = [(0, 1), (0, 1)]
    >>> MG.add_edges_from(edges)  # Returns list of edge keys
    [0, 1]
    >>> attributes = {(0, 1, 0): {"cost": 21}, (0, 1, 1): {"cost": 7}}
    >>> nx.set_edge_attributes(MG, attributes)
    >>> MG[0][1][0]["cost"]
    21
    >>> MG[0][1][1]["cost"]
    7

If MultiGraph attributes are desired for a Graph, you must convert the 3-tuple
multiedge to a 2-tuple edge and the last multiedge's attribute value will
overwrite the previous values. Continuing from the previous case we get::

    >>> H = nx.path_graph([0, 1, 2])
    >>> nx.set_edge_attributes(H, {(u, v): ed for u, v, ed in MG.edges.data()})
    >>> nx.get_edge_attributes(H, "cost")
    {(0, 1): 7}

NTr   )	r   r   _adjr   rb   r   r   rP   r   )	r0   r   r   ure   keyvaluer   rA   s	            r1   r   r     s   J 	$  *0,,.&KQ327q	!S)$/ +9 &,\\^MFQE-2q	!T* &4 ??"(,,.sFF1IaL%,,Q/ #1 $\\^	FF1IaL''* ,
 OOA5 $  $  	$gg4g0
d#T
 1	$      s   -E D-
E E (D?<E /$E=1!F-
D<7E ;D<<E ?
E	E EE 'E:9E:=
F
F
FFc                 $   U R                  5       (       a  U R                  SSS9nOU R                  SS9nUb(  U Vs0 s H  oDSS US   R                  X5      _M     sn$ U Vs0 s H  oAUS   ;   d  M  USS US   U   _M     sn$ s  snf s  snf )a  Get edge attributes from graph

Parameters
----------
G : NetworkX Graph

name : string
   Attribute name

default: object (default=None)
   Default value of the edge attribute if there is no value set for that
   edge in graph. If `None` then edges without this attribute are not
   included in the returned dict.

Returns
-------
Dictionary of attributes keyed by edge. For (di)graphs, the keys are
2-tuples of the form: (u, v). For multi(di)graphs, the keys are 3-tuples of
the form: (u, v, key).

Examples
--------
>>> G = nx.Graph()
>>> nx.add_path(G, [1, 2, 3], color="red")
>>> color = nx.get_edge_attributes(G, "color")
>>> color[(1, 2)]
'red'
>>> G.add_edge(3, 4)
>>> color = nx.get_edge_attributes(G, "color", default="yellow")
>>> color[(3, 4)]
'yellow'
Tkeysr   r   N)r   r   rK   )r0   r   r   r   xs        r1   r    r    p  s    B 	T-T":?@%Q#2"		$00%@@).@A!B%-AcrFAbE$K@@ A@s   !B%B5B)ebunchc                L   Uc4  U R                  5       (       a  U R                  SS9OU R                  5       nU HV  nU R                  5       (       a  U R                  SSS9OU R                  SS9nU H  Gt pV[        U5      U;   d  M   Xc	 M     MX     g! [         a     M/  f = f)a  Remove edge attributes from all edges in the graph.

Parameters
----------
G : NetworkX Graph

*attr_names : List of Strings
    The attribute names to remove from the graph.

Examples
--------
>>> G = nx.path_graph(3)
>>> nx.set_edge_attributes(G, {(u, v): u + v for u, v in G.edges()}, name="weight")
>>> nx.get_edge_attributes(G, "weight")
{(0, 1): 1, (1, 2): 3}
>>> remove_edge_attributes(G, "weight")
>>> nx.get_edge_attributes(G, "weight")
{}
NT)r   r   r   )r   r   tupler   )r0   r   r   rm   r   r   rA   s          r1   r!   r!     s    ( ~'('8'8d#aggi-.__->->AGGDG)AGGQUGDV 	 EQQx6! 	    s   
B
B#"B#c                     U R                  5       (       a,  [        U R                  U5      U R                  U5      5      nU$ U R	                  U5      nU$ )aB  Returns all of the neighbors of a node in the graph.

If the graph is directed returns predecessors as well as successors.

Parameters
----------
graph : NetworkX graph
    Graph to find neighbors.

node : node
    The node whose neighbors will be returned.

Returns
-------
neighbors : iterator
    Iterator of neighbors
)r   r   predecessors
successorsr   )r   noder   s      r1   r"   r"     sQ    $ u))$/1A1A$1GH M &Mr2   c                 z    U R                   R                  5       U R                   U   R                  5       -
  U1-
  $ )a"  Returns the non-neighbors of the node in the graph.

Parameters
----------
graph : NetworkX graph
    Graph to find neighbors.

node : node
    The node whose neighbors will be returned.

Returns
-------
non_neighbors : set
    Set of nodes in the graph that are not neighbors of the node.
)r   r   )r   r   s     r1   r#   r#     s3      ::??uzz$/4466$??r2   c              #     #    U R                  5       (       a!  U  H  n[        X5       H  nX4v   M
     M     g[        U 5      nU(       a6  UR                  5       nU[        X   5      -
   H  nX4v   M
     U(       a  M5  gg7f)zReturns the nonexistent edges in the graph.

Parameters
----------
graph : NetworkX graph.
    Graph to find nonexistent edges.

Returns
-------
non_edges : iterator
    Iterator of edges that are not in the graph.
N)r   r#   r   pop)r   r   re   r   s       r1   r$   r$     sr      A"5,f -  E
		AS]*f + es   A<B Bdirectedc                     X;  a  [         R                  " S5      eX ;  a  [         R                  " S5      eU R                  U   R                  5       U R                  U   R                  5       X1-
  -  $ )a  Returns the common neighbors of two nodes in a graph.

Parameters
----------
G : graph
    A NetworkX undirected graph.

u, v : nodes
    Nodes in the graph.

Returns
-------
cnbors : set
    Set of common neighbors of u and v in the graph.

Raises
------
NetworkXError
    If u or v is not a node in the graph.

Examples
--------
>>> G = nx.complete_graph(5)
>>> sorted(nx.common_neighbors(G, 0, 1))
[2, 3, 4]
zu is not in the graph.zv is not in the graph.)rP   rQ   r   r   )r0   r   re   s      r1   r%   r%     sb    8 	z788z78866!9>>affQinn.!777r2   r7   c                    ^ Ub4  U R                   " U6 nUc  SU< S3n[        R                  " U5      eTU;   $ [        U 5      (       a  g[	        U4S jU R                  SS9 5       5      $ )a  Returns True if `G` has weighted edges.

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

edge : tuple, optional
    A 2-tuple specifying the only edge in `G` that will be tested. If
    None, then every edge in `G` is tested.

weight: string, optional
    The attribute name used to query for edge weights.

Returns
-------
bool
    A boolean signifying if `G`, or the specified edge, is weighted.

Raises
------
NetworkXError
    If the specified edge does not exist.

Examples
--------
>>> G = nx.path_graph(4)
>>> nx.is_weighted(G)
False
>>> nx.is_weighted(G, (2, 3))
False

>>> G = nx.DiGraph()
>>> G.add_edge(1, 2, weight=1)
>>> nx.is_weighted(G)
True

Edge  does not exist.Fc              3   4   >#    U  H  u  pnTU;   v   M     g 7frD   rE   rF   r   re   r   r7   s       r1   rG   is_weighted.<locals>.<genexpr>X  s     C0B*!v~0Bs   Tr   )get_edge_datarP   rQ   r(   allr   r0   edger7   r   msgs     `  r1   r&   r&   &  sq    N %<$!12C""3''~{{CT0BCCCr2   )
edge_attrsc                    ^ UbC  U R                   " U6 nUc  SU< S3n[        R                  " U5      eTU;   =(       a    UT   S:  $ [        U4S jU R	                  SS9 5       5      $ )aw  Returns True if `G` has negatively weighted edges.

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

edge : tuple, optional
    A 2-tuple specifying the only edge in `G` that will be tested. If
    None, then every edge in `G` is tested.

weight: string, optional
    The attribute name used to query for edge weights.

Returns
-------
bool
    A boolean signifying if `G`, or the specified edge, is negatively
    weighted.

Raises
------
NetworkXError
    If the specified edge does not exist.

Examples
--------
>>> G = nx.Graph()
>>> G.add_edges_from([(1, 3), (2, 4), (2, 6)])
>>> G.add_edge(1, 2, weight=4)
>>> nx.is_negatively_weighted(G, (1, 2))
False
>>> G[2][4]["weight"] = -2
>>> nx.is_negatively_weighted(G)
True
>>> G = nx.DiGraph()
>>> edges = [("0", "3", 3), ("0", "1", -5), ("1", "0", -2)]
>>> G.add_weighted_edges_from(edges)
>>> nx.is_negatively_weighted(G)
True

r   r   r   c              3   R   >#    U  H  u  pnTU;   =(       a    UT   S :  v   M     g7f)r   NrE   r   s       r1   rG   )is_negatively_weighted.<locals>.<genexpr>  s,     XEWzqTv~2$v,"22EWs   $'Tr   )r   rP   rQ   anyr   r   s     `  r1   r'   r'   [  st    X %<$!12C""3''~2$v,"22XQWWRVWEWXXXr2   c                 R    [        U R                  R                  5       5      (       + $ )ab  Returns True if `G` has no edges.

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

Returns
-------
bool
    True if `G` has no edges, and False otherwise.

Notes
-----
An empty graph can have nodes but not edges. The empty graph with zero
nodes is known as the null graph. This is an $O(n)$ operation where n
is the number of nodes in the graph.

)r   r   r   r/   s    r1   r(   r(     s    ( 166==?###r2   c                 D    S U R                   R                  5        5       $ )a  Returns an iterator over nodes with self loops.

A node with a self loop has an edge with both ends adjacent
to that node.

Returns
-------
nodelist : iterator
    A iterator over nodes with self loops.

See Also
--------
selfloop_edges, number_of_selfloops

Examples
--------
>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1, 1)
>>> G.add_edge(1, 2)
>>> list(nx.nodes_with_selfloops(G))
[1]

c              3   8   #    U  H  u  pX;   d  M  Uv   M     g 7frD   rE   rF   r9   nbrss      r1   rG   'nodes_with_selfloops.<locals>.<genexpr>  s     9^'!qyAA^s   	)r   r   r/   s    r1   r*   r*     s    0 :QVV\\^99r2   c                    ^^ TSL a}  U R                  5       (       aG  USL a!  S U R                  R                  5        5       $ S U R                  R                  5        5       $ S U R                  R                  5        5       $ TSLa  U R                  5       (       aO  USL a%  UU4S jU R                  R                  5        5       $ UU4S jU R                  R                  5        5       $ UU4S jU R                  R                  5        5       $ U R                  5       (       aG  USL a!  S	 U R                  R                  5        5       $ S
 U R                  R                  5        5       $ S U R                  R                  5        5       $ )aV  Returns an iterator over selfloop edges.

A selfloop edge has the same node at both ends.

Parameters
----------
G : graph
    A NetworkX graph.
data : string or bool, optional (default=False)
    Return selfloop edges as two tuples (u, v) (data=False)
    or three-tuples (u, v, datadict) (data=True)
    or three-tuples (u, v, datavalue) (data='attrname')
keys : bool, optional (default=False)
    If True, return edge keys with each edge.
default : value, optional (default=None)
    Value used for edges that don't have the requested attribute.
    Only relevant if data is not True or False.

Returns
-------
edgeiter : iterator over edge tuples
    An iterator over all selfloop edges.

See Also
--------
nodes_with_selfloops, number_of_selfloops

Examples
--------
>>> G = nx.MultiGraph()  # or Graph, DiGraph, MultiDiGraph, etc
>>> ekey = G.add_edge(1, 1)
>>> ekey = G.add_edge(1, 2)
>>> list(nx.selfloop_edges(G))
[(1, 1)]
>>> list(nx.selfloop_edges(G, data=True))
[(1, 1, {})]
>>> list(nx.selfloop_edges(G, keys=True))
[(1, 1, 0)]
>>> list(nx.selfloop_edges(G, keys=True, data=True))
[(1, 1, 0, {})]
Tc              3   t   #    U  H.  u  pX;   d  M  X!   R                  5         H  u  p4XX44v   M     M0     g 7frD   )r   )rF   r9   r   krA   s        r1   rG   !selfloop_edges.<locals>.<genexpr>  sB      #1y ! !% 1L !0 !#1s   8'8c              3   p   #    U  H,  u  pX;   d  M  X!   R                  5         H	  nXU4v   M     M.     g 7frD   )r   )rF   r9   r   rA   s       r1   rG   r     sA      #1y  "W^^- 1I . #1s   6%6c              3   @   #    U  H  u  pX;   d  M  XX!   4v   M     g 7frD   rE   r   s      r1   rG   r     s     O>QYOQ47O>s   Fc           	   3      >#    U  H>  u  pX;   d  M  X!   R                  5         H  u  p4XX4R                  TT5      4v   M     M@     g 7frD   )r   rK   )rF   r9   r   r   rA   r   r   s        r1   rG   r     sO      #1y 4 !% 1eeD'23 !0 4#1s
   A	7A	c              3      >#    U  H<  u  pX;   d  M  X!   R                  5         H  nXUR                  TT5      4v   M     M>     g 7frD   )r   rK   )rF   r9   r   rA   r   r   s       r1   rG   r   	  sN      #1y 1 "W^^- 155w/0 . 1#1s
   A5Ac              3   b   >#    U  H$  u  pX;   d  M  XX!   R                  TT5      4v   M&     g 7frD   )rK   )rF   r9   r   r   r   s      r1   rG   r     s3      -GA9 3tw{{412-s   //c              3   T   #    U  H  u  pX;   d  M  X!     H	  nXU4v   M     M      g 7frD   rE   )rF   r9   r   r   s       r1   rG   r     s:      #1y  "W 1I % #1s   ((c              3   v   #    U  H/  u  pX;   d  M  [        [        X!   5      5        H  nX4v   M
     M1     g 7frD   )rI   len)rF   r9   r   rM   s       r1   rG   r     sA      #1y  #3tw<0 F 1 #1s   9(9c              3   :   #    U  H  u  pX;   d  M  X4v   M     g 7frD   rE   r   s      r1   rG   r   &  s     F.wqAIFQF.s   
)r   r   r   )r0   r   r   r   s    ` `r1   r)   r)     sG   T t|??t|#$66<<> #$66<<>  P166<<>OO	U	??t|#$66<<> #$66<<>  vv||~  ??t|#$66<<> #$66<<>  G!&&,,.FFr2   c                 N    [        S [        R                  " U 5       5       5      $ )ag  Returns the number of selfloop edges.

A selfloop edge has the same node at both ends.

Returns
-------
nloops : int
    The number of selfloops.

See Also
--------
nodes_with_selfloops, selfloop_edges

Examples
--------
>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1, 1)
>>> G.add_edge(1, 2)
>>> nx.number_of_selfloops(G)
1
c              3   &   #    U  H  nS v   M	     g7f)r>   NrE   )rF   _s     r1   rG   &number_of_selfloops.<locals>.<genexpr>?  s     /.Qq.s   )sumrP   r)   r/   s    r1   r+   r+   )  s     , /"++A.///r2   c                    ^   [        U 4S j[        R                  R                  U5       5       5      $ ! [        [
        4 a     gf = f)ak  Returns whether or not the specified path exists.

For it to return True, every node on the path must exist and
each consecutive pair must be connected via one or more edges.

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

path : list
    A list of nodes which defines the path to traverse

Returns
-------
bool
    True if `path` is a valid path in `G`

c              3   L   >#    U  H  u  pUTR                   U   ;   v   M     g 7frD   )r   )rF   r   nbrr0   s      r1   rG   is_path.<locals>.<genexpr>W  s"     P8O943!&&,&8Os   !$F)r   rP   utilsr   r   	TypeError)r0   paths   ` r1   r-   r-   B  s?    (P8I8I$8OPPPi  s   15 AAc                   ^ U R                  5       nSn[        R                  " X5      (       d  [        R                  " S5      e[        R                  R                  U5       H[  u  pVU(       a8  U[        U4S jU R                  U   U   R                  5        5       5      -  nMD  X@R                  U   U   T   -  nM]     U$ )a  Returns total cost associated with specified path and weight

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

path: list
    A list of node labels which defines the path to traverse

weight: string
    A string indicating which edge attribute to use for path cost

Returns
-------
cost: int or float
    An integer or a float representing the total cost with respect to the
    specified weight of the specified path

Raises
------
NetworkXNoPath
    If the specified edge does not exist.
r   zpath does not existc              3   ,   >#    U  H	  oT   v   M     g 7frD   rE   )rF   re   r7   s     r1   rG   path_weight.<locals>.<genexpr>|  s     F+Ea&	+Erg   )	r   rP   r-   NetworkXNoPathr   r   minr   r   )r0   r   r7   
multigraphcostr   r   s     `    r1   r,   r,   \  s    2 "JD::a 566XX&&t,	CF166$<+<+C+C+EFFFDFF4L%f--D	 -
 Kr2   rD   )NN)T)Nr7   )FFN)4__doc__collectionsr   	itertoolsr   networkxrP   networkx.utilsr   r   __all__r   r   r	   r   r   r   r   r
   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   _dispatchabler'   r(   r*   r)   r+   r-   r,   rE   r2   r1   <module>r      s<   C    8(V	$ FO.
=
/d$ .FN@D.+:\8Uv5Op+-.bJ!AH 37 !HGT'AT 37  F2@&2 Z  8 ! 8F2Dj X&2Y '2Yj$.:6cGL024#r2   