Yc@sdZddlmZddlZddlZdjdddgZdd d d d d gZddZ dZ dZ dZ dZ dZdZdS(sTest sequences for graphiness. i(t defaultdictNs sAric Hagberg (hagberg@lanl.gov)sPieter Swart (swart@lanl.gov)sDan Schult (dschult@colgate.edu)Joel Miller (joel.c.miller.research@gmail.com)Ben EdwardsBrian Cloteaux t is_graphicaltis_multigraphicaltis_pseudographicaltis_digraphicalt%is_valid_degree_sequence_erdos_gallait%is_valid_degree_sequence_havel_hakimitegcCs[|dkr!tt|}n6|dkrBtt|}nd}tj||S(s@Returns True if sequence is a valid degree sequence. A degree sequence is valid if some graph can realize it. Parameters ---------- sequence : list or iterable container A sequence of integer node degrees method : "eg" | "hh" The method used to validate the degree sequence. "eg" corresponds to the Erdős-Gallai algorithm, and "hh" to the Havel-Hakimi algorithm. Returns ------- valid : bool True if the sequence is a valid degree sequence and False if not. Examples -------- >>> G = nx.path_graph(4) >>> sequence = (d for n, d in G.degree()) >>> nx.is_graphical(sequence) True References ---------- Erdős-Gallai [EG1960]_, [choudum1986]_ Havel-Hakimi [havel1955]_, [hakimi1962]_, [CL1996]_ Rthhs`method` must be 'eg' or 'hh'(RtlistRtnxtNetworkXException(tsequencetmethodtvalidtmsg((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyRs#  cCstjj|stjnt|}dg|}d|ddf\}}}}x|D]}|dksz||krtjq\|dkr\t||t|||||df\}}}}||cd7= \frac{(\max(d) + \min(d) + 1)^2}{4*\min(d)} then d is graphical. This was shown in Theorem 6 in [1]_. References ---------- .. [1] I.E. Zverovich and V.E. Zverovich. "Contributions to the theory of graphic sequences", Discrete Mathematics, 105, pp. 292-303 (1992). [havel1955]_, [hakimi1962]_, [CL1996]_ iii(RR RtFalsetTruetrange( RRRRRRtmodstubstmslentktitstub((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyR_s4"4  %c Cssyt|\}}}}}Wntjk r6tSX|dkskd||||d||dkrotSd\}}}} xt||ddD]} | |dkrtS|| dkr|| } | || kr| |} n|| | 7}x@t| D]2} |||| 7}| || ||| 7} qW|| 7}|||d||| krktSqqWtS(s7Returns True if deg_sequence can be realized by a simple graph. The validation is done using the Erdős-Gallai theorem [EG1960]_. Parameters ---------- deg_sequence : list A list of integers Returns ------- valid : bool True if deg_sequence is graphical and False if not. Notes ----- This implementation uses an equivalent form of the Erdős-Gallai criterion. Worst-case run time is $O(n)$ where $n$ is the length of the sequence. Specifically, a sequence d is graphical if and only if the sum of the sequence is even and for all strong indices k in the sequence, .. math:: \sum_{i=1}^{k} d_i \leq k(k-1) + \sum_{j=k+1}^{n} \min(d_i,k) = k(n-1) - ( k \sum_{j=0}^{k-1} n_j - \sum_{j=0}^{k-1} j n_j ) A strong index k is any index where d_k >= k and the value n_j is the number of occurrences of j in d. The maximal strong index is called the Durfee index. This particular rearrangement comes from the proof of Theorem 3 in [2]_. The ZZ condition says that for the sequence d if .. math:: |d| >= \frac{(\max(d) + \min(d) + 1)^2}{4*\min(d)} then d is graphical. This was shown in Theorem 6 in [2]_. References ---------- .. [1] A. Tripathi and S. Vijay. "A note on a theorem of Erdős & Gallai", Discrete Mathematics, 265, pp. 417-420 (2003). .. [2] I.E. Zverovich and V.E. Zverovich. "Contributions to the theory of graphic sequences", Discrete Mathematics, 105, pp. 292-303 (1992). [EG1960]_, [choudum1986]_ iiii(iiii(RR RRR R!( RRRRRRR$tsum_degtsum_njtsum_jnjtdktrun_sizetv((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyRs,34     cCst|}tjj|s"tSd\}}x8|D]0}|dkrKtS||t||}}q5W|ds|d|krtStS(s0Returns True if some multigraph can realize the sequence. Parameters ---------- deg_sequence : list A list of integers Returns ------- valid : bool True if deg_sequence is a multigraphic degree sequence and False if not. Notes ----- The worst-case run time is $O(n)$ where $n$ is the length of the sequence. References ---------- .. [1] S. L. Hakimi. "On the realizability of a set of integers as degrees of the vertices of a linear graph", J. SIAM, 10, pp. 496-506 (1962). ii(ii(R R RRRRR (R RRRR((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyRs    cCsHt|}tjj|s"tSt|ddkoGt|dkS(sReturns True if some pseudograph can realize the sequence. Every nonnegative integer sequence with an even sum is pseudographical (see [1]_). Parameters ---------- sequence : list or iterable container A sequence of integer node degrees Returns ------- valid : bool True if the sequence is a pseudographic degree sequence and False if not. Notes ----- The worst-case run time is $O(n)$ where n is the length of the sequence. References ---------- .. [1] F. Boesch and F. Harary. "Line removal algorithms for graphs and their degree lists", IEEE Trans. Circuits and Systems, CAS-23(12), pp. 778-782 (1976). ii(R R RRRtsumR(R ts((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyRs cCs>t|}t|}tjj|s.tStjj|sDtSddt|t|f\}}}}t||}d} |dkrtSgg} } xt|D]} d\} }| |kr|| }n| |kr|| } n| dks |dkr tS|| ||t| | }}} | dkr\| j d|d| fq|dkr| j d|qqW||krtSt j | t j | dg| d}x|| r9t j | \}}|d9}|t| t| krtSd}xt|D]}| rY| sA| dd| dkrYt j | }d}nt j | \}}|dkr~tS|ddks|dkr|d|f||<|d7}qqWxUt|D]G}||}|ddkrt j | |qt j | |dqW|dkrt j | |qqWtS(s4Returns True if some directed graph can realize the in- and out-degree sequences. Parameters ---------- in_sequence : list or iterable container A sequence of integer node in-degrees out_sequence : list or iterable container A sequence of integer node out-degrees Returns ------- valid : bool True if in and out-sequences are digraphic False if not. Notes ----- This algorithm is from Kleitman and Wang [1]_. The worst case runtime is $O(s \times \log n)$ where $s$ and $n$ are the sum and length of the sequences respectively. References ---------- .. [1] D.J. Kleitman and D.L. Wang Algorithms for Constructing Graphs and Digraphs with Given Valences and Factors, Discrete Mathematics, 6(1), pp. 79-88 (1973) iii(ii(ii(R R RRRRRR R!tappendtheapqtheapifytheappoptheappush(t in_sequencet out_sequencetin_deg_sequencetout_deg_sequencetsumintsumouttnintnouttmaxntmaxintstubheaptzeroheapRtin_degtout_degR"tfreeouttfreeinR#R%tstubouttstubinR&((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyR9sl  *       %       %    (t__doc__t collectionsRR0tnetworkxR tjoint __author__t__all__RRRRRRR(((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/graphical.pyts&      -  H N $