a
    #©bb  ã                   @   s   d Z dgZddd„ZdS )z-
Text-based visual representations of graphs
Ú
forest_strTNFc                     sz  ddl }g }|du r|j}n|}|rNd}d}	d}
d}d}d}d}d	}d
}d}n(d}d}	d}
d}d}d}d}d}d}d}tˆ jƒdkr�||ƒ �nÒ| ˆ ¡s¤| d¡‚ˆ  ¡ }|r¶ˆ jnˆ j}|du rö|rÞ‡ fdd„ˆ jD ƒ}n‡ fdd„| 	ˆ ¡D ƒ}t|ƒd ‰‡fdd„t
|ƒD ƒddd… }tƒ ‰|�rb| ¡ \}}}|ˆv �rJ�q(ˆ |¡ |�s„|�rr||	 }|| }n||
 }|| }nX|�r´|�r¢|| }|| }n|| }|| }n(|�rÌ|| }|| }n|| }|| }|�röˆ j|  d|¡}n|}||t|ƒ ƒ ‡fdd„|| D ƒ}t
|ddd… dd�D ]&\}}|dk}|||f}| |¡ �q6�q(|du �rvd |¡S dS ) u£  
    Creates a nice utf8 representation of a directed forest

    Parameters
    ----------
    graph : nx.DiGraph | nx.Graph
        Graph to represent (must be a tree, forest, or the empty graph)

    with_labels : bool
        If True will use the "label" attribute of a node to display if it
        exists otherwise it will use the node value itself. Defaults to True.

    sources : List
        Mainly relevant for undirected forests, specifies which nodes to list
        first. If unspecified the root nodes of each tree will be used for
        directed forests; for undirected forests this defaults to the nodes
        with the smallest degree.

    write : callable
        Function to use to write to, if None new lines are appended to
        a list and returned. If set to the `print` function, lines will
        be written to stdout as they are generated. If specified,
        this function will return None. Defaults to None.

    ascii_only : Boolean
        If True only ASCII characters are used to construct the visualization

    Returns
    -------
    str | None :
        utf8 representation of the tree / forest

    Example
    -------
    >>> graph = nx.balanced_tree(r=2, h=3, create_using=nx.DiGraph)
    >>> print(nx.forest_str(graph))
    â•™â”€â”€ 0
        â”œâ”€â•¼ 1
        â”‚Â Â  â”œâ”€â•¼ 3
        â”‚Â Â  â”‚Â Â  â”œâ”€â•¼ 7
        â”‚Â Â  â”‚Â Â  â””â”€â•¼ 8
        â”‚Â Â  â””â”€â•¼ 4
        â”‚Â Â      â”œâ”€â•¼ 9
        â”‚Â Â      â””â”€â•¼ 10
        â””â”€â•¼ 2
            â”œâ”€â•¼ 5
            â”‚Â Â  â”œâ”€â•¼ 11
            â”‚Â Â  â””â”€â•¼ 12
            â””â”€â•¼ 6
                â”œâ”€â•¼ 13
                â””â”€â•¼ 14


    >>> graph = nx.balanced_tree(r=1, h=2, create_using=nx.Graph)
    >>> print(nx.forest_str(graph))
    â•™â”€â”€ 0
        â””â”€â”€ 1
            â””â”€â”€ 2

    >>> print(nx.forest_str(graph, ascii_only=True))
    +-- 0
        L-- 1
            L-- 2
    é    Nú+z+-- z    u   :Â Â  u   |Â Â  zL-> z|-> zL-- z|-- u   â•™u
   â•™â”€â”€ u
   â•Ÿâ”€â”€ u   â•ŽÂ Â  u   â”‚Â Â  u
   â””â”€â•¼ u
   â”œâ”€â•¼ u
   â””â”€â”€ u
   â”œâ”€â”€ z)input must be a forest or the empty graphc                    s   g | ]}ˆ j | d kr|‘qS )r   )Z	in_degree)Ú.0Ún©Úgraph© ú`/home/yuhw/wc/wire-cell-python-pr-11/pr11/lib/python3.9/site-packages/networkx/readwrite/text.pyÚ
<listcomp>|   ó    zforest_str.<locals>.<listcomp>c                    s    g | ]}t |‡ fd d„d�‘qS )c                    s
   ˆ j |  S )N)Zdegree)r   r   r   r	   Ú<lambda>€   r   z'forest_str.<locals>.<listcomp>.<lambda>)Úkey)Úmin)r   Úccr   r   r	   r
      s   ÿé   c                    s   g | ]\}}|d |ˆ kf‘qS )Ú r   )r   ÚidxÚnode)Úlast_idxr   r	   r
   ˆ   r   éÿÿÿÿÚlabelc                    s   g | ]}|ˆ vr|‘qS r   r   )r   Úchild)Úseenr   r	   r
   ¸   r   )ÚstartÚ
)ZnetworkxÚappendÚlenÚnodesZ	is_forestZNetworkXNotImplementedÚis_directedÚsuccZadjZconnected_componentsÚ	enumerateÚsetÚpopÚaddÚgetÚstrÚjoin) r   Zwith_labelsÚsourcesÚwriteZ
ascii_onlyZnxZprintbufÚ_writeZglyph_emptyZglyph_newtree_lastZglyph_newtree_midZglyph_endof_forestZglyph_within_forestZglyph_within_treeZglyph_directed_lastZglyph_directed_midZglyph_undirected_lastZglyph_undirected_midr   r   Ústackr   ÚindentZislastZthis_prefixZnext_prefixr   Úchildrenr   r   Zislast_nextZ	try_framer   )r   r   r   r	   r      s˜    A


þÿ








)TNNF)Ú__doc__Ú__all__r   r   r   r   r	   Ú<module>   s   