
    wgq                     $   d Z ddlZddlZddlmZ ddlZddlmZ ddgZ	 G d d      Z
 G d	 d
e
      Z G d de      Z G d de      Z G d de
      Z G d de      Z G d de      Z	 	 	 	 	 ddZ edd      	 	 	 	 	 	 	 dd       Zd Zd Zy)z-
Text-based visual representations of graphs
    N)defaultdict)	open_filegenerate_network_textwrite_network_textc                       e Zd Zed        Zy)
BaseGlyphsc                     t        |       D ci c]%  }|j                  d      s|dk7  r|t        | |      ' c}S c c}w )N_as_dict)dir
startswithgetattr)clsas     \/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/readwrite/text.pyr   zBaseGlyphs.as_dict   sE     X
<<$i wsA
 	
 
s   *;N)__name__
__module____qualname__classmethodr        r   r   r      s    
 
r   r   c                   b    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   dZ	eed	<   d
Z
eed<   y)AsciiBaseGlyphs+emptyz+-- newtree_lastnewtree_mid    endof_forestz:   within_forestz|   within_treeNr   r   r   r   str__annotations__r   r   r   r    r!   r   r   r   r   r      s>    E3L#KL#M3Kr   r   c                   F    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   y	)
AsciiDirectedGlyphszL-> lastz|-> midz<-backedge!vertical_edgeN	r   r   r   r'   r#   r$   r(   r)   r+   r   r   r   r&   r&   "   s*    D#CHcM3r   r&   c                   F    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   y	)
AsciiUndirectedGlyphszL-- r'   z|-- r(   -r)   |r+   Nr,   r   r   r   r.   r.   )   s*    D#CHcM3r   r.   c                   b    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   d	Z	eed
<   dZ
eed<   y)UtfBaseGlyphsu   ╙r   u
   ╙── r   u
   ╟── r   r   r   u   ╎   r    u   │   r!   Nr"   r   r   r   r2   r2   0   s@     E3$L#$#K#L#!M3!Kr   r2   c                   F    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   y	)
UtfDirectedGlyphsu
   └─╼ r'   u
   ├─╼ r(   u   ╾r)   u   ╽r+   Nr,   r   r   r   r4   r4   <   *    D#CHcM3r   r4   c                   F    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   y	)
UtfUndirectedGlyphsu
   └── r'   u
   ├── r(   u   ─r)   u   │r+   Nr,   r   r   r   r7   r7   C   r5   r   r7   c              #   :	  (K   ddl m(m}  G (fdd|      }d}| j                         }	|	r'|rt        nt
        }
| j                  }| j                  }n&|rt        nt        }
| j                  }| j                  }t        |t              r|}n|rd}nd}|dk(  r|
j                  dz    yt        | j                        dk(  r|
j                   y|t!        |       }t        |      d	z
  }t#        |      D cg c]  \  }} |d|g ||k(  d
       c}}ddd   }t%        d       }t'               }|rI|j)                         \  }}}}}|t*        urs||v }|r||xx   d	z  cc<   |rJ||   rE|Cd} ||t*        ||d
      }|j-                  |       d
} ||||||      }|j-                  |       |r|j/                  |       |sF|r#d
}||
j0                  gz   }||
j2                  gz   }nk||
j4                  gz   }||
j6                  gz   }nJ|r|}|}nC|r!||
j8                  gz   }||
j2                  gz   }n ||
j:                  gz   }||
j<                  gz   }|t*        u rd}d}g }nb|)t        | j                  |   j?                  ||            }nt        |      }| | j                  |   j?                  |d
      } nd
} |	rtA        ||         }|h}!n||   D "cg c]	  }"|"|vs|" }}"h ||}!|t        |      |d	z
  k(  r|rt*        g}|h}!| r|rt*        g}|h}!||   D #cg c]	  }#|#|!vs|# }$}#|$r|FdjC                  |$D #cg c]*  }#t        | j                  |#   j?                  ||#            , c}#      }%n(djC                  |$D #cg c]  }#t        |#       c}#      }%djC                  d|
