ó žÃÒYc@ s½dZddlmZddlmZddlZddddd d d d gZddd „Z dd„Z ddd„Z ddd„Z ddd„Z ddd„Zd„Zd„ZdS(s7Functions for finding and evaluating cuts in a graph. iÿÿÿÿ(tdivision(tchainNtboundary_expansiont conductancetcut_sizetedge_expansiontmixing_expansiontnode_expansiontnormalized_cut_sizetvolumec C sntj|||d|ddƒ}|jƒrZt|tj|||d|ddƒƒ}ntd„|DƒƒS(s©Returns the size of the cut between two sets of nodes. A *cut* is a partition of the nodes of a graph into two sets. The *cut size* is the sum of the weights of the edges "between" the two sets of nodes. Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. T : sequence A sequence of nodes in `G`. If not specified, this is taken to be the set complement of `S`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number Total weight of all edges from nodes in set `S` to nodes in set `T` (and, in the case of directed graphs, all edges from nodes in `T` to nodes in `S`). Examples -------- In the graph with two cliques joined by a single edges, the natural bipartition of the graph into two blocks, one for each clique, yields a cut of weight one:: >>> G = nx.barbell_graph(3, 0) >>> S = {0, 1, 2} >>> T = {3, 4, 5} >>> nx.cut_size(G, S, T) 1 Each parallel edge in a multigraph is counted when determining the cut size:: >>> G = nx.MultiGraph(['ab', 'ab']) >>> S = {'a'} >>> T = {'b'} >>> nx.cut_size(G, S, T) 2 Notes ----- In a multigraph, the cut size is the total weight of edges including multiplicity. tdatatdefaultics s|]\}}}|VqdS(N((t.0tutvtweight((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pys Ws(tnxt edge_boundaryt is_directedRtsum(tGtStTRtedges((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyRs8! -cC s>|jƒr|jn|j}td„||d|ƒDƒƒS(sxReturns the volume of a set of nodes. The *volume* of a set *S* is the sum of the (out-)degrees of nodes in *S* (taking into account parallel edges in multigraphs). [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number The volume of the set of nodes represented by `S` in the graph `G`. See also -------- conductance cut_size edge_expansion edge_boundary normalized_cut_size References ---------- .. [1] David Gleich. *Hierarchical Directed Spectral Graph Partitioning*. cs s|]\}}|VqdS(N((R Rtd((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pys sR(Rt out_degreetdegreeR(RRRR((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyR Zs&cC s~|dkr%t|ƒt|ƒ}nt||d|d|ƒ}t||d|ƒ}t||d|ƒ}|d|d|S(súReturns the normalized size of the cut between two sets of nodes. The *normalized cut size* is the cut size times the sum of the reciprocal sizes of the volumes of the two sets. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. T : sequence A sequence of nodes in `G`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number The normalized cut size between the two sets `S` and `T`. Notes ----- In a multigraph, the cut size is the total weight of edges including multiplicity. See also -------- conductance cut_size edge_expansion volume References ---------- .. [1] David Gleich. *Hierarchical Directed Spectral Graph Partitioning*. RRiN(tNonetsetRR (RRRRt num_cut_edgestvolume_Stvolume_T((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyR„s , cC sx|dkr%t|ƒt|ƒ}nt|||d|ƒ}t||d|ƒ}t||d|ƒ}|t||ƒS(shReturns the conductance of two sets of nodes. The *conductance* is the quotient of the cut size and the smaller of the volumes of the two sets. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. T : sequence A sequence of nodes in `G`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number The conductance between the two sets `S` and `T`. See also -------- cut_size edge_expansion normalized_cut_size volume References ---------- .. [1] David Gleich. *Hierarchical Directed Spectral Graph Partitioning*. RN(RRRR tmin(RRRRRRR((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyR¸s ' cC s]|dkr%t|ƒt|ƒ}nt||d|d|ƒ}|tt|ƒt|ƒƒS(s¡Returns the edge expansion between two node sets. The *edge expansion* is the quotient of the cut size and the smaller of the cardinalities of the two sets. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. T : sequence A sequence of nodes in `G`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number The edge expansion between the two sets `S` and `T`. See also -------- boundary_expansion mixing_expansion node_expansion References ---------- .. [1] Fan Chung. *Spectral Graph Theory*. (CBMS Regional Conference Series in Mathematics, No. 92), American Mathematical Society, 1997, ISBN 0-8218-0315-8 RRN(RRRR tlen(RRRRR((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyRçs( cC s3t||d|d|ƒ}|jƒ}|d|S(smReturns the mixing expansion between two node sets. The *mixing expansion* is the quotient of the cut size and twice the number of edges in the graph. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. T : sequence A sequence of nodes in `G`. weight : object Edge attribute key to use as weight. If not specified, edges have weight one. Returns ------- number The mixing expansion between the two sets `S` and `T`. See also -------- boundary_expansion edge_expansion node_expansion References ---------- .. [1] Vadhan, Salil P. "Pseudorandomness." *Foundations and Trends in Theoretical Computer Science* 7.1–3 (2011): 1–336. RRi(Rtnumber_of_edges(RRRRRtnum_total_edges((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyRs( c s9ttj‡fd†|Dƒƒƒ}t|ƒt|ƒS(s¯Returns the node expansion of the set `S`. The *node expansion* is the quotient of the size of the node boundary of *S* and the cardinality of *S*. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. Returns ------- number The node expansion of the set `S`. See also -------- boundary_expansion edge_expansion mixing_expansion References ---------- .. [1] Vadhan, Salil P. "Pseudorandomness." *Foundations and Trends in Theoretical Computer Science* 7.1–3 (2011): 1–336. c3 s|]}ˆj|ƒVqdS(N(t neighbors(R R(R(sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pys es(RRt from_iterableR!(RRt neighborhood((Rsn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyRDs!%cC s ttj||ƒƒt|ƒS(s°Returns the boundary expansion of the set `S`. The *boundary expansion* is the quotient of the size of the edge boundary and the cardinality of *S*. [1] Parameters ---------- G : NetworkX graph S : sequence A sequence of nodes in `G`. Returns ------- number The boundary expansion of the set `S`. See also -------- edge_expansion mixing_expansion node_expansion References ---------- .. [1] Vadhan, Salil P. "Pseudorandomness." *Foundations and Trends in Theoretical Computer Science* 7.1–3 (2011): 1–336. (R!Rt node_boundary(RR((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyRks!(t__doc__t __future__Rt itertoolsRtnetworkxRt__all__RRR RRRRRR(((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cuts.pyts    > *4/./ '