K idZddlmZddlmZddlZddlmZm Z gdZ dZ d;dZ dd3Z,ejBd2(d>d4Z-ejBd5Z.d6Z/d?d7Z0ejBd8Z1d9Z2d:Z3y)@z=Functional interface to graph methods and assorted utilities.)Counter)chainN)not_implemented_forpairwise)'nodesedgesdegreedegree_histogram neighborsnumber_of_nodesnumber_of_edgesdensity is_directedfreeze is_frozensubgraphinduced_subgraph edge_subgraphrestricted_view to_directed to_undirectedadd_staradd_path add_cyclecreate_empty_copyset_node_attributesget_node_attributesremove_node_attributesset_edge_attributesget_edge_attributesremove_edge_attributes all_neighbors non_neighbors non_edgescommon_neighbors is_weightedis_negatively_weightedis_emptyselfloop_edgesnodes_with_selfloopsnumber_of_selfloops path_weightis_pathc"|jS)z{Returns a NodeView over the graph nodes. This function wraps the :func:`G.nodes ` property. )rGs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/classes/function.pyrr4s 779c$|j|S)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 ` property. )rr0nbunchs r1rr<s 776?r2c&|j||S)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 ` property. )r )r0r5weights r1r r Hs 88FF ##r2c$|j|S)zReturns an iterator over all neighbors of node n. This function wraps the :func:`G.neighbors ` function. )r )r0ns r1r r Qs ;;q>r2c"|jS)zReturns the number of nodes in the graph. This function wraps the :func:`G.number_of_nodes ` function. )r r/s r1r r Y   r2c"|jS)zReturns the number of edges in the graph. This function wraps the :func:`G.number_of_edges ` function. )r r/s r1r r ar;r2ct|}t|}|dk(s|dkry|||dz zz }|js|dz}|S)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)r0r9mds r1rrisQ2 AAAva Q!a%[A ==? Q Hr2ctd|jD}t|rt|dzndDcgc]}|j |dc}Scc}w)avReturns 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)) c3&K|] \}}| ywN).0r9rAs r1 z#degree_histogram..s.41aQ.sr>r)rr rangemaxget)r0countsis r1r r sJ&.188:. .F&+vCK!O1&M NFJJq!  NN NsAc"|jS)z!Return True if graph is directed.)rr/s r1rrs ==?r2c,tjd)zCDummy method for raising errors when trying to modify frozen graphszFrozen graph can't be modified)nx NetworkXError)argskwargss r1frozenrSs  ; <>> 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) rSadd_nodeadd_nodes_from remove_noderemove_nodes_fromadd_edgeadd_edges_fromadd_weighted_edges_from remove_edgeremove_edges_fromclear clear_edgesr/s r1rrscFAJAAM AAJA &AAM AAGAMAH Hr2c: |jS#t$rYywxYw)zReturns True if graph is frozen. Parameters ---------- G : graph A NetworkX graph See Also -------- freeze F)rSAttributeErrorr/s r1rrs"xx s  c t|} t||jfd|D}|j|fi|y#t$rYywxYw)aPAdd 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) Nc3&K|]}|f ywrDrE)rFr9vs r1rGzadd_star..s #aV #s)iternext StopIterationrUrZ) G_to_add_tonodes_for_starattrnlistrrds @r1rrs`2  E K #U #EKu-- s A AAc t|} t|}|j||jt t |f|fi|y#t$rYywxYw)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) NrerfrgrUrZrr)rhnodes_for_pathrjrk first_nodes r1rrsc.  E%[ $KxzmU(CDMM s A AAc t|} t|}|j||jt t |f|dfi|y#t$rYywxYw)apAdd 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)cyclicrm)rhnodes_for_cyclerjrkros r1rr4sk.  !E%[ $K }e,T:>B s A AAc$|j|S)a9Returns 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() )rr4s r1rrVs( ::f r2ctjj|j|}tj||S)aReturns 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)rOfilters show_nodes nbunch_iter subgraph_view)r0r5 induced_nodess r1rrms5TJJ))!--*?@M  A= 99r2ctj}t|}t}|D]}|j|dd|j |}|j r4|j r|j|}nE|j|}n3|j r|j|}n|j|}tj|||S)aReturns 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?ru filter_edge) rOrvsetupdaterw is_multigraphrshow_multidiedgesshow_multiedges show_diedges show_edgesry)r0rnxfrerz induced_edgess r1rrsP **C JE EE  QrUNN5)M ==?11%8M//6M ==?,,U3MNN51M  A=m TTr2cbtj}|j|}|jr4|j r|j |}nE|j |}n3|j r|j|}n|j|}tj|||S)aReturns 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|) rOrv hide_nodesrrhide_multidiedgeshide_multiedges hide_diedges hide_edgesry)r0rrrrrs r1rrsR **C&J ==?..u5J,,U3J ==?))%0J.J  A:: NNr2c&|jdS)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. Tas_view)rgraphs r1rrs   T  **r2c&|jdS)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)rrs r1rrs   t  ,,r2Tc|j}|j|j||r%|jj |j|S)aReturns 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__rVrrr)r0 with_dataHs r1rr"sD AQWW)W,- qww Hr2)preserve_node_attrs mutates_inputc|0 |jD]\}} |||j||<n7|jD]$\}} |j|j |&t j |y#t$rYwwxYw#t$r|D]}||j||<YIwxYw#t$rYwxYw)aU 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)itemsrKeyErrorrarrO _clear_cache)r0valuesnamer9rdrAs r1rr9sf  *  1'-ayAGGAJt$ LLN DAq  !!!$  OOA  * *#) 4  * *  sEBBB B7 B  B B  B"B43B47 CCrdefault) node_attrsc |>|jjDcic]\}}||j||c}}S|jjDcic]\}}||vs |||c}}Scc}}wcc}}w)aGet 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' )rrrJ)r0rrr9rAs r1rrsp@45GGMMODDAq155w''DD#$77==? @41adaiAqwJ @@E @sA7 A=+A=)r5c||j}|D]%}|jdD]\}}||vs ||='y#t$rYwxYw)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)rr)r0r5 attr_namesrjr9rAs r1rrsh2~GGG& DAqF{$   s> A  A )preserve_edge_attrsrc|y |jr6|jD]"\\}}}} ||j||||<$n1|jD]\\}}} ||j|||< n|jrB|jD].\\}}}} |j|||j |0n=|jD]*\\}}} |j||j |,tj|y#t$rY wxYw#t$rYwxYw#t$r#|j dD] \}}}|||< Y`wxYw#t$rYwxYw#t$rYwxYw)aMSets 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) rr_adjrrarrrOr) r0rrurdkeyvaluerrAs r1rrsL  $ *0,,.&KQ327q ! S)$/ &,\\^MFQE-2q ! T* ?? "(,,.  AsQFF1IaL%,,Q/  $\\^  AFF1IaL''*  OOA5$ $ $gg4g0 $ 1d#T  $ $   s|*ED!E$D19E'$E/(!E>! D.*E-D..E1 D=:E<D==E)E,+E,/ E;:E;> F  F ) edge_attrsc|jr|jdd}n|jd}|'|Dcic]}|dd|dj||c}S|Dcic]}||dvs |dd|d|c}Scc}wcc}w)aeGet 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' TkeysrrN)rrrJ)r0rrrxs r1r r tsD T-T":?@Q#2" $00@@). @A$!B%-AcrFAbE$K  @@A @s B$ B1B)ebunchc:|2|jr|jdn|j}|D]S}|jr|jddn|jd}|D]^}}t||vs ||=Uy#t$rY(wxYw)aRemove 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)rrr)rrtupler)r0rrrjrrrAs r1r!r!s*~'('8d#aggi -.__->AGGDG )AGGQUGDV  EQQx6!$   sB BBc|jr,t|j||j|}|S|j |}|S)arReturns 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 )rr predecessors successorsr )rnoders r1r"r"sN$ u))$/1A1A$1GH M& Mr2cz|jj|j|jz |hz S)aNReturns 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. )rr)rrs r1r#r#s3 ::?? uzz$/446 6$ ??r2c#K|jr|D]}t||D]}||f yt|}|r0|j}|t||z D]}||f |r/yyw)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)rr#r~pop)rrrdrs r1r$r$s  A"5!, !f   E  ASq]* !f  s A+A0.A0directedc||vrtjd||vrtjd|j|j|j|j||hz zS)aReturns 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] zu is not in the graph.zv is not in the graph.)rOrPrr)r0rrds r1r%r%sh8 z788z788 66!9>> affQinn.!Q7 77r2)rr7c|0|j|}|d|d}tj||vSt|ryt fd|j dDS)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.Fc3.K|] \}}}|vywrDrErFrrdrr7s r1rGzis_weighted.._sC*!Qv~CsTr) get_edge_datarOrPr(allrr0edger7rmsgs ` r1r&r&,sqP q% <$!12C""3' '~{ CT0BC CCr2c|:|j|}|d|d}tj||vxr|dkStfd|j dDS)aReturns 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 rrrc3BK|]\}}}|vxr|dkyw)rNrErs r1rGz)is_negatively_weighted..s,Xzq!Tv~2$v,"22XsTr)rrOrPanyrrs ` r1r'r'bsvX q% <$!12C""3' '~2$v,"22 XQWWRVWEWX XXr2cJt|jj S)aReturns 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. )rrrr/s r1r(r(s*166==?# ##r2cDd|jjDS)aReturns 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] c30K|]\}}||vs |ywrDrErFr9nbrss r1rGz'nodes_with_selfloops..s 9'!TqDyA 9s )rrr/s r1r*r*s0 :QVV\\^ 99r2cdurw|jrF|dur!d|jjDSd|jjDSd|jjDSdur|jrL|dur$fd|jjDSfd|jjDSfd|jjDS|jrF|dur!d |jjDSd |jjDSd |jjDS) aReturns 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, {})] Tc3lK|],\}}||vr#||jD] \}}||||f.ywrD)r)rFr9rkrAs r1rGz!selfloop_edges..sN4Dy $Q  11aL s24c3dK|](\}}||vr||jD] }|||f *ywrD)r)rFr9rrAs r1rGz!selfloop_edges..sI4Dy!!W^^- 1Is.0c3<K|]\}}||vs ||||fywrDrErs r1rGz!selfloop_edges..s$O4Q$YQ47OOs  Fc 3K|]<\}}||vr3||jD]\}}||||jf>ywrD)rrJ)rFr9rrrArrs r1rGz!selfloop_edges.. s[4Dy $Q  11aeeD'233sAAc3K|]8\}}||vr/||jD]}|||jf:ywrD)rrJ)rFr9rrArrs r1rGz!selfloop_edges..sV4Dy!!W^^- 155w/00s>Ac3\K|]#\}}||vr||||jf%ywrD)rJ)rFr9rrrs r1rGz!selfloop_edges..s:At9AtAw{{412s),c3HK|]\}}||vr||D] }|||f ywrDrE)rFr9rrs r1rGz!selfloop_edges.. sB4Dy!!W 1Is "c3jK|]+\}}||vr"tt||D]}||f -ywrD)rHlen)rFr9rrLs r1rGz!selfloop_edges..'sI4Dy"3tAw<0 Fs13c34K|]\}}||vs ||fywrDrErs r1rGz!selfloop_edges...sFwq$AIQFFs  )rrr)r0rrrs ` `r1r)r)s>T t| ?? t|#$66<<>#$66<<>P166<<>O O U  ?? t|#$66<<>#$66<<> vv||~  ?? t|#$66<<>#$66<<>G!&&,,.F Fr2cLtdtj|DS)aReturns 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 c3 K|]}dyw)r>NrE)rF_s r1rGz&number_of_selfloops..Hs/Qq/s )sumrOr)r/s r1r+r+1s . /"++A./ //r2c tfdtjj|DS#tt f$rYywxYw)aReturns 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` c3FK|]\}}|j|vywrD)r)rFrnbrr0s r1rGzis_path..`s"P943!&&,&Ps!F)rrOutilsrr TypeError)r0paths` r1r-r-Ks?(P8I8I$8OPPP i s04AAcp|j}d}tj||stjdtjj |D]U\}}|r6|t fd|j||jDz }>||j||z }W|S)a8Returns 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. rzpath does not existc3(K|] }| ywrDrE)rFrdr7s r1rGzpath_weight..sFa& Fs) rrOr-NetworkXNoPathrrminrr)r0rr7 multigraphcostrrs ` r1r,r,es2"J D ::a  566XX&&t,. c  CF166$<+<+C+C+EFF FD AFF4L%f- -D . Kr2rD)NN)T)Nr7)FFN)4__doc__ collectionsr itertoolsrnetworkxrOnetworkx.utilsrr__all__rrr r r r rr rrSrrrrrrrrrrrr _dispatchablerrrrr r!r"r#r$r%r&r'r(r*r)r+r-r,rEr2r1rsCC8( V $  FO. = / d$ .FN@D.+:\8Uv5Op+- .d$?b@bJfi01!A2!AHd$?26!@!Hd$?G@GTfi01'A2'ATd$?26 @ F2@&2Z  8! 8Fd+2D,2DjX&2Y'2Yj$$.:6cGL0024#r2