jD                  |%g      }nd}|r!djC                  ||
jF                  gz          djC                  |||gz          |r5|	rt        t'        |            }&nt        t'        |      |hz
        }&|&d	k(  }'nd
}'t#        |ddd         D ]'  \  }}"|dk(  } |||"|||'      }|j-                  |       ) |rHyyc c}}w c c}"w c c}#w c c}#w c c}#w w)ul  Generate lines in the "network text" format

    This works via a depth-first traversal of the graph and writing a line for
    each unique node encountered. Non-tree edges are written to the right of
    each node, and connection to a non-tree edge is indicated with an ellipsis.
    This representation works best when the input graph is a forest, but any
    graph can be represented.

    This notation is original to networkx, although it is simple enough that it
    may be known in existing literature. See #5602 for details. The procedure
    is summarized as follows:

    1. Given a set of source nodes (which can be specified, or automatically
    discovered via finding the (strongly) connected components and choosing one
    node with minimum degree from each), we traverse the graph in depth first
    order.

    2. Each reachable node will be printed exactly once on it's own line.

    3. Edges are indicated in one of four ways:

        a. a parent "L-style" connection on the upper left. This corresponds to
        a traversal in the directed DFS tree.

        b. a backref "<-style" connection shown directly on the right. For
        directed graphs, these are drawn for any incoming edges to a node that
        is not a parent edge. For undirected graphs, these are drawn for only
        the non-parent edges that have already been represented (The edges that
        have not been represented will be handled in the recursive case).

        c. a child "L-style" connection on the lower right. Drawing of the
        children are handled recursively.

        d. if ``vertical_chains`` is true, and a parent node only has one child
        a "vertical-style" edge is drawn between them.

    4. The children of each node (wrt the directed DFS tree) are drawn
    underneath and to the right of it. In the case that a child node has already
    been drawn the connection is replaced with an ellipsis ("...") to indicate
    that there is one or more connections represented elsewhere.

    5. If a maximum depth is specified, an edge to nodes past this maximum
    depth will be represented by an ellipsis.

    6. If a node has a truthy "collapse" value, then we do not traverse past
    that node.

    Parameters
    ----------
    graph : nx.DiGraph | nx.Graph
        Graph to represent

    with_labels : bool | str
        If True will use the "label" attribute of a node to display if it
        exists otherwise it will use the node value itself. If given as a
        string, then that attribute name will be used instead of "label".
        Defaults to True.

    sources : List
        Specifies which nodes to start traversal from. Note: nodes that are not
        reachable from one of these sources may not be shown. If unspecified,
        the minimal set of nodes needed to reach all others will be used.

    max_depth : int | None
        The maximum depth to traverse before stopping. Defaults to None.

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

    vertical_chains : Boolean
        If True, chains of nodes will be drawn vertically when possible.

    Yields
    ------
    str : a line of generated text

    Examples
    --------
    >>> graph = nx.path_graph(10)
    >>> graph.add_node("A")
    >>> graph.add_node("B")
    >>> graph.add_node("C")
    >>> graph.add_node("D")
    >>> graph.add_edge(9, "A")
    >>> graph.add_edge(9, "B")
    >>> graph.add_edge(9, "C")
    >>> graph.add_edge("C", "D")
    >>> graph.add_edge("C", "E")
    >>> graph.add_edge("C", "F")
    >>> nx.write_network_text(graph)
    ╙── 0
        └── 1
            └── 2
                └── 3
                    └── 4
                        └── 5
                            └── 6
                                └── 7
                                    └── 8
                                        └── 9
                                            ├── A
                                            ├── B
                                            └── C
                                                ├── D
                                                ├── E
                                                └── F
    >>> nx.write_network_text(graph, vertical_chains=True)
    ╙── 0
        │
        1
        │
        2
        │
        3
        │
        4
        │
        5
        │
        6
        │
        7
        │
        8
        │
        9
        ├── A
        ├── B
        └── C
            ├── D
            ├── E
            └── F
    r   )Any
NamedTuplec                   F    e Zd ZU W  ed<   W  ed<   eed<   eed<   eed<   y))generate_network_text.<locals>.StackFrameparentnodeindentsthis_islastthis_verticalN)r   r   r   r$   listboolr9   s   r   
StackFramer<      s    	r   rE   collapselabelNz ...   Fc                       y)Nr   r   r   r   r   <lambda>z'generate_network_text.<locals>.<lambda>
  s    r   T ,  )$typingr9   r:   is_directedr&   r4   succpredr.   r7   adj
