U
    9vf                     @   s   d dl mZ d dlZdddgZdd ZdddZdd	d
Zdd Zdd Z	G dd dZ
G dd dZdd ZG dd dZG dd dejZdS )    )defaultdictNcheck_planarity	is_planarPlanarEmbeddingc                 C   s   t | ddd S )a$  Returns True if and only if `G` is planar.

    A graph is *planar* iff it can be drawn in a plane without
    any edge intersections.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    bool
       Whether the graph is planar.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2)])
    >>> nx.is_planar(G)
    True
    >>> nx.is_planar(nx.complete_graph(5))
    False

    See Also
    --------
    check_planarity :
        Check if graph is planar *and* return a `PlanarEmbedding` instance if True.
    F)counterexampler   )r   )G r   A/tmp/pip-unpacked-wheel-_lngutwb/networkx/algorithms/planarity.pyr      s    Fc                 C   s:   t | }| }|dkr.|r(dt| fS dS nd|fS dS )ad  Check if a graph is planar and return a counterexample or an embedding.

    A graph is planar iff it can be drawn in a plane without
    any edge intersections.

    Parameters
    ----------
    G : NetworkX graph
    counterexample : bool
        A Kuratowski subgraph (to proof non planarity) is only returned if set
        to true.

    Returns
    -------
    (is_planar, certificate) : (bool, NetworkX graph) tuple
        is_planar is true if the graph is planar.
        If the graph is planar `certificate` is a PlanarEmbedding
        otherwise it is a Kuratowski subgraph.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2)])
    >>> is_planar, P = nx.check_planarity(G)
    >>> print(is_planar)
    True

    When `G` is planar, a `PlanarEmbedding` instance is returned:

    >>> P.get_data()
    {0: [1, 2], 1: [0], 2: [0]}

    Notes
    -----
    A (combinatorial) embedding consists of cyclic orderings of the incident
    edges at each vertex. Given such an embedding there are multiple approaches
    discussed in literature to drawing the graph (subject to various
    constraints, e.g. integer coordinates), see e.g. [2].

    The planarity check algorithm and extraction of the combinatorial embedding
    is based on the Left-Right Planarity Test [1].

    A counterexample is only generated if the corresponding parameter is set,
    because the complexity of the counterexample generation is higher.

    See also
    --------
    is_planar :
        Check for planarity without creating a `PlanarEmbedding` or counterexample.

    References
    ----------
    .. [1] Ulrik Brandes:
        The Left-Right Planarity Test
        2009
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.217.9208
    .. [2] Takao Nishizeki, Md Saidur Rahman:
        Planar graph drawing
        Lecture Notes Series on Computing: Volume 12
        2004
    NFFNT)LRPlanaritylr_planarityget_counterexampler   r   planarity_state	embeddingr   r   r	   r   (   s    >c                 C   s:   t | }| }|dkr.|r(dt| fS dS nd|fS dS )z-Recursive version of :meth:`check_planarity`.NFr
   T)r   lr_planarity_recursiveget_counterexample_recursiver   r   r   r	   check_planarity_recursives   s    r   c                 C   s|   t | } t| d r t dt  }| D ]J}t| | }|D ]4}| || t| d r@| || ||| q@q,|S )a  Obtains a Kuratowski subgraph.

    Raises nx.NetworkXException if G is planar.

    The function removes edges such that the graph is still not planar.
    At some point the removal of any edge would make the graph planar.
    This subgraph must be a Kuratowski subgraph.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    subgraph : NetworkX graph
        A Kuratowski subgraph that proves that G is not planar.

    r   !G is planar - no counter example.)nxGraphr   NetworkXExceptionlistremove_edgeadd_edger   ZsubgraphuZnbrsvr   r   r	   r      s    

r   c                 C   s|   t | } t| d r t dt  }| D ]J}t| | }|D ]4}| || t| d r@| || ||| q@q,|S )z0Recursive version of :meth:`get_counterexample`.r   r   )r   r   r   r   r   r   r   r   r   r   r	   r      s    

