K idZddlZddlmZgdZej dZedej dZededej d d d d Z y) z5Functions for computing and verifying regular graphs.N)not_implemented_for) is_regular is_k_regulark_factorct|dk(rtjdtjj |}|j s/|j |tfd|j DS|j|tfd|jD}|j|tfd|jD}|xr|S)aDetermines whether the graph ``G`` is a regular graph. A regular graph is a graph where each vertex has the same degree. A regular digraph is a graph where the indegree and outdegree of each vertex are equal. Parameters ---------- G : NetworkX graph Returns ------- bool Whether the given graph or digraph is regular. Examples -------- >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 1)]) >>> nx.is_regular(G) True rzGraph has no nodes.c3.K|] \}}|k(ywN).0_dd1s a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/regular.py zis_regular..&s0tq!270c3.K|] \}}|k(ywr r )r r r d_ins rrzis_regular..)s;tq!;rc3.K|] \}}|k(ywr r )r r r d_outs rrzis_regular..+s>A%1*>r) lennxNetworkXPointlessConceptutilsarbitrary_element is_directeddegreeall in_degree out_degree)Gn1 in_regular out_regularrrrs @@@rrr s0 1v{))*?@@  # #A &B ==? XXb\0qxx000{{2;q{{;;  R >>> )k)directedc@tfd|jDS)aDetermines whether the graph ``G`` is a k-regular graph. A k-regular graph is a graph where each vertex has degree k. Parameters ---------- G : NetworkX graph Returns ------- bool Whether the given graph is k-regular. Examples -------- >>> G = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)]) >>> nx.is_k_regular(G, k=3) False c3.K|] \}}|k(ywr r )r nr ks rrzis_k_regular..Fs+$!QqAv+r)rr)r r)s `rrr/s. +!((+ ++r$ multigraphT)preserve_edge_attrs returns_graphc ddlm}m}G fdd}Gdd}tfd|jDrt j d|j g}t jD]E\}} | d z kr || | } n || | } | j|j| G| d | } | | st j d  jD],} | | vs| d | df| vs j| d| d .|D]} | j S)uCompute a k-factor of G A k-factor of a graph is a spanning k-regular subgraph. A spanning k-regular subgraph of G is a subgraph that contains each vertex of G and a subset of the edges of G such that each vertex has degree k. Parameters ---------- G : NetworkX graph Undirected graph matching_weight: string, optional (default='weight') Edge data key corresponding to the edge weight. Used for finding the max-weighted perfect matching. If key not found, uses 1 as weight. Returns ------- G2 : NetworkX graph A k-factor of G Examples -------- >>> G = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)]) >>> G2 = nx.k_factor(G, k=1) >>> G2.edges() EdgeView([(1, 2), (3, 4)]) References ---------- .. [1] "An algorithm for computing simple k-factors.", Meijer, Henk, Yurai Núñez-Rodríguez, and David Rappaport, Information processing letters, 2009. r)is_perfect_matchingmax_weight_matchingc$eZdZdZdZfdZy)k_factor..LargeKGadgetc||_||_||_||_t |Dcgc]}||fc}|_t ||z Dcgc] }|||zf c}|_ycc}wcc}wr )originalgr)rrangeouter_vertices core_verticesselfr)rnoder4xs r__init__z'k_factor..LargeKGadget.__init__tsg DMDFDF DK6;Fm"DD!9"DD >CFQJ>O!P4V"4!PD #E!Ps A$ A)c|j|j}t|j}t|j }t |j ||D]$\}}}|jj||fi|&|jD]/}|j D]}|jj|| 1|jj|jyr ) r4r3listkeysvalueszipr6add_edger7 remove_node)r9adj_view neighbors edge_attrsouterneighborcores r replace_nodez+k_factor..LargeKGadget.replace_node}svvdmm,HX]]_-Ihoo/0J/2##Y 0 ?+x x>:> ?** 1!001EFFOOD%01 1 FF  t}} -r$c|jj|j|jD]j}|j|}t |j D]=\}}||j vs|jj|j|fi|jlj|jj|j yr ) r4add_noder3r6r>itemsr7rBremove_nodes_from)r9rGrDrHrFr4s r restore_nodez+k_factor..LargeKGadget.restore_nodes FFOODMM *,, 66%=,01A,B(Hjt'9'99' xN:N     3 3 4   2 2 3r$N__name__ __module__ __qualname__r<rJrO)r4sr LargeKGadgetr1ss Q . 4r$rTceZdZdZdZdZy)k_factor..SmallKGadgetc,||_||_||_||_t |Dcgc]}||fc}|_t |Dcgc] }|||zf c}|_t |Dcgc] }||d|zzfc}|_ycc}wcc}wcc}w)N)r3r)rr4r5r6inner_verticesr7r8s rr<z'k_factor..SmallKGadget.__init__s DMDF DKDF6;Fm"DD!9"DD ?DV}"M!D!f*#5"MD BG(!KQ4QZ"8!KD #E"M!Ks B B -Bc|j|j}t|j|jt |j D]C\}}\}}|jj|||jj||fi|E|jD]/}|jD]}|jj|| 1|jj|jyr ) r4r3rAr6rYr>rMrBr7rC)r9rDrGinnerrHrFrIs rrJz+k_factor..SmallKGadget.replace_nodesvvdmm,H8;##T%8%8$x~~?O:P9 ?4u4xu-x>:>  ? ** 1!001EFFOOD%01 1 FF  t}} -r$c |jj|j|jD]a}|j|}|j D]=\}}||j vs|jj |j|fi|ac|jj|j|jj|j|jj|j yr ) r4rLr3r6rMr7rBrNrY)r9rGrDrHrFs rrOz+k_factor..SmallKGadget.restore_nodes FFOODMM *,, 66%=,4NN,<(Hjt'9'99' xN:N  FF $ $T%8%8 9 FF $ $T%8%8 9 FF $ $T%7%7 8r$NrPr r$r SmallKGadgetrVs L . 9r$r]c3.K|] \}}|kywr r )r r r r)s rrzk_factor..s &TQ1q5 &rz/Graph contains a vertex with degree less than kg@T)maxcardinalityweightz7Cannot find k-factor because no perfect matching exists)networkx.algorithms.matchingr.r/anyrrNetworkXUnfeasiblecopyr>rJappendedges remove_edgerO)r r)matching_weightr.r/rTr]gadgetsr:rgadgetmatchingedger4s ` @rrrIsVPV 4 4D!9!9H &QXX &&##$UVV AGQXX f v| !!VT15F!!VT15Fv #1T/RH q( +## E   , x T!Wd1g$6h$F MM$q'47 +, Hr$)r`) __doc__networkxrnetworkx.utilsr__all__ _dispatchablerrrr r$rrss;. 4"*"*JZ ,!,0Z \"d$?K @#!K r$