ó ŸÃÒYc@sÍdZddlZddlZddlZddlZdjdddgƒZddd d d d d dgZd„Z d„Z dd„Z de d„Zde d„Ze dd„Zd„Zd„ZdS(sBGenerators for classes of graphs used in studying social networks.iÿÿÿÿNs s!Ben Edwards (bedwards@cs.unm.edu)sAric Hagberg (hagberg@lanl.gov)s2Konstantinos Karakatsanis t caveman_graphtconnected_caveman_graphtrelaxed_caveman_graphtrandom_partition_graphtplanted_partition_graphtgaussian_random_partition_graphtring_of_cliquestwindmill_graphcCsstj||ƒ}|dkroxMtd|||ƒD]2}tjt|||ƒdƒ}|j|ƒq6Wn|S(s8Returns a caveman graph of `l` cliques of size `k`. Parameters ---------- l : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.caveman_graph(3, 3) See also -------- connected_caveman_graph References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. iii(tnxt empty_graphtranget itertoolst combinationstadd_edges_from(tltktGtstarttedges((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRs & cCsgtj||ƒ}xNtd|||ƒD]6}|j||dƒ|j||d||ƒq)W|S(sÄReturns a connected caveman graph of `l` cliques of size `k`. The connected caveman graph is formed by creating `n` cliques of size `k`, then a single edge in each clique is rewired to a node in an adjacent clique. Parameters ---------- l : int number of cliques k : int size of cliques Returns ------- G : NetworkX Graph connected caveman graph Notes ----- This returns an undirected graph, it can be converted to a directed graph using :func:`nx.to_directed`, or a multigraph using ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is described in [1]_ and it is unclear which of the directed generalizations is most useful. Examples -------- >>> G = nx.connected_caveman_graph(3, 3) References ---------- .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.' Amer. J. Soc. 105, 493-527, 1999. ii(RRR t remove_edgetadd_edge(RRRR((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRDs $ c Cs·|dk rtj|ƒntj||ƒ}t|ƒ}xv|jƒD]h\}}tjƒ|krGtj|ƒ}|j||ƒrŒqGn|j ||ƒ|j ||ƒqGqGW|S(sSReturn a relaxed caveman graph. A relaxed caveman graph starts with `l` cliques of size `k`. Edges are then randomly rewired with probability `p` to link different cliques. Parameters ---------- l : int Number of groups k : int Size of cliques p : float Probabilty of rewiring each edge. seed : int,optional Seed for random number generator(default=None) Returns ------- G : NetworkX Graph Relaxed Caveman Graph Raises ------ NetworkXError: If p is not in [0,1] Examples -------- >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42) References ---------- .. [1] Santo Fortunato, Community Detection in Graphs, Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174. https://arxiv.org/abs/0906.0612 N( tNonetrandomtseedRRtlistRtchoicethas_edgeRR( RRtpRRtnodestutvtx((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRos%  csŠ|d k rtj|ƒnd|ko3dknsJtjdƒ‚nd|koadknsxtjdƒ‚n|rtjƒ}n tjƒ}g|jd>> G = nx.random_partition_graph([10,10,10],.25,.01) >>> len(G) 30 >>> partition = G.graph['partition'] >>> len(partition) 3 Notes ----- This is a generalization of the planted-l-partition described in [1]_. It allows for the creation of groups of any size. The partition is store as a graph attribute 'partition'. References ---------- .. [1] Santo Fortunato 'Community Detection in Graphs' Physical Reports Volume 486, Issue 3-5 p. 75-174. https://arxiv.org/abs/0906.0612 ggð?sp_in must be in [0,1]sp_out must be in [0,1]t partitionic3s)|]\}}|ˆ|ˆfVqdS(N((t.0RR(R(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pys ístdirectediN(RRRRt NetworkXErrortDiGraphtGraphtgraphtsumtadd_nodes_fromR tfast_gnp_random_graphRR tupdatetdicttfromkeystappendtsettlentziptmathtlogtinttgetR(tsizestp_intp_outRR"Rtnt next_grouptgroupRttargetstlpRRtlr((Rss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyR¢sl6      " *'    #*     cCst|g|||||ƒS(sFReturn the planted l-partition graph. This model partitions a graph with n=l*k vertices in l groups with k vertices each. Vertices of the same group are linked with a probability p_in, and vertices of different groups are linked with probability p_out. Parameters ---------- l : int Number of groups k : int Number of vertices in each group p_in : float probability of connecting vertices within a group p_out : float probability of connected vertices between groups seed : int,optional Seed for random number generator(default=None) directed : bool,optional (default=False) If True return a directed graph Returns ------- G : NetworkX Graph or DiGraph planted l-partition graph Raises ------ NetworkXError: If p_in,p_out are not in [0,1] or Examples -------- >>> G = nx.planted_partition_graph(4, 3, 0.5, 0.1,seed=42) See Also -------- random_partition_model References ---------- .. [1] A. Condon, R.M. Karp, Algorithms for graph partitioning on the planted partition model, Random Struct. Algor. 18 (2001) 116-140. .. [2] Santo Fortunato 'Community Detection in Graphs' Physical Reports Volume 486, Issue 3-5 p. 75-174. https://arxiv.org/abs/0906.0612 (R(RRR6R7RR"((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRs2c CsÁ||krtjdƒ‚nd}g}x~trªttj|t|ƒ|dƒƒ} | dkrkq-n|| |kr|j||ƒPn|| 7}|j| ƒq-Wt|||||ƒS(s'Generate a Gaussian random partition graph. A Gaussian random partition graph is created by creating k partitions each with a size drawn from a normal distribution with mean s and variance s/v. Nodes are connected within clusters with probability p_in and between clusters with probability p_out[1] Parameters ---------- n : int Number of nodes in the graph s : float Mean cluster size v : float Shape parameter. The variance of cluster size distribution is s/v. p_in : float Probabilty of intra cluster connection. p_out : float Probability of inter cluster connection. directed : boolean, optional default=False Whether to create a directed graph or not seed : int Seed value for random number generator Returns ------- G : NetworkX Graph or DiGraph gaussian random partition graph Raises ------ NetworkXError If s is > n If p_in or p_out is not in [0,1] Notes ----- Note the number of partitions is dependent on s,v and n, and that the last partition may be considerably smaller, as it is sized to simply fill out the nodes [1] See Also -------- random_partition_graph Examples -------- >>> G = nx.gaussian_random_partition_graph(100,10,10,.25,.1) >>> len(G) 100 References ---------- .. [1] Ulrik Brandes, Marco Gaertler, Dorothea Wagner, Experiments on Graph Clustering Algorithms, In the proceedings of the 11th Europ. Symp. Algorithms, 2003. ss must be <= nigà?i( RR#tTrueR3Rt normalvariatetfloatR-R( R8tsRR6R7R"RtassignedR5tsize((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRPs;  &  cCs¿|dkrtjdƒ‚n|dkr<tjdƒ‚ntjƒ}xpt|ƒD]b}tjt|||||ƒdƒ}|j|ƒ|j||d|d|||ƒqUW|S(s]Defines a "ring of cliques" graph. A ring of cliques graph is consisting of cliques, connected through single links. Each clique is a complete graph. Parameters ---------- num_cliques : int Number of cliques clique_size : int Size of cliques Returns ------- G : NetworkX Graph ring of cliques graph Raises ------ NetworkXError If the number of cliques is lower than 2 or if the size of cliques is smaller than 2. Examples -------- >>> G = nx.ring_of_cliques(8, 4) See Also -------- connected_caveman_graph Notes ----- The `connected_caveman_graph` graph removes a link from each clique to connect it with the next clique. Instead, the `ring_of_cliques` graph simply adds the link without removing any link from the cliques. is0A ring of cliques must have at least two cliquess(The cliques must have at least two nodesi(RR#R%R R R R R(t num_cliquest clique_sizeRtiR((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyR›s&    cs­|dkr$d}tj|ƒ‚nˆdkrBtjdƒ‚ntjtjtjˆƒg‡fd†t|dƒDƒƒƒ}|jd„tˆ|jƒƒDƒƒ|S(s%Generate a windmill graph. A windmill graph is a graph of `n` cliques each of size `k` that are all joined at one node. It can be thought of as taking a disjoint union of `n` cliques of size `k`, selecting one point from each, and contracting all of the selected points. Alternatively, one could generate `n` cliques of size `k-1` and one node that is connected to all other nodes in the graph. Parameters ---------- n : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph windmill graph with n cliques of size k Raises ------ NetworkXError If the number of cliques is less than two If the size of the cliques are less than two Examples -------- >>> G = nx.windmill_graph(4, 5) Notes ----- The node labeled `0` will be the node connected to all other nodes. Note that windmill graphs are usually denoted `Wd(k,n)`, so the parameters are in the opposite order as the parameters of this method. is/A windmill graph must have at least two cliquess(The cliques must have at least two nodesc3s"|]}tjˆdƒVqdS(iN(Rtcomplete_graph(R!t_(R(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pys ýsicss|]}d|fVqdS(iN((R!RF((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pys ÿs( RR#tdisjoint_union_allR tchainRGR R tnumber_of_nodes(R8RtmsgR((Rss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyRÑs%   &(t__doc__R R1RtnetworkxRtjoint __author__t__all__RRRRtFalseRRRRR(((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/community.pyts(        . + 3y5 J 6