r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )IntervalzRepresents a set of return edges.

    All return edges in an interval induce a same constraint on the contained
    edges, which means that all edges must either have a left orientation or
    all edges must have a right orientation.
    Nc                 C   s   || _ || _d S Nlowhigh)selfr!   r"   r   r   r	   __init__   s    zInterval.__init__c                 C   s   | j dko| jdkS )zCheck if the interval is emptyNr    r#   r   r   r	   empty   s    zInterval.emptyc                 C   s   t | j| jS )zReturns a copy of this interval)r   r!   r"   r%   r   r   r	   copy   s    zInterval.copyc                 C   s    |    o|j| j |j| kS )z0Returns True if interval I conflicts with edge b)r&   lowptr"   )r#   br   r   r   r	   conflicting   s    
zInterval.conflicting)NN)__name__
__module____qualname____doc__r$   r&   r'   r*   r   r   r   r	   r      s
   
r   c                   @   s2   e Zd ZdZe e fddZdd Zdd ZdS )	ConflictPairzRepresents a different constraint between two intervals.

    The edges in the left interval must have a different orientation than
    the one in the right interval.
    c                 C   s   || _ || _d S r   leftright)r#   r1   r2   r   r   r	   r$      s    zConflictPair.__init__c                 C   s   | j }| j| _ || _dS )zSwap left and right intervalsNr0   )r#   tempr   r   r	   swap   s    zConflictPair.swapc                 C   sN   | j  r|j| jj S | j r0|j| j j S t|j| j j |j| jj S )z.Returns the lowest lowpoint of a conflict pair)r1   r&   r(   r2   r!   min)r#   r   r   r   r	   lowest   s    

 zConflictPair.lowestN)r+   r,   r-   r.   r   r$   r4   r6   r   r   r   r	   r/      s   r/   c                 C   s   | sdS | d S )z(Returns the element on top of the stack.Nr   )lr   r   r	   top_of_stack   s    r9   c                   @   s   e Zd ZdZdddddddd	d
dddddddddgZdd Zdd Zdd Zdd Zdd Z	dd Z
d d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.S )/r   z5A class to maintain the state during planarity check.r   rootsheightr(   lowpt2nesting_depthparent_edgeDGadjsordered_adjsrefsideSstack_bottom
lowpt_edgeleft_ref	right_refr   c                 C   s   t  | _| j|j |jD ]*}|d |d kr| j|d |d  qg | _tdd | _	i | _
i | _i | _tdd | _t  | _| j|j i | _i | _tdd | _tdd | _g | _i | _i | _i | _i | _t | _d S )Nr      c                   S   s   d S r   r   r   r   r   r	   <lambda>       z&LRPlanarity.__init__.<locals>.<lambda>c                   S   s   d S r   r   r   r   r   r	   rJ   '  rK   c                   S   s   d S r   r   r   r   r   r	   rJ   0  rK   c                   S   s   dS )NrI   r   r   r   r   r	   rJ   1  rK   )r   r   r   add_nodes_fromnodesedgesr   r:   r   r;   r(   r<   r=   r>   DiGraphr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r   r   )r#   r   er   r   r	   r$     s.    


zLRPlanarity.__init__c                    s   j  dkr. j  d j   d kr.dS  j D ]t j   j< q4 j D ]2 j dkrTd j<  j   qTd _ d _	d _ j
D ]&t j
  fddd j< q jD ] s dS qd _d _d _d _d _ j
jD ] } | j|   j|< q
 j j
j  j
D ]Rt j
  fd	dd j< d} j D ]} j|| |}qvqBd _
