ó žÃÒYc@sdZddlZddlZddlZddlmZddlZddlmZdj dddgƒZ d d d d d dddgZ eddgƒdd„ƒZ ddd„Zdd„Zdd„Zdd„Zddd„Zded„Zded„Zd„ZdS(s1 Generators and functions for bipartite graphs. iÿÿÿÿN(treduce(tnodes_or_numbers sAric Hagberg (hagberg@lanl.gov)sPieter Swart (swart@lanl.gov)sDan Schult(dschult@colgate.edu)tconfiguration_modelthavel_hakimi_graphtreverse_havel_hakimi_graphtalternating_havel_hakimi_graphtpreferential_attachment_grapht random_graphtgnmk_random_graphtcomplete_bipartite_graphiicsî|dkrtjƒ}n.|jƒr9tjdƒ‚n|}|jƒ|\}}|\}‰t|tƒrgˆD]}||^qw‰n|j|ddƒ|jˆddƒ|j ‡fd†|Dƒƒd||f|j d<|S( s“Return the complete bipartite graph `K_{n_1,n_2}`. Composed of two partitions with `n_1` nodes in the first and `n_2` nodes in the second. Each node in the first is connected to each node in the second. Parameters ---------- n1 : integer Number of nodes for node set A. n2 : integer Number of nodes for node set B. create_using : NetworkX graph instance, optional Return graph of this type. Notes ----- Node labels are the integers 0 to `n_1 + n_2 - 1`. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. sDirected Graph not supportedt bipartiteiic3s(|]}ˆD]}||fVq qdS(N((t.0tutv(tbottom(s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pys Gsscomplete_bipartite_graph(%s,%s)tnameN( tNonetnxtGrapht is_directedt NetworkXErrortcleart isinstancetinttadd_nodes_fromtadd_edges_fromtgraph(tn1tn2t create_usingtGttopti((Rs~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyR !s      cCs|dkrtjƒ}n|jƒr9tjdƒ‚ntjd|ƒ}|dk rgtj|ƒnt|ƒ}t|ƒ}t |ƒ}t |ƒ}||ks¿tjd||fƒ‚nt |||ƒ}t |ƒdkrç|Sg} | j gt d|ƒD]} | g|| ^qƒg} g| D]} | D] } | ^q8q.} g} | j gt |||ƒD]} | g|| |^qmƒg}g| D]} | D] } | ^q¦qœ}tj| ƒtj|ƒ|jgt |ƒD]}| |||g^qèƒd|_|S(s(Return a random bipartite graph from two given degree sequences. Parameters ---------- aseq : list Degree sequence for node set A. bseq : list Degree sequence for node set B. create_using : NetworkX graph instance, optional Return graph of this type. seed : integer, optional Seed for random number generator. Nodes from the set A are connected to nodes in the set B by choosing randomly from the possible free stubs, one in A and one in B. Notes ----- The sum of the two sequences must be equal: sum(aseq)=sum(bseq) If no graph type is specified use MultiGraph with parallel edges. If you want a graph with no parallel edges use create_using=Graph() but then the resulting degree sequences might not be exact. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. sDirected Graph not supportedis4invalid degree sequences, sum(aseq)!=sum(bseq),%s,%stbipartite_configuration_modelN(Rtnetworkxt MultiGraphRRt empty_graphtrandomtseedtlentsumt_add_nodes_with_bipartite_labeltmaxtextendtrangetshuffleRR(taseqtbseqRR&RtlenatlenbtsumatsumbtstubsR tastubstsubseqtxtbstubsR ((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyRLs@         4&<&  4 cCsÛ|dkrtjƒ}n|jƒr9tjdƒ‚ntjd|ƒ}t|ƒ}t|ƒ}t|ƒ}t|ƒ}||ks£tjd||fƒ‚nt|||ƒ}t |ƒdkrË|Sgt d|ƒD]}|||g^qÛ} gt |||ƒD]}||||g^q } | j ƒx–| rÍ| j ƒ\} } | dkr`Pn| j ƒx]| | D]P} | d}|j | |ƒ| dcd8<| ddkrv| j| ƒqvqvWq8Wd|_|S(s1Return a bipartite graph from two given degree sequences using a Havel-Hakimi style construction. Nodes from the set A are connected to nodes in the set B by connecting the highest degree nodes in set A to the highest degree nodes in set B until all stubs are connected. Parameters ---------- aseq : list Degree sequence for node set A. bseq : list Degree sequence for node set B. create_using : NetworkX graph instance, optional Return graph of this type. Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. The sum of the two sequences must be equal: sum(aseq)=sum(bseq) If no graph type is specified use MultiGraph with parallel edges. If you want a graph with no parallel edges use create_using=Graph() but then the resulting degree sequences might not be exact. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. sDirected Graph not supportedis4invalid degree sequences, sum(aseq)!=sum(bseq),%s,%sitbipartite_havel_hakimi_graphN(RR"R#RRR$R'R(R)R*R,tsorttpoptadd_edgetremoveR(R.R/RRtnaseqtnbseqR2R3R R5R8tdegreeR ttarget((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyR›sB        ,4      cCsÝ|dkrtjƒ}n|jƒr9tjdƒ‚ntjd|ƒ}t|ƒ}t|ƒ}t|ƒ}t|ƒ}||ks£tjd||fƒ‚nt|||ƒ}t |ƒdkrË|Sgt d|ƒD]}|||g^qÛ} gt |||ƒD]}||||g^q } | j ƒ| j ƒxŽ| rÏ| j ƒ\} } | dkrjPnx_| d| !D]P} | d}|j | |ƒ| dcd8<| ddkrx| j| ƒqxqxWqBWd|_|S(s,Return a bipartite graph from two given degree sequences using a Havel-Hakimi style construction. Nodes from set A are connected to nodes in the set B by connecting the highest degree nodes in set A to the lowest degree nodes in set B until all stubs are connected. Parameters ---------- aseq : list Degree sequence for node set A. bseq : list Degree sequence for node set B. create_using : NetworkX graph instance, optional Return graph of this type. Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. The sum of the two sequences must be equal: sum(aseq)=sum(bseq) If no graph type is specified use MultiGraph with parallel edges. If you want a graph with no parallel edges use create_using=Graph() but then the resulting degree sequences might not be exact. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. sDirected Graph not supportedis4invalid degree sequences, sum(aseq)!=sum(bseq),%s,%sit$bipartite_reverse_havel_hakimi_graphN(RR"R#RRR$R'R(R)R*R,R:R;R<R=R(R.R/RRR0R1R2R3R R5R8R@R RA((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyRçsB        ,4      cCsa|dkrtjƒ}n|jƒr9tjdƒ‚ntjd|ƒ}t|ƒ}t|ƒ}t|ƒ}t|ƒ}||ks£tjd||fƒ‚nt|||ƒ}t |ƒdkrË|Sgt d|ƒD]}|||g^qÛ} gt |||ƒD]}||||g^q } x&| rS| j ƒ| j ƒ\} } | dkr`Pn| j ƒ| d| d!} | | | d}gt || ƒD]}|D] }|^q¨qž}t|ƒt| ƒt|ƒkrõ|j|j ƒƒnxX|D]P}|d}|j| |ƒ|dcd8<|ddkrü| j|ƒqüqüWq.Wd|_|S(s_Return a bipartite graph from two given degree sequences using an alternating Havel-Hakimi style construction. Nodes from the set A are connected to nodes in the set B by connecting the highest degree nodes in set A to alternatively the highest and the lowest degree nodes in set B until all stubs are connected. Parameters ---------- aseq : list Degree sequence for node set A. bseq : list Degree sequence for node set B. create_using : NetworkX graph instance, optional Return graph of this type. Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. The sum of the two sequences must be equal: sum(aseq)=sum(bseq) If no graph type is specified use MultiGraph with parallel edges. If you want a graph with no parallel edges use create_using=Graph() but then the resulting degree sequences might not be exact. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. sDirected Graph not supportedis4invalid degree sequences, sum(aseq)!=sum(bseq),%s,%siit(bipartite_alternating_havel_hakimi_graphN(RR"R#RRR$R'R(R)R*R,R:R;tziptappendR<R=R(R.R/RRR>R?R2R3R R5R8R@R tsmalltlargetzR7R4RA((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyR2sL        ,4    /"   c Cs|dkrtjƒ}n|jƒr9tjdƒ‚n|dkr[tjd|ƒ‚ntjd|ƒ}|dk r‰tj|ƒnt|ƒ}t ||dƒ}gt d|ƒD]}|g||^q·}x|róxÿ|drÞ|dd}|dj |ƒtjƒ|ks-|j ƒ|kr_|j ƒ} |j | ddƒ|j|| ƒqàgt ||j ƒƒD]} | g|j| ƒ^qu} td„| ƒ} tj| ƒ} |j | ddƒ|j|| ƒqàW|j |dƒq×Wd|_|S( sCreate a bipartite graph with a preferential attachment model from a given single degree sequence. Parameters ---------- aseq : list Degree sequence for node set A. p : float Probability that a new bottom node is added. create_using : NetworkX graph instance, optional Return graph of this type. seed : integer, optional Seed for random number generator. References ---------- .. [1] Jean-Loup Guillaume and Matthieu Latapy, Bipartite structure of all complex networks, Inf. Process. Lett. 90, 2004, pg. 215-221 http://dx.doi.org/10.1016/j.ipl.2004.03.007 Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. sDirected Graph not supportedisprobability %s > 1iR cSs||S(N((R7ty((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyt¹st'bipartite_preferential_attachment_modelN(RR"R#RRR$R%R&R'R)R,R=tnumber_of_nodestadd_nodeR<R@RtchoiceR( R.tpRR&RR>R tvvtsourceRAtbtbbtbbstubs((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyRs:      -  $ 8 c Cstjƒ}t|||ƒ}|r6tj|ƒ}nd|||f|_|dk rhtj|ƒn|dkrx|S|dkr”tj||ƒSt j d|ƒ}d}d}x—||krLt j dtjƒƒ} |dt | |ƒ}x0||kr%||kr%||}|d}qöW||kr¶|j |||ƒq¶q¶W|rüd}d}xš||krøt j dtjƒƒ} |dt | |ƒ}x0||krÑ||krÑ||}|d}q¢W||krb|j |||ƒqbqbWn|S(sÏReturn a bipartite random graph. This is a bipartite version of the binomial (ErdÅ‘s-Rényi) graph. Parameters ---------- n : int The number of nodes in the first bipartite set. m : int The number of nodes in the second bipartite set. p : float Probability for edge creation. seed : int, optional Seed for random number generator (default=None). directed : bool, optional (default=False) If True return a directed graph Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. The bipartite random graph algorithm chooses each of the n*m (undirected) or 2*nm (directed) possible edges with probability p. This algorithm is $O(n+m)$ where $m$ is the expected number of edges. The nodes are assigned the attribute 'bipartite' with the value 0 or 1 to indicate which bipartite set the node belongs to. See Also -------- gnp_random_graph, configuration_model References ---------- .. [1] Vladimir Batagelj and Ulrik Brandes, "Efficient generation of large random networks", Phys. Rev. E, 71, 036113, 2005. sfast_gnp_random_graph(%s,%s,%s)iigð?iÿÿÿÿN( RRR)tDiGraphRRR%R&R tmathtlogRR<( tntmROR&tdirectedRtlpR twtlr((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyRÃsD)        c Csrtjƒ}t|||ƒ}|r6tj|ƒ}nd|||f|_|dk rhtj|ƒn|dks€|dkr„|S||}||kr°tj ||d|ƒSg|j dt ƒD]"\}}|ddkrÃ|^qÃ}t t |ƒt |ƒƒ} d} x^| |krmtj|ƒ} tj| ƒ} | || krPqq|j| | ƒ| d7} qW|S(séReturn a random bipartite graph G_{n,m,k}. Produces a bipartite graph chosen randomly out of the set of all graphs with n top nodes, m bottom nodes, and k edges. Parameters ---------- n : int The number of nodes in the first bipartite set. m : int The number of nodes in the second bipartite set. k : int The number of edges seed : int, optional Seed for random number generator (default=None). directed : bool, optional (default=False) If True return a directed graph Examples -------- from networkx.algorithms import bipartite G = bipartite.gnmk_random_graph(10,20,50) See Also -------- gnm_random_graph Notes ----- This function is not imported in the main namespace. To use it you have to explicitly import the bipartite package. If k > m * n then a complete bipartite graph is returned. This graph is a bipartite version of the `G_{nm}` random graph model. s$bipartite_gnm_random_graph(%s,%s,%s)iRtdataR iN(R"RR)RRURRR%R&R tnodestTruetlisttsetRNR<( RXRYtkR&RZRt max_edgestdRRt edge_countR R ((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyRs.%    ;cCs†|jtd||ƒƒtttd|ƒdg|ƒƒ}|jttt|||ƒdg|ƒƒƒtj||dƒ|S(NiiR (RR,tdictRDtupdateRtset_node_attributes(RR0R1RR((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyR)Ys %0(t__doc__RVR%R"t functoolsRRtnetworkx.utilsRtjoint __author__t__all__RR RRRRRtFalseRRR)(((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/generators.pyts6       *O L K OBUA