isinstancer#   r   lennodes_find_sources	enumerater   setpopEllipsisappendaddr   r   r   r    r'   r(   r!   getrB   joinr)   r+   ))graphwith_labelssources	max_depth
ascii_onlyvertical_chainsr:   rE   collapse_attrrP   glyphsrQ   rR   
label_attrlast_idxidxr>   stacknum_skipped_children
seen_nodesr=   r?   r@   rA   skipnext_islast	try_framethis_prefixnext_prefixrG   suffixchildrenrF   handled_parentschildpother_parentsother_parents_labelsnum_childrennext_is_verticalr9   s)                                           @r   r   r   J   s)    Z 'Z  M##%K(2$8Izzzz*4&:Myyyy+s# 
	

A~llV##	U[[	Q	ll ?#E*G w<!# 'w/
T tT2x%@
 B$
  +95U
@E		=FD';8#z)(0A50 ,F38J&*$. (G[%%	 Y/ ',$."D';%	 Y/ t$ $)M")V-@-@,A"AK")V-@-@,A"AK")V-?-?,@"@K")V-A-A,B"BK !")K")K"&-&=&-1D1D0E&E&-&<&-1C1C0D&Dx)D 1 5 5j$ GHEIE !,${{4044]EJH$H
   $DJ/H (.hO
 ,0: "'j9P H   ':&9&&9O(S\Y]-J$,:'-hO$,:'-hO -1J Sq!?:R S S !-/3yy *7$% !$EKKN$6$6z1$E F0, 04yy-:;SV;0, !XXr6??<P&QRFF ggkV-A-A,BBCC''+788#&s8}#5L#&s8}x'?#@L $01#4 #(  (27 (
U!Qh&%k;K	 Y'(g 
| . !T <sV   C8R;RGR	R)R-<R)	R3R7R/R;RR$CR?RrH   wc                     |t         j                  j                  }n;t        |d      r|j                  }n"t	        |      r|}nt        t        |            t        | |||||      D ]  }	 ||	|z           y)u  Creates a nice text representation of a graph

    This works via a depth-first traversal of the graph and writing a line for
    each unique node encountered. Non-tree edges are written to the right of
    each node, and connection to a non-tree edge is indicated with an ellipsis.
    This representation works best when the input graph is a forest, but any
    graph can be represented.

    Parameters
    ----------
    graph : nx.DiGraph | nx.Graph
        Graph to represent

    path : string or file or callable or None
       Filename or file handle for data output.
       if a function, then it will be called for each generated line.
       if None, this will default to "sys.stdout.write"

    with_labels : bool | str
        If True will use the "label" attribute of a node to display if it
        exists otherwise it will use the node value itself. If given as a
        string, then that attribute name will be used instead of "label".
        Defaults to True.

    sources : List
        Specifies which nodes to start traversal from. Note: nodes that are not
        reachable from one of these sources may not be shown. If unspecified,
        the minimal set of nodes needed to reach all others will be used.

    max_depth : int | None
        The maximum depth to traverse before stopping. Defaults to None.

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

    end : string
        The line ending character

    vertical_chains : Boolean
        If True, chains of nodes will be drawn vertically when possible.

    Examples
    --------
    >>> graph = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
    >>> nx.write_network_text(graph)
    ╙── 0
        ├─╼ 1
        │   ├─╼ 3
        │   └─╼ 4
        └─╼ 2
            ├─╼ 5
            └─╼ 6

    >>> # A near tree with one non-tree edge
    >>> graph.add_edge(5, 1)
    >>> nx.write_network_text(graph)
    ╙── 0
        ├─╼ 1 ╾ 5
        │   ├─╼ 3
        │   └─╼ 4
        └─╼ 2
            ├─╼ 5
            │   └─╼  ...
            └─╼ 6

    >>> graph = nx.cycle_graph(5)
    >>> nx.write_network_text(graph)
    ╙── 0
        ├── 1
        │   └── 2
        │       └── 3
        │           └── 4 ─ 0
        └──  ...

    >>> graph = nx.cycle_graph(5, nx.DiGraph)
    >>> nx.write_network_text(graph, vertical_chains=True)
    ╙── 0 ╾ 4
        ╽
        1
        ╽
        2
        ╽
        3
        ╽
        4
        └─╼  ...

    >>> nx.write_network_text(graph, vertical_chains=True, ascii_only=True)
    +-- 0 <- 4
        !
        1
        !
        2
        !
        3
        !
        4
        L->  ...

    >>> graph = nx.generators.barbell_graph(4, 2)
    >>> nx.write_network_text(graph, vertical_chains=False)
    ╙── 4
        ├── 5
        │   └── 6
        │       ├── 7
        │       │   ├── 8 ─ 6
        │       │   │   └── 9 ─ 6, 7
        │       │   └──  ...
        │       └──  ...
        └── 3
            ├── 0
            │   ├── 1 ─ 3
            │   │   └── 2 ─ 0, 3
            │   └──  ...
            └──  ...
    >>> nx.write_network_text(graph, vertical_chains=True)
    ╙── 4
        ├── 5
        │   │
        │   6
        │   ├── 7
        │   │   ├── 8 ─ 6
        │   │   │   │
        │   │   │   9 ─ 6, 7
        │   │   └──  ...
        │   └──  ...
        └── 3
            ├── 0
            │   ├── 1 ─ 3
            │   │   │
            │   │   2 ─ 0, 3
            │   └──  ...
            └──  ...

    >>> graph = nx.complete_graph(5, create_using=nx.Graph)
    >>> nx.write_network_text(graph)
    ╙── 0
        ├── 1
        │   ├── 2 ─ 0
        │   │   ├── 3 ─ 0, 1
        │   │   │   └── 4 ─ 0, 1, 2
        │   │   └──  ...
        │   └──  ...
        └──  ...

    >>> graph = nx.complete_graph(3, create_using=nx.DiGraph)
    >>> nx.write_network_text(graph)
    ╙── 0 ╾ 1, 2
        ├─╼ 1 ╾ 2
        │   ├─╼ 2 ╾ 0
        │   │   └─╼  ...
        │   └─╼  ...
        └─╼  ...
    Nwrite)ra   rb   rc   rd   re   )sysstdoutr~   hasattrcallable	TypeErrortyper   )