d _d _ jD ]  qd _d _d _d _d _d _ jS )
zExecute the LR planarity test.

        Returns
        -------
        embedding : dict
            If the graph is planar an embedding is returned. Otherwise None.
                 Nr   c                    s    j | f S r   r=   xr#   r   r   r	   rJ   ]  rK   z*LRPlanarity.lr_planarity.<locals>.<lambda>keyc                    s    j | f S r   rT   rU   rW   r   r	   rJ   q  rK   )r   ordersizer   r@   r;   r:   appenddfs_orientationr<   r?   sortedrA   dfs_testingr(   rD   rE   rF   rN   signr=   r   rL   rM   add_half_edge_cwrB   dfs_embeddingr>   rG   rH   rC   r#   rP   Zprevious_nodewr   rW   r	   r   =  sb    *



 


 
zLRPlanarity.lr_planarityc                    sf   j  dkr. j  d j   d kr.dS  j D ]2 j dkr4d j<  j   q4d _  jD ]&t j  fddd j	< qt jD ] 
s dS q jjD ]} | j|   j|< q j jj  jD ]Pt j  fd	dd j	< d} j	 D ]} j|| |}q*q jD ]  qN jS )
z*Recursive version of :meth:`lr_planarity`.rQ   rR   rS   Nr   c                    s    j | f S r   rT   rU   rW   r   r	   rJ     rK   z4LRPlanarity.lr_planarity_recursive.<locals>.<lambda>rX   c                    s    j | f S r   rT   rU   rW   r   r	   rJ     rK   )r   rZ   r[   r;   r:   r\   dfs_orientation_recursiver?   r^   rA   dfs_testing_recursiverN   sign_recursiver=   r   rL   rM   ra   dfs_embedding_recursiverc   r   rW   r	   r     s>    *


 


 

z"LRPlanarity.lr_planarity_recursivec                 C   s  |g}t dd }t dd }|r| }| j| }| j| || d D ]}||f}|| s$||f| jjks||f| jjkr||  d7  < qL| j|| | j| | j|< | j| | j	|< | j| dkr|| j|< | j| d | j|< |
| |
| d||<  qn| j| | j|< d| j|  | j|< | j	| | j| k r`| j|  d7  < |dk	r| j| | j| k rt| j| | j	| | j	|< | j| | j|< nP| j| | j| krt| j	| | j| | j	|< nt| j	| | j	| | j	|< ||  d7  < qLqdS )z=Orient the graph by DFS, compute lowpoints and nesting order.c                   S   s   dS Nr   r   r   r   r   r	   rJ     rK   z-LRPlanarity.dfs_orientation.<locals>.<lambda>c                   S   s   dS NFr   r   r   r   r	   rJ     rK   NrI   TrQ   )r   popr>   r@   r?   rN   r   r;   r(   r<   r\   r=   r5   )r#   r   	dfs_stackind	skip_initrP   rd   vwr   r   r	   r]     sD    

 



zLRPlanarity.dfs_orientationc                 C   s  | j | }| j| D ]z}||f| jjks||f| jjkr<q||f}| j|| | j| | j|< | j| | j|< | j| dkr|| j |< | j| d | j|< | | n| j| | j|< d| j|  | j	|< | j| | j| k r| j	|  d7  < |dk	r| j| | j| k r@t
| j| | j| | j|< | j| | j|< q| j| | j| krtt
| j| | j| | j|< qt
| j| | j| | j|< qdS )z-Recursive version of :meth:`dfs_orientation`.NrI   rQ   )r>   r   r?   rN   r   r;   r(   r<   re   r=   r5   )r#   r   rP   rd   ro   r   r   r	   re     s.    
 
z%LRPlanarity.dfs_orientation_recursivec           	      C   sJ  |g}t dd }t dd }|rF| }| j| }d}| j| || d D ]}||f}|| st| j| j|< || j| kr|| || d||< d} q.n"|| j|< | jt	t
||d | j| | j| k r|| j| d kr| j| | j|< n| ||s dS ||  d	7  < qP|s|dk	r| | qdS )
zTest for LR partition.c                   S   s   dS ri   r   r   r   r   r	   rJ     rK   z)LRPlanarity.dfs_testing.<locals>.<lambda>c                   S   s   dS rj   r   r   r   r   r	   rJ     rK   FNTr2   r   rI   )r   rk   r>   rA   r9   rD   rE   r\   rF   r/   r   r(   r;   add_constraintsremove_back_edges)	r#   r   rl   rm   rn   rP   Z
skip_finalrd   eir   r   r	   r_     s:    



zLRPlanarity.dfs_testingc                 C   s   | j | }| j| D ]}||f}t| j| j|< || j | krP| |sr dS n"|| j|< | jtt	||d | j
| | j| k r|| j| d kr| j| | j|< q| ||s dS q|dk	r| | dS )z)Recursive version of :meth:`dfs_testing`.Frp   r   NT)r>   rA   r9   rD   rE   rf   rF   r\   r/   r   r(   r;   rq   rr   )r#   r   rP   rd   rs   r   r   r	   rf   B  s"    



z!LRPlanarity.dfs_testing_recursivec                 C   s  t  }| j }|j s"|  |j s0dS | j|jj | j| kr|j r`|j	 |_n|jj
| j|jj< |jj|j_n| j| | j|jj< t| j| j| krqqt| jj|| st| jj|| rp| j }|j|| r|  |j|| r
dS |jj
| j|jj< |jjd k	r6|jj|j_|j rP|j	 |_n|jj
| j|jj< |jj|j_q|j r|j s| j| dS )NFT)r/   rD   rk   r1   r&   r4   r(   r2   r!   r'   r"   rB   rF   r9   rE   r*   r\   )r#   rs   rP   PQr   r   r	   rq   ]  sF    



 
zLRPlanarity.add_constraintsc                 C   s  |d }| j rNt| j | | j| krN| j  }|jjd k	rd| j|jj< q| j rT| j  }|jjd k	r|jjd |kr| j	|jj |j_q`|jjd kr|jjd k	r|j
j| j	|jj< d| j|jj< d |j_|j
jd k	r|j
jd |kr| j	|j
j |j
_q|j
jd krH|j
jd k	rH|jj| j	|j
j< d| j|j
j< d |j
_| j | | j| | j| k rt| j jj}t| j j
j}|d k	r|d ks| j| | j| kr|| j	|< n
|| j	|< d S )Nr   r7   rI   )rD   r9   r6   r;   rk   r1   r!   rC   r"   rB   r2   r\   r(   )r#   rP   r   rt   Zhlhrr   r   r	   rr     s4     

 *zLRPlanarity.remove_back_edgesc                 C   s   |g}t dd }|r| }| j| || d D ]}||  d7  < ||f}|| j| kr| j|| || j|< || j|< || ||  qq4| j	| dkr| j
||| j|  q4| j||| j|  || j|< q4qdS )zCompletes the embedding.c                   S   s   dS ri   r   r   r   r   r	   rJ     rK   z+LRPlanarity.dfs_embedding.<locals>.<lambda>NrI   )r   rk   rA   r>   r   add_half_edge_firstrG   rH   r\   rC   ra   add_half_edge_ccw)r#   r   rl   rm   rd   rs   r   r   r	   rb     s$    



zLRPlanarity.dfs_embeddingc                 C   s   | j | D ]}||f}|| j| krR| j|| || j|< || j|< | | q
| j| dkrx| j||| j|  q
| j	||| j|  || j|< q
dS )z+Recursive version of :meth:`dfs_embedding`.rI   N)
rA   r>   r   rw   rG   rH   rh   rC   ra   rx   )r#   r   rd   rs   r   r   r	   rh     s    

z#LRPlanarity.dfs_embedding_recursivec                 C   s   |g}t dd }|r~| }| j| dk	r`|| || j|  | j| ||< d| j|< q| j|  | j||  9  < q| j| S )z:Resolve the relative side of an edge to the absolute side.c                   S   s   d S r   r   r   r   r   r	   rJ     rK   z"LRPlanarity.sign.<locals>.<lambda>N)r   rk   rB   r\   rC   )r#   rP   rl   Zold_refr   r   r	   r`     s    
zLRPlanarity.signc                 C   sB   | j | dk	r8| j| | | j |  | j|< d| j |< | j| S )z"Recursive version of :meth:`sign`.N)rB   rC   rg   )r#   rP   r   r   r	   rg     s     
zLRPlanarity.sign_recursiveN)r+   r,   r-   r.   	__slots__r$   r   r   r]   re   r_   rf   rq   rr   rb   rh   r`   rg   r   r   r   r	   r      sB   (O/5!1,'r   c                   @   sj   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdddZdd ZdS )r   a  Represents a planar graph with its planar embedding.

    The planar embedding is given by a `combinatorial embedding
    <https://en.wikipedia.org/wiki/Graph_embedding#Combinatorial_embedding>`_.

    .. note:: `check_planarity` is the preferred way to check if a graph is planar.

    **Neighbor ordering:**

    In comparison to a usual graph structure, the embedding also stores the
    order of all neighbors for every vertex.
    The order of the neighbors can be given in clockwise (cw) direction or
    counterclockwise (ccw) direction. This order is stored as edge attributes
    in the underlying directed graph. For the edge (u, v) the edge attribute
    'cw' is set to the neighbor of u that follows immediately after v in
    clockwise direction.

    In order for a PlanarEmbedding to be valid it must fulfill multiple
    conditions. It is possible to check if these conditions are fulfilled with
    the method :meth:`check_structure`.
    The conditions are:

    * Edges must go in both directions (because the edge attributes differ)
    * Every edge must have a 'cw' and 'ccw' attribute which corresponds to a
      correct planar embedding.
    * A node with non zero degree must have a node attribute 'first_nbr'.

    As long as a PlanarEmbedding is invalid only the following methods should
    be called:

    * :meth:`add_half_edge_ccw`
    * :meth:`add_half_edge_cw`
    * :meth:`connect_components`
    * :meth:`add_half_edge_first`

    Even though the graph is a subclass of nx.DiGraph, it can still be used
    for algorithms that require undirected graphs, because the method
    :meth:`is_directed` is overridden. This is possible, because a valid
    PlanarGraph must have edges in both directions.

    **Half edges:**

    In methods like `add_half_edge_ccw` the term "half-edge" is used, which is
    a term that is used in `doubly connected edge lists
    <https://en.wikipedia.org/wiki/Doubly_connected_edge_list>`_. It is used
    to emphasize that the edge is only in one direction and there exists
    another half-edge in the opposite direction.
    While conventional edges always have two faces (including outer face) next
    to them, it is possible to assign each half-edge *exactly one* face.
    For a half-edge (u, v) that is orientated such that u is below v then the
    face that belongs to (u, v) is to the right of this half-edge.

    See Also
    --------
    is_planar :
        Preferred way to check if an existing graph is planar.

    check_planarity :
        A convenient way to create a `PlanarEmbedding`. If not planar,
        it returns a subgraph that shows this.

    Examples
    --------

    Create an embedding of a star graph (compare `nx.star_graph(3)`):

    >>> G = nx.PlanarEmbedding()
    >>> G.add_half_edge_cw(0, 1, None)
    >>> G.add_half_edge_cw(0, 2, 1)
    >>> G.add_half_edge_cw(0, 3, 2)
    >>> G.add_half_edge_cw(1, 0, None)
    >>> G.add_half_edge_cw(2, 0, None)
    >>> G.add_half_edge_cw(3, 0, None)

    Alternatively the same embedding can also be defined in counterclockwise
    orientation. The following results in exactly the same PlanarEmbedding:

    >>> G = nx.PlanarEmbedding()
    >>> G.add_half_edge_ccw(0, 1, None)
    >>> G.add_half_edge_ccw(0, 3, 1)
    >>> G.add_half_edge_ccw(0, 2, 3)
    >>> G.add_half_edge_ccw(1, 0, None)
    >>> G.add_half_edge_ccw(2, 0, None)
    >>> G.add_half_edge_ccw(3, 0, None)

    After creating a graph, it is possible to validate that the PlanarEmbedding
    object is correct:

    >>> G.check_structure()

    c                 C   s$   i }| D ]}t | |||< q|S )a  Converts the adjacency structure into a better readable structure.

        Returns
        -------
        embedding : dict
            A dict mapping all nodes to a list of neighbors sorted in
            clockwise order.

        See Also
        --------
        set_data

        )r   neighbors_cw_order)r#   r   r   r   r   r	   get_dataX  s    zPlanarEmbedding.get_datac                 C   s,   |D ]"}t || D ]}| || qqdS )a\  Inserts edges according to given sorted neighbor list.

        The input format is the same as the output format of get_data().

        Parameters
        ----------
        data : dict
            A dict mapping all nodes to a list of neighbors sorted in
            clockwise order.

        See Also
        --------
        get_data

        N)reversedrw   )r#   datar   rd   r   r   r	   set_datak  s    zPlanarEmbedding.set_datac                 c   s\   t | | dkrdS | j| d }|V  | | | d }||krX|V  | | | d }q8dS )zGenerator for the neighbors of v in clockwise order.

        Parameters
        ----------
        v : node

        Yields
        ------
        node

        r   N	first_nbrcw)lenrM   )r#   r   
