ó žÃÒYc@südZddlZddlmZddlmZddlmZddlm Z ddl Z ddd d gZ ed ƒd „ƒZ ed ƒd „ƒZd„Zedƒed ƒd„ƒƒZd efd„ƒYZdd„Zd„Zd„ZdS(sw Algorithms for finding k-edge-connected components and subgraphs. A k-edge-connected component (k-edge-cc) is a maximal set of nodes in G, such that all pairs of node have an edge-connectivity of at least k. A k-edge-connected subgraph (k-edge-subgraph) is a maximal set of nodes in G, such that the subgraph of G defined by the nodes has an edge-connectivity at least k. iÿÿÿÿN(tarbitrary_element(tnot_implemented_for(tbridges(tpartialtk_edge_componentstk_edge_subgraphstbridge_componentstEdgeComponentAuxGrapht multigraphcCs®|dkrtdƒ‚n|jƒr_|dkr@tj|ƒStj|ƒ}|j|ƒSnK|dkrxtj|ƒS|dkrŽt|ƒStj|ƒ}|j|ƒSdS(s¨Generates nodes in each maximal k-edge-connected component in G. Parameters ---------- G : NetworkX graph k : Integer Desired edge connectivity Returns ------- k_edge_components : a generator of k-edge-ccs. Each set of returned nodes will have k-edge-connectivity in the graph G. See Also ------- :func:`local_edge_connectivity` :func:`k_edge_subgraphs` : similar to this function, but the subgraph defined by the nodes must also have k-edge-connectivity. :func:`k_components` : similar to this function, but uses node-connectivity instead of edge-connectivity Raises ------ NetworkXNotImplemented: If the input graph is a multigraph. ValueError: If k is less than 1 Notes ----- Attempts to use the most efficient implementation available based on k. If k=1, this is simply simply connected components for directed graphs and connected components for undirected graphs. If k=2 on an efficient bridge connected component algorithm from _[1] is run based on the chain decomposition. Otherwise, the algorithm from _[2] is used. Example ------- >>> import itertools as it >>> from networkx.utils import pairwise >>> paths = [ ... (1, 2, 4, 3, 1, 4), ... (5, 6, 7, 8, 5, 7, 8, 6), ... ] >>> G = nx.Graph() >>> G.add_nodes_from(it.chain(*paths)) >>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths])) >>> # note this returns {1, 4} unlike k_edge_subgraphs >>> sorted(map(sorted, nx.k_edge_components(G, k=3))) [[1, 4], [2], [3], [5, 6, 7, 8]] References ---------- .. [1] https://en.wikipedia.org/wiki/Bridge_%28graph_theory%29 .. [2] Wang, Tianhao, et al. (2015) A simple algorithm for finding all k-edge-connected components. http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0136264 isk cannot be less than 1iN( t ValueErrort is_directedtnxtstrongly_connected_componentsRt constructRtconnected_componentsR(tGtkt aux_graph((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyR#s@        cCsz|dkrtdƒ‚n|jƒrP|dkr@t||ƒSt||ƒSn&|dkrit||ƒSt||ƒSdS(súGenerates nodes in each maximal k-edge-connected subgraph in G. Parameters ---------- G : NetworkX graph k : Integer Desired edge connectivity Returns ------- k_edge_subgraphs : a generator of k-edge-subgraphs Each k-edge-subgraph is a maximal set of nodes that defines a subgraph of G that is k-edge-connected. See Also ------- :func:`edge_connectivity` :func:`k_edge_components` : similar to this function, but nodes only need to have k-edge-connctivity within the graph G and the subgraphs might not be k-edge-connected. Raises ------ NetworkXNotImplemented: If the input graph is a multigraph. ValueError: If k is less than 1 Notes ----- Attempts to use the most efficient implementation available based on k. If k=1, or k=2 and the graph is undirected, then this simply calls `k_edge_components`. Otherwise the algorithm from _[1] is used. Example ------- >>> import itertools as it >>> from networkx.utils import pairwise >>> paths = [ ... (1, 2, 4, 3, 1, 4), ... (5, 6, 7, 8, 5, 7, 8, 6), ... ] >>> G = nx.Graph() >>> G.add_nodes_from(it.chain(*paths)) >>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths])) >>> # note this does not return {1, 4} unlike k_edge_components >>> sorted(map(sorted, nx.k_edge_subgraphs(G, k=3))) [[1], [2], [3], [4], [5, 6, 7, 8]] References ---------- .. [1] Zhou, Liu, et al. (2012) Finding maximal k-edge-connected subgraphs from a large graph. ACM International Conference on Extending Database Technology 2012 480-–491. https://openproceedings.org/2012/conf/edbt/ZhouLYLCL12.pdf isk cannot be less than 1iN(R R Rt_k_edge_subgraphs_nodes(RR((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyRvs<      ccs/x(t||ƒD]}t|jƒƒVqWdS(siHelper to get the nodes from the subgraphs. This allows k_edge_subgraphs to return a generator. N(tgeneral_k_edge_subgraphstsettnodes(RRtC((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyRÄstdirectedccsB|jƒ}|jt|ƒƒxtj|ƒD] }|Vq/WdS(sàFinds all bridge-connected components G. Parameters ---------- G : NetworkX undirected graph Returns ------- bridge_components : a generator of 2-edge-connected components See Also -------- :func:`k_edge_subgraphs` : this function is a special case for an undirected graph where k=2. :func:`biconnected_components` : similar to this function, but is defined using 2-node-connectivity instead of 2-edge-connectivity. Raises ------ NetworkXNotImplemented: If the input graph is directed or a multigraph. Notes ----- Bridge-connected components are also known as 2-edge-connected components. Example ------- >>> # The barbell graph with parameter zero has a single bridge >>> G = nx.barbell_graph(5, 0) >>> from networkx.algorithms.connectivity.edge_kcomponents import bridge_components >>> sorted(map(sorted, bridge_components(G))) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]] N(tcopytremove_edges_fromRR R(RtHtcc((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyRÍs& cBs/eZdZed„ƒZd„Zd„ZRS(s- A simple algorithm to find all k-edge-connected components in a graph. Constructing the AuxillaryGraph (which may take some time) allows for the k-edge-ccs to be found in linear time for arbitrary k. Notes ----- This implementation is based on [1]_. The idea is to construct an auxillary graph from which the k-edge-ccs can be extracted in linear time. The auxillary graph is constructed in $O(|V|\cdot F)$ operations, where F is the complexity of max flow. Querying the components takes an additional $O(|V|)$ operations. This algorithm can be slow for large graphs, but it handles an arbitrary k and works for both directed and undirected inputs. The undirected case for k=1 is exactly connected components. The undirected case for k=2 is exactly bridge connected components. The directed case for k=1 is exactly strongly connected components. References ---------- .. [1] Wang, Tianhao, et al. (2015) A simple algorithm for finding all k-edge-connected components. http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0136264 Example ------- >>> import itertools as it >>> from networkx.utils import pairwise >>> from networkx.algorithms.connectivity import EdgeComponentAuxGraph >>> # Build an interesting graph with multiple levels of k-edge-ccs >>> paths = [ ... (1, 2, 3, 4, 1, 3, 4, 2), # a 3-edge-cc (a 4 clique) ... (5, 6, 7, 5), # a 2-edge-cc (a 3 clique) ... (1, 5), # combine first two ccs into a 1-edge-cc ... (0,), # add an additional disconnected 1-edge-cc ... ] >>> G = nx.Graph() >>> G.add_nodes_from(it.chain(*paths)) >>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths])) >>> # Constructing the AuxGraph takes about O(n ** 4) >>> aux_graph = EdgeComponentAuxGraph.construct(G) >>> # Once constructed, querying takes O(n) >>> sorted(map(sorted, aux_graph.k_edge_components(k=1))) [[0], [1, 2, 3, 4, 5, 6, 7]] >>> sorted(map(sorted, aux_graph.k_edge_components(k=2))) [[0], [1, 2, 3, 4], [5, 6, 7]] >>> sorted(map(sorted, aux_graph.k_edge_components(k=3))) [[0], [1, 2, 3, 4], [5], [6], [7]] >>> sorted(map(sorted, aux_graph.k_edge_components(k=4))) [[0], [1], [2], [3], [4], [5], [6], [7]] Example ------- >>> # The auxillary graph is primarilly used for k-edge-ccs but it >>> # can also speed up the queries of k-edge-subgraphs by refining the >>> # search space. >>> import itertools as it >>> from networkx.utils import pairwise >>> from networkx.algorithms.connectivity import EdgeComponentAuxGraph >>> paths = [ ... (1, 2, 4, 3, 1, 4), ... ] >>> G = nx.Graph() >>> G.add_nodes_from(it.chain(*paths)) >>> G.add_edges_from(it.chain(*[pairwise(path) for path in paths])) >>> aux_graph = EdgeComponentAuxGraph.construct(G) >>> sorted(map(sorted, aux_graph.k_edge_subgraphs(k=3))) [[1], [2], [3], [4]] >>> sorted(map(sorted, aux_graph.k_edge_components(k=3))) [[1, 4], [2], [3]] cs×tdƒd„ƒ|ƒ‡fd†‰|jƒ}|j|jƒƒ|j|jƒddƒtjƒ}|jƒdkr¸t |jƒƒ}t |jƒƒ}ˆ||||ƒn|ƒ}||_ ||_ |S(sPBuilds an auxillary graph encoding edge-connectivity between nodes. Notes ----- Given G=(V, E), initialize an empty auxillary graph A. Choose an arbitrary source node s. Initialize a set N of available nodes (that can be used as the sink). The algorithm picks an arbitrary node t from N - {s}, and then computes the minimum st-cut (S, T) with value w. If G is directed the the minimum of the st-cut or the ts-cut is used instead. Then, the edge (s, t) is added to the auxillary graph with weight w. The algorithm is called recursively first using S as the available nodes and s as the source, and then using T and t. Recusion stops when the source is the only available node. Parameters ---------- G : NetworkX graph RcSs|S(N((R((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pytYsc sì|h|krdSt||hƒ}tj|||ƒ\}\}}|jƒrštj|||ƒ\}\} } ||krš|| | }}}qšn|j||d|ƒˆ||||j|ƒƒˆ||||j|ƒƒdS(Ntweight(RR t minimum_cutR tadd_edget intersection( RtAtsourcetavailtsinktvaluetStTtvalue_tT_tS_(t_recursive_build(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyR+[s! ! tcapacityii( Rt fresh_copytadd_nodes_fromRtadd_edges_fromtedgesR tGraphtnumber_of_nodesRRR!R(RRRR!R"R#tself((R+s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyR Cs     c#s›ˆdkrtdƒ‚n|j}tj|dƒ}tjƒ}|j|jƒƒ|j‡fd†|jƒDƒƒxtj |ƒD] }|VqˆWdS(s-Queries the auxillary graph for k-edge-connected components. Parameters ---------- k : Integer Desired edge connectivity Returns ------- k_edge_components : a generator of k-edge-ccs Notes ----- Given the auxillary graph, the k-edge-connected components can be determined in linear time by removing all edges with weights less than k from the auxillary graph. The resulting connected components are the k-edge-ccs in the original graph. isk cannot be less than 1Rc3s'|]\}}|ˆkr|VqdS(N((t.0tetw(R(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pys £sN( R R!R tget_edge_attributesR1R.RR/titemsR(R3RR!t aux_weightstRR((Rs‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyR‡s   #c #sûˆdkrtdƒ‚n|j}|j}tj|dƒ}tjƒ}|j|jƒƒ|j‡fd†|j ƒDƒƒxstj |ƒD]b}t |ƒˆkrÅxG|D]}|hVq°Wq‘|j |ƒ}xt |ˆƒD] } | VqäWq‘WdS(s:Queries the auxillary graph for k-edge-connected subgraphs. Parameters ---------- k : Integer Desired edge connectivity Returns ------- k_edge_subgraphs : a generator of k-edge-subgraphs Notes ----- Refines the k-edge-ccs into k-edge-subgraphs. The running time is more than $O(|V|)$. For single values of k it is faster to use `nx.k_edge_subgraphs`. But for multiple values of k, it can be faster to build AuxGraph and then use this method. isk cannot be less than 1Rc3s'|]\}}|ˆkr|VqdS(N((R4R5R6(R(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pys ÈsN(R RR!R R7R1R.RR/R8RtlentsubgraphR( R3RRR!R9R:RtnodeRtsub_cc((Rs‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyR©s    # (t__name__t __module__t__doc__t classmethodR RR(((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyRùsGD "ccsÞ|jƒr¦tƒ}x>|j|ƒD]-\}}||kr%|j|ƒ|Vq%q%Wx|j|ƒD]9\}}||krf||krf|j|ƒ|VqfqfWn4x1|j|ƒD] \}}||kr¶|Vq¶q¶WdS(s1Helper for finding nodes with degree less than k.N(R Rt out_degreetaddt in_degreetdegree(RRtnbunchtseenR=RF((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyt_low_degree_nodes×s       ccsî|jƒ}tt||ƒƒ}xy|rœttjjt|j|ƒƒƒ}|j|ƒ|j |ƒx|D]}|hVqoWtt|||ƒƒ}q$W|j ƒrËx>t j |ƒD] }|Vq¹Wnxt j |ƒD] }|VqÛWdS(sÒHelper for filtering components that cant be k-edge-connected. Removes and generates each node with degree less than k. Then generates remaining components where all nodes have degree at least k. N(RRRItittchaint from_iterabletmapt neighborstdifference_updatetremove_nodes_fromR R R R(RRRt singletonsRGR=R((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyt_high_degree_componentsìs  $      c #s;|dkrtdƒ‚nttd|ƒ}ˆjƒ|kryx+ˆjƒD]}ˆj|gƒjƒVqLWtƒ‚n‡fd†|ˆƒDƒ}xŸ|r6|jƒ}|jƒdkrÄ|Vq˜t j |ƒ}t |ƒ}||kr.|j |ƒx8||ƒD]"}|j |j|ƒjƒƒqWq˜|Vq˜WdS(s2General algorithm to find all maximal k-edge-connected subgraphs in G. Returns ------- k_edge_subgraphs : a generator of nx.Graphs that are k-edge-subgraphs Each k-edge-subgraph is a maximal set of nodes that defines a subgraph of G that is k-edge-connected. Notes ----- Implementation of the basic algorithm from _[1]. The basic idea is to find a global minimum cut of the graph. If the cut value is at least k, then the graph is a k-edge-connected subgraph and can be added to the results. Otherwise, the cut is used to split the graph in two and the procedure is applied recursively. If the graph is just a single node, then it is also added to the results. At the end, each result is either gaurenteed to be a single node or a subgraph of G that is k-edge-connected. This implementation contains optimizations for reducing the number of calls to max-flow, but there are other optimizations in _[1] that could be implemented. References ---------- .. [1] Zhou, Liu, et al. (2012) Finding maximal k-edge-connected subgraphs from a large graph. ACM International Conference on Extending Database Technology 2012 480-–491. https://openproceedings.org/2012/conf/edbt/ZhouLYLCL12.pdf Example ------- >>> from networkx.utils import pairwise >>> paths = [ ... (11, 12, 13, 14, 11, 13, 14, 12), # a 4-clique ... (21, 22, 23, 24, 21, 23, 24, 22), # another 4-clique ... # connect the cliques with high degree but low connectivity ... (50, 13), ... (12, 50, 22), ... (13, 102, 23), ... (14, 101, 24), ... ] >>> G = nx.Graph(it.chain(*[pairwise(path) for path in paths])) >>> sorted(map(len, k_edge_subgraphs(G, k=3))) [1, 1, 1, 4, 4] isk cannot be less than 1Rcs%h|]}ˆj|ƒjƒ’qS((R<R(R4R(R(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pys Cs N(R RRRR2RR<Rt StopIterationtpopR tminimum_edge_cutR;RRD( RRtfind_ccsR=tR0tG1t cut_edgest cut_valueR((Rs‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyRs&.       #(RAtnetworkxR tnetworkx.utilsRRtnetworkx.algorithmsRt functoolsRt itertoolsRJt__all__RRRRtobjectRtNoneRIRRR(((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/connectivity/edge_kcomponents.pyts&   SN +Þ