r`   pathra   rb   rc   rd   endre   _writelines
             r   r   r     s{    J |!!	w		$T
##%'  	tczr   c                 v     j                         rt        t        j                               }t        j                   |      }|j                         D ci c]  }|g  }}|j                  d   } j
                  D ]  }||   }||   j                  |        g }|j                         D ];  }|j                  |   dk(  s||   }t        | fd      }	|j                  |	       = |S t        j                         D 
cg c]  }
t        |
 fd       }}
t        | fd      }|S c c}w c c}
w )zR
    Determine a minimal set of nodes such that the entire graph is reachable
    mappingr   c                 "    j                   |    S N)	in_degreenr`   s    r   rK   z_find_sources.<locals>.<lambda>{  s    eooa.@ r   )keyc                 "    j                   |    S r   degreer   s    r   rK   z_find_sources.<locals>.<lambda>  s    %,,q/ r   c                 "    j                   |    S r   r   r   s    r   rK   z_find_sources.<locals>.<lambda>  s    Q r   )rP   rB   nxstrongly_connected_componentscondensationrV   r`   r\   r   minconnected_componentssorted)r`   sccs	scc_graphsnsupernode_to_nodesr   r   rb   sccr>   ccs   `          r   rW   rW   c  sA    B44U;< OOE40	/8/@Ab"fAA //), 	-ABr"))!,	- //# 	%B""2&!+(,3$@At$		% N --e4
 12
 
 &?@N+ B 
s   
D1D6c                 	  )* ddl m} ddlm)m}m}  G )fdd|      }t        |       }d}d}g }	 t        |      }	|j                  |	       |	d   }
|
t        j                  t        j                  d   t        j                  d   hv rd}nH|
t        j                  t        j                  d   t        j                  d   hv rd}nt        d	|
       |r)t         j#                         }t$        j#                         }n(t&        j#                         }t(        j#                         }t+        |j-                               }t+        |j-                               }g }|D ],  }|}|D cg c]	  }||v s| }}|r|j                  |       . g }|D ],  }|}|D cg c]	  }||v s| }}|r|j                  |       . |D ]C  *|j                  *       t/        *fd
|D              rd} nt/        *fd|D              sAd} n |d}|r|n|}d|d   z   dz   } |||      }g }g }d}t1               } ||dd      g}|D ]  **|d   k(  rd}|*v r*j3                  |      \  }}|j3                  d      D cg c]  }|j5                          } }|j7                         }|j9                  dd      \  }!}"|"j5                         }"|j;                  | D cg c]  }||"f c}       n%*j9                  dd      \  }!}"|"j5                         }"|j=                         }#|"|d   v r1 ||#j>                  |#j@                  d      }$|j                  |$       tC        |!      }% ||"|%d      }&|#jD                  rnC|&j@                  |#j@                  k  r*|j=                         }#|&j@                  |#j@                  k  r*|"dk(  r|j                  |#       |j                  |#       |j                  |&       |j                  |&j>                         |#j>                  |us|j                  |#j>                  |&j>                  f       
 |rtC        |      dk(  sJ |rtF        jH                  ntF        jJ                  }' |'       }(|(jM                  |       |(jO                  |       |(S # t        $ r Y w xY wc c}w c c}w c c}w c c}w )a  Reconstructs a graph from a network text representation.

    This is mainly used for testing.  Network text is for display, not
    serialization, as such this cannot parse all network text representations
    because node labels can be ambiguous with the glyphs and indentation used
    to represent edge structure. Additionally, there is no way to determine if
    disconnected graphs were originally directed or undirected.

    Parameters
    ----------
    lines : list or iterator of strings
        Input data in network text format

    Returns
    -------
    G: NetworkX graph
        The graph corresponding to the lines in network text format.
    r   )chain)r9   r:   Unionc                   6    e Zd ZU W  ed<   eed<   edz  ed<   y),_parse_network_text.<locals>.ParseStackFramer>   indentNhas_vertical_child)r   r   r   r$   intrD   s   r   ParseStackFramer     s    	$J&r   r   NFTzUnexpected first character: c              3   &   K   | ]  }|v  
 y wr   r   .0itemr   s     r   	<genexpr>z&_parse_network_text.<locals>.<genexpr>  s     Ett|E   c              3   &   K   | ]  }|v  
 y wr   r   r   s     r   r   z&_parse_network_text.<locals>.<genexpr>  s     E$Er   rN   r)   rI   r   rM   rH   r+   z...)(	itertoolsr   rO   r9   r:   r   iternextr\   r2   r   r   r   r   AssertionErrorStopIterationr&   r   r.   r4   r7   rY   valuesanyobjectsplitstriprstriprsplitextendrZ   r>   r   rU   r   r   DiGraphGraphadd_nodes_fromadd_edges_from)+linesr   r:   r   r   initial_line_iteris_asciirP   initial_lines
first_line
first_chardirected_glyphsundirected_glyphsdirected_itemsundirected_itemsunambiguous_directed_itemsr   other_itemsotherother_supersetsunambiguous_undirected_itemsrg   backedge_symbolparsing_line_iteredgesrV   is_emptynoparentrk   	node_partbackedge_partubackedge_nodesprefixr>   prevmodified_prevr   currr   newr9   r   s+                                            @@r   _parse_network_textr     s   &  --'* '
 UHK MN+,
 	Z(]
%%a(&&q)
 

 H!!''*((+
 

 H #?
|!LMM-557199;+335/779 //12N,3356!# 4&.9KUTU]5KK&--d3	4
 $&   6$.9KUTU]5KK(//5	6 " T"E(DEEKE*DEEK  +_1BF F:..4O m->? EEHxH Xr401E! E56'?" Hd"'+zz/'B$I}1>1D1DT1JKAaggiKNK!((*I$++C3LFD::<DLL^<1d)<=  ;;sA.LFD::<Dyy{6/** ,		M
 LL'
 VtVT2""  ++,yy{ ++, 5= LL LLLL LL#yy(dii34KE5N 5zQ $"**C
%CuuJE  J L Lb L =s5   R- '	R=1R=	S$S=SS
-	R:9R:)TNNFF)NTNNF
F)__doc__r   warningscollectionsr   networkxr   networkx.utilsr   __all__r   r   r&   r.   r2   r4   r7   r   r   rW   r   r   r   r   <module>r      s      #  $"$8
9
 
j / O 	 J 	  -  Z(z
 1c 
x xv"JLr   