start_nodeZcurrent_noder   r   r	   rz     s    z"PlanarEmbedding.neighbors_cw_orderc                 C   sB  | D ]}zt | |}W n8 tk
rR } zd| }t||W 5 d}~X Y nX t | | }||krvd}t|| | D ]}| ||s~d}t|q~qt  }t| D ]}t|dkrqt|}	d}
d}|D ]>}| |D ].}|
d7 }
||f|kr|d7 }| ||| qq|
d }|	| | dkrd}t|qdS )	aw  Runs without exceptions if this object is valid.

        Checks that the following properties are fulfilled:

        * Edges go in both directions (because the edge attributes differ).
        * Every edge has a 'cw' and 'ccw' attribute which corresponds to a
          correct planar embedding.
        * A node with a degree larger than 0 has a node attribute 'first_nbr'.

        Running this method verifies that the underlying Graph must be planar.

        Raises
        ------
        NetworkXException
            This exception is raised with a short explanation if the
            PlanarEmbedding is invalid.
        z5Bad embedding. Missing orientation for a neighbor of Nz3Bad embedding. Edge orientations not set correctly.z-Bad embedding. Opposite half-edge is missing.rI   r   rQ   z7Bad embedding. The graph does not match Euler's formula)	setrz   KeyErrorr   r   Zhas_edgeZconnected_componentsr   traverse_face)r#   r   Zsorted_nbrserrmsgZunsorted_nbrsrd   Zcounted_half_edges	componentZ	num_nodesZnum_half_edgesZ	num_facesZ	num_edgesr   r   r	   check_structure  s>    

