
    ިsgz                         d Z ddlmZ ddlZddlmZ ddgZ ed      ej                  d               Z	ej                  d	        Z
y)
z
Dominance algorithms.
    )reduceN)not_implemented_forimmediate_dominatorsdominance_frontiers
undirectedc                   	 || vrt        j                  d      ||i	t        t        j                  | |            }t	        |      D ci c]  \  }}||
 c}}|j                          |j                          	fd}d}|rBd}|D ]8  }t        |	fd| j                  |   D              }|	vs		|   |k7  s2|	|<   d}: |rB	S c c}}w )a1  Returns the immediate dominators of all nodes of a directed graph.

    Parameters
    ----------
    G : a DiGraph or MultiDiGraph
        The graph where dominance is to be computed.

    start : node
        The start node of dominance computation.

    Returns
    -------
    idom : dict keyed by nodes
        A dict containing the immediate dominators of each node reachable from
        `start`.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is undirected.

    NetworkXError
        If `start` is not in `G`.

    Notes
    -----
    Except for `start`, the immediate dominators are the parents of their
    corresponding nodes in the dominator tree.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 5), (3, 4), (4, 5)])
    >>> sorted(nx.immediate_dominators(G, 1).items())
    [(1, 1), (2, 1), (3, 1), (4, 3), (5, 1)]

    References
    ----------
    .. [1] Cooper, Keith D., Harvey, Timothy J. and Kennedy, Ken.
           "A simple, fast dominance algorithm." (2006).
           https://hdl.handle.net/1911/96345
    zstart is not in Gc                     | |k7  r>|    |   k  r|    } |    |   k  r|    |   kD  r|   }|    |   kD  r| |k7  r>| S N )uvdfnidoms     P/var/www/html/venv/lib/python3.12/site-packages/networkx/algorithms/dominance.py	intersectz'immediate_dominators.<locals>.intersectC   sl    1fa&3q6/G a&3q6/a&3q6/G a&3q6/ 1f
     TFc              3   ,   K   | ]  }|v s|  y wr
   r   ).0r   r   s     r   	<genexpr>z'immediate_dominators.<locals>.<genexpr>O   s     )L!t)!)Ls   	)	nxNetworkXErrorlistdfs_postorder_nodes	enumeratepopreverser   pred)
Gstartorderir   r   changednew_idomr   r   s
           @@r   r   r      s    X A~2335>D''512E%e,
-DAq1a4
-C	IIK	MMO G
 	Ai)LQVVAY)LMH}Q8 3"Q		  K+ .s   Cc                 :   t        j                  | |      }|D ci c]  }|t                }}|D ]b  }t        | j                  |         dk\  s| j                  |   D ]2  }||v s|||   k7  s||   j                  |       ||   }|||   k7  r"4 d |S c c}w )a  Returns the dominance frontiers of all nodes of a directed graph.

    Parameters
    ----------
    G : a DiGraph or MultiDiGraph
        The graph where dominance is to be computed.

    start : node
        The start node of dominance computation.

    Returns
    -------
    df : dict keyed by nodes
        A dict containing the dominance frontiers of each node reachable from
        `start` as lists.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is undirected.

    NetworkXError
        If `start` is not in `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 5), (3, 4), (4, 5)])
    >>> sorted((u, sorted(df)) for u, df in nx.dominance_frontiers(G, 1).items())
    [(1, []), (2, [5]), (3, [5]), (4, [5]), (5, [])]

    References
    ----------
    .. [1] Cooper, Keith D., Harvey, Timothy J. and Kennedy, Ken.
           "A simple, fast dominance algorithm." (2006).
           https://hdl.handle.net/1911/96345
       )r   r   setlenr   add)r   r   r   r   dfr   s         r   r   r   W   s    L ""1e,D 	!q!SU(	!B	! $qvvay>QVVAY $9tAw,1		! G tAw,$$ I 
"s   B)__doc__	functoolsr   networkxr   networkx.utilsr   __all___dispatchabler   r   r   r   r   <module>r0      sd      .!#8
9 \"E  #EP / /r   