ó žÃÒYc@sdZddlZdjddgƒZdddd d d gZd „Zd „Zd„Zdd„Z d„Z dd„Z dS(sR ========================== Bipartite Graph Algorithms ========================== iÿÿÿÿNs s%Jordi Torrents s%Aric Hagberg t is_bipartitetis_bipartite_node_settcolortsetstdensitytdegreescs8ˆjƒr-ddl‰‡‡fd†}n ˆj}i}xÓˆD]Ë}||ksCtˆ|ƒdkrqqCn|g}d||>> from networkx.algorithms import bipartite >>> G = nx.path_graph(4) >>> c = bipartite.color(G) >>> print(c) {0: 1, 1: 0, 2: 1, 3: 0} You can use this to set a node attribute indicating the biparite set: >>> nx.set_node_attributes(G, c, 'bipartite') >>> print(G.nodes[0]['bipartite']) 1 >>> print(G.nodes[1]['bipartite']) 0 iÿÿÿÿNcs(ˆjjˆj|ƒˆj|ƒgƒS(N(tchaint from_iterablet predecessorst successors(tv(tGt itertools(sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyt neighbors<siisGraph is not bipartite.( t is_directedR R tlentpoptnxt NetworkXErrortappendtupdatetdicttfromkeystisolates(R R RtntqueueR tctw((R R sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRs*"    "      "cCs.yt|ƒtSWntjk r)tSXdS(sG Returns True if graph G is bipartite, False if not. Parameters ---------- G : NetworkX graph Examples -------- >>> from networkx.algorithms import bipartite >>> G = nx.path_graph(4) >>> print(bipartite.is_bipartite(G)) True See Also -------- color, is_bipartite_node_set N(RtTrueRRtFalse(R ((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRVs  cCs|t|ƒ}xitj|ƒD]X}t|ƒ\}}|j|ƒrR|j|ƒpm|j|ƒom|j|ƒstSqWtS(sùReturns True if nodes and G/nodes are a bipartition of G. Parameters ---------- G : NetworkX graph nodes: list or container Check if nodes are a one of a bipartite set. Examples -------- >>> from networkx.algorithms import bipartite >>> G = nx.path_graph(4) >>> X = set([1,3]) >>> bipartite.is_bipartite_node_set(G,X) True Notes ----- For connected graphs the bipartite sets are unique. This function handles disconnected graphs. (tsetRtconnected_component_subgraphsRtissubsett isdisjointRR(R tnodestStCCtXtY((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRns cCs²|jƒrtj}n tj}|dk rLt|ƒ}t|ƒ|}n\||ƒspd}tj|ƒ‚nt|ƒ}d„|jƒDƒ}d„|jƒDƒ}||fS(s=Returns bipartite node sets of graph G. Raises an exception if the graph is not bipartite or if the input graph is disconnected and thus more than one valid solution exists. See :mod:`bipartite documentation ` for further details on how bipartite graphs are handled in NetworkX. Parameters ---------- G : NetworkX graph top_nodes : container Container with all nodes in one bipartite node set. If not supplied it will be computed. But if more than one solution exists an exception will be raised. Returns ------- (X,Y) : two-tuple of sets One set of nodes for each part of the bipartite graph. Raises ------ AmbiguousSolution : Exception Raised if the input bipartite graph is disconnected and no container with all nodes in one bipartite set is provided. When determining the nodes in each bipartite set more than one valid solution is possible if the input graph is disconnected. NetworkXError: Exception Raised if the input graph is not bipartite. Examples -------- >>> from networkx.algorithms import bipartite >>> G = nx.path_graph(4) >>> X, Y = bipartite.sets(G) >>> list(X) [0, 2] >>> list(Y) [1, 3] See Also -------- color s:Disconnected graph: Ambiguous solution for bipartite sets.cSs"h|]\}}|r|’qS(((t.0Rtis_top((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pys Ís cSs"h|]\}}|s|’qS(((R'RR(((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pys Îs N( RRtis_weakly_connectedt is_connectedtNoneRtAmbiguousSolutionRtitems(R t top_nodesR*R%R&tmsgR((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRŽs3       cCs…t|ƒ}tj|ƒ}t|ƒ}||}|dkrFd}n;|jƒrm|dt||ƒ}n|t||ƒ}|S(s]Return density of bipartite graph B. Parameters ---------- G : NetworkX graph nodes: list or container Nodes in one node set of the bipartite graph. Returns ------- d : float The bipartite density Examples -------- >>> from networkx.algorithms import bipartite >>> G = nx.complete_bipartite_graph(3,2) >>> X=set([0,1,2]) >>> bipartite.density(G,X) 1.0 >>> Y=set([3,4]) >>> bipartite.density(G,Y) 1.0 Notes ----- The container of nodes passed as argument must contain all nodes in one of the two bipartite node sets to avoid ambiguity in the case of disconnected graphs. See :mod:`bipartite documentation ` for further details on how bipartite graphs are handled in NetworkX. See Also -------- color igg@(RRtnumber_of_edgesRtfloat(tBR"Rtmtnbtnttd((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRÑs&      cCs>t|ƒ}t|ƒ|}|j||ƒ|j||ƒfS(s¯Return the degrees of the two node sets in the bipartite graph B. Parameters ---------- G : NetworkX graph nodes: list or container Nodes in one node set of the bipartite graph. weight : string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. Returns ------- (degX,degY) : tuple of dictionaries The degrees of the two bipartite sets as dictionaries keyed by node. Examples -------- >>> from networkx.algorithms import bipartite >>> G = nx.complete_bipartite_graph(3,2) >>> Y=set([3,4]) >>> degX,degY=bipartite.degrees(G,Y) >>> dict(degX) {0: 2, 1: 2, 2: 2} Notes ----- The container of nodes passed as argument must contain all nodes in one of the two bipartite node sets to avoid ambiguity in the case of disconnected graphs. See :mod:`bipartite documentation ` for further details on how bipartite graphs are handled in NetworkX. See Also -------- color, density (Rtdegree(R2R"tweighttbottomttop((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyRs) ( t__doc__tnetworkxRtjoint __author__t__all__RRRR+RRR(((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/basic.pyts     >  C 3