zPlanarEmbedding.check_structurec                 C   s   |dkrD|  || || | | d< || | | d< || j| d< nB| | | d }| ||| || j| ddkr|| j| d< dS )a  Adds a half-edge from start_node to end_node.

        The half-edge is added counter clockwise next to the existing half-edge
        (start_node, reference_neighbor).

        Parameters
        ----------
        start_node : node
            Start node of inserted edge.
        end_node : node
            End node of inserted edge.
        reference_neighbor: node
            End node of reference edge.

        Raises
        ------
        NetworkXException
            If the reference_neighbor does not exist.

        See Also
        --------
        add_half_edge_cw
        connect_components
        add_half_edge_first

        Nr   ccwr   )r   rM   ra   get)r#   r   end_nodereference_neighborZccw_referencer   r   r	   rx     s    z!PlanarEmbedding.add_half_edge_ccwc                 C   s   |  || |dkrF|| | | d< || | | d< || j| d< dS || | kr\td| | | d }|| | | d< || | | d< || | | d< || | | d< dS )a~  Adds a half-edge from start_node to end_node.

        The half-edge is added clockwise next to the existing half-edge
        (start_node, reference_neighbor).

        Parameters
        ----------
        start_node : node
            Start node of inserted edge.
        end_node : node
            End node of inserted edge.
        reference_neighbor: node
            End node of reference edge.

        Raises
        ------
        NetworkXException
            If the reference_neighbor does not exist.

        See Also
        --------
        add_half_edge_ccw
        connect_components
        add_half_edge_first
        Nr   r   r   z2Cannot add edge. Reference neighbor does not exist)r   rM   r   r   )r#   r   r   r   Zcw_referencer   r   r	   ra     s    z PlanarEmbedding.add_half_edge_cwc                 C   s   |  || |  || dS )at  Adds half-edges for (v, w) and (w, v) at some position.

        This method should only be called if v and w are in different
        components, or it might break the embedding.
        This especially means that if `connect_components(v, w)`
        is called it is not allowed to call `connect_components(w, v)`
        afterwards. The neighbor orientations in both directions are
        all set correctly after the first call.

        Parameters
        ----------
        v : node
        w : node

        See Also
        --------
        add_half_edge_ccw
        add_half_edge_cw
        add_half_edge_first
        N)rw   )r#   r   rd   r   r   r	   connect_components)  s    z"PlanarEmbedding.connect_componentsc                 C   s<   || kr&d| j | kr&| j | d }nd}| ||| dS )a  The added half-edge is inserted at the first position in the order.

        Parameters
        ----------
        start_node : node
        end_node : node

        See Also
        --------
        add_half_edge_ccw
        add_half_edge_cw
        connect_components
        r   N)rM   rx   )r#   r   r   	referencer   r   r	   rw   A  s    z#PlanarEmbedding.add_half_edge_firstc                 C   s   | | | d }||fS )zReturns the following half-edge left of a face.

        Parameters
        ----------
        v : node
        w : node

        Returns
        -------
        half-edge : tuple
        r   r   )r#   r   rd   Znew_noder   r   r	   next_face_half_edgeU  s    z#PlanarEmbedding.next_face_half_edgeNc                 C   s   |dkrt  }|g}|||f |}|}| | | d }||ksJ||kr|| | ||\}}||f|krztd|||f q:|S )a  Returns nodes on the face that belong to the half-edge (v, w).

        The face that is traversed lies to the right of the half-edge (in an
        orientation where v is below w).

        Optionally it is possible to pass a set to which all encountered half
        edges are added. Before calling this method, this set must not include
        any half-edges that belong to the face.

        Parameters
        ----------
        v : node
            Start node of half-edge.
        w : node
            End node of half-edge.
        mark_half_edges: set, optional
            Set to which all encountered half-edges are added.

        Returns
        -------
        face : list
            A list of nodes that lie on this face.
        Nr   z&Bad planar embedding. Impossible face.)r   addr\   r   r   r   )r#   r   rd   Zmark_half_edgesZ
face_nodesZ	prev_nodeZcur_nodeZincoming_noder   r   r	   r   d  s    

zPlanarEmbedding.traverse_facec                 C   s   dS )zA valid PlanarEmbedding is undirected.

        All reverse edges are contained, i.e. for every existing
        half-edge (v, w) the half-edge in the opposite direction (w, v) is also
        contained.
        Fr   r%   r   r   r	   is_directed  s    zPlanarEmbedding.is_directed)N)r+   r,   r-   r.   r{   r~   rz   r   rx   ra   r   rw   r   r   r   r   r   r   r	   r     s   \;)0
+)F)F)collectionsr   Znetworkxr   __all__r   r   r   r   r   r   r/   r9   r   rO   r   r   r   r   r	   <module>   s    
 
K
&    