ó žÃÒYc@sèdZddlmZmZmZddlmZmZddlZ ddl m Z m Z m Z mZdddgZe d ƒe d ƒddd „ƒƒZe d ƒe d ƒdd „ƒƒZe d ƒe d ƒdd „ƒƒZdS(sDAlgorithms for finding the lowest common ancestor of trees and DAGs.iÿÿÿÿ(t defaultdicttMappingtSet(tchaintcountN(tarbitrary_elementtnot_implemented_fort UnionFindtgenerate_unique_nodet all_pairs_lowest_common_ancestort%tree_all_pairs_lowest_common_ancestortlowest_common_ancestort undirectedt multigraphccsðt|ƒdkr$tjdƒ‚nd |krBtjdƒ‚n|d k rÿttƒ}t|tt fƒs~t|ƒ}nx~|D]s\}}xB||fD]4}||krždt |ƒ}tj |ƒ‚qžqžW||j |ƒ||j |ƒq…Wn|d kr…xw|j D]i\}}|dkrZ|d k rQd}tj|ƒ‚n|}q|dkrd}tj|ƒ‚qqWn|d kr£tjdƒ‚ntƒ} i} x|D]} | | | | ès s"The node %s is not in the digraph.tkeyc 3s»x´ˆdk rˆn|D]š\‰‰d}d}ddg}ˆˆˆˆg}‡‡‡‡fd†}||ƒ}|||||f}xð|dk r|} ||cd7<||ƒ}|dk r|||||f}|d| dkr|| d|d} } | | f|kr0|| | f} n|| | f} | ry|dkscˆ| |kryˆ| }| }qyq|qqW| rˆdksŸ|ˆkrˆˆf|fVqqWdS(sìIterate through the in-order merge for each pair of interest. We do this to answer the user's query, but it is also used to avoid generating unnecessary tree entries when the user only needs some pairs. ics |\}}|tˆˆƒkr<|tˆˆƒkr<dS|tˆˆƒkrVdS|tˆˆƒkrpdSˆˆˆ|ˆˆˆ|kr˜dSdSdS(s·Returns index of the list containing the next item Next order refers to the merged order. Index can be 0 or 1 (or None if exhausted). iiN(RR(tindicestindex1tindex2(R(teuler_tour_posR.R/(s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/lowest_common_ancestors.pytget_next_in_merged_lists#s iN(R( ttree_lcatdry_runtbest_root_distancetbestR5tancestors_by_indexR9titcurtprevt tree_node1t tree_node2R1(R(R8R t root_distancet super_root(R.R/s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/lowest_common_ancestors.pyt!_compute_dag_lca_from_tree_valuess2%     N(&Rtis_directed_acyclic_graphRRRRRRRRRtcopyRtadd_edgetdfs_treetDiGraphtedgestadd_nodes_fromRtnextt bfs_edgestdepth_first_searchtdfs_preorder_nodesRt from_iterableRRtdagR(RtsortedtgettdictR RtintRtFalse(RR R$R&tsourcesRtsourceRStcountertedgeR)tpairsetR%R#t my_ancestorsRFR:t_tpairtlca((R(R8R RDR3REs/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/lowest_common_ancestors.pyR ¢sn"  ".     "             E    (t__doc__t collectionsRRRt itertoolsRRtnetworkxRtnetworkx.utilsRRRRt__all__RR R R (((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/lowest_common_ancestors.pyt s  "   `  %