K iqdZddlZddlmZddlZddlmZddgZGddZ Gd d e Z Gd d e Z Gd de Z Gdde Z Gdde ZGdde Z ddZedd ddZdZdZy)z- Text-based visual representations of graphs N) defaultdict) open_filegenerate_network_textwrite_network_textceZdZedZy) BaseGlyphsct|Dcic]%}|jds|dk7r |t||'c}Scc}w)N_as_dict)dir startswithgetattr)clsas ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/readwrite/text.pyr zBaseGlyphs.as_dictsEX <<$i wsA    s*;N)__name__ __module__ __qualname__ classmethodr rrrs  rrcbeZdZUdZeed<dZeed<dZeed<dZeed<dZ eed <d Z eed <y ) AsciiBaseGlyphs+emptyz+-- newtree_last newtree_mid endof_forestz: within_forestz| within_treeN rrrrstr__annotations__rrrr r!rrrrrs>E3L#KL#M3KrrcFeZdZUdZeed<dZeed<dZeed<dZeed<y ) AsciiDirectedGlyphszL-> lastz|-> midz<-backedge! vertical_edgeN rrrr'r#r$r(r)r+rrrr&r&!s*D#CHcM3rr&cFeZdZUdZeed<dZeed<dZeed<dZeed<y ) AsciiUndirectedGlyphszL-- r'z|-- r(-r)|r+Nr,rrrr.r.(s*D#CHcM3rr.cbeZdZUdZeed<dZeed<dZeed<dZeed<d Z eed <d Z eed <y ) UtfBaseGlyphsu╙ru ╙── ru ╟── rrru╎ r u│ r!Nr"rrrr2r2/s@E3$L#$#K#L#!M3!Krr2cFeZdZUdZeed<dZeed<dZeed<dZeed<y ) UtfDirectedGlyphsu └─╼ r'u ├─╼ r(u╾r)u╽r+Nr,rrrr4r4;*D#CHcM3rr4cFeZdZUdZeed<dZeed<dZeed<dZeed<y ) UtfUndirectedGlyphsu └── r'u ├── r(u─r)u│r+Nr,rrrr7r7Br5rr7c #: (Kddlm(m}G(fdd|}d}|j} | r'|rtnt } |j } |j} n&|rtnt} |j} |j} t|tr|} n|rd} nd} |dk(r| jdzyt|jdk(r| jy| t!|}t|d z }t#|Dcgc]\}}|d|g||k(d c}}ddd }t%d }t'}|rI|j)\}}}}}|t*urs||v}|r ||xxd z cc<|rJ||rE|Cd }||t*||d }|j-|d }||||||}|j-||r|j/||sF|r#d }|| j0gz}|| j2gz}nk|| j4gz}|| j6gz}nJ|r|}|}nC|r!|| j8gz}|| j2gz}n || j:gz}|| j<gz}|t*urd}d}g}nb| )t|j|j?| |}n t|}| |j|j?|d } nd } | rtA| |}|h}!n| |D"cgc] }"|"|vs|" }}"h||}!|t||d z k(r |rt*g}|h}!| r |rt*g}|h}!| |D#cgc] }#|#|!vs|# }$}#|$r| FdjC|$D#cgc]*}#t|j|#j?| |#,c}#}%n(djC|$D#cgc] }#t|#c}#}%djCd| jD|%g}nd}|r!djC|| jFgzdjC|||gz|r5| rtt'|}&ntt'||hz }&|&d k(}'nd }'t#|ddd D]'\}}"|dk(}|||"|||'}|j-|)|rHyycc}}wcc}"wcc}#wcc}#wcc}#ww)ulGenerate 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 rAny NamedTuplecFeZdZUWed<Wed<eed<eed<eed<y))generate_network_text..StackFrameparentnodeindents this_islast this_verticalN)rrrr$listboolr:sr StackFramer=s   rrFcollapselabelNz ...Fcy)Nrrrrrz'generate_network_text.. srT,  )$typingr:r; is_directedr&r4succpredr.r7adj isinstancer#rlennodes _find_sources enumeratersetpopEllipsisappendaddrrrr r'r(r!getrCjoinr)r+))graph with_labelssources max_depth ascii_onlyvertical_chainsr;rF collapse_attrrQglyphsrRrS label_attrlast_idxidxr?stacknum_skipped_children seen_nodesr>r@rArBskip next_islast try_frame this_prefix next_prefixrHsuffixchildrenrGhandled_parentschildp other_parentsother_parents_labels num_childrennext_is_verticalr:s) @rrrIs)Z'ZM##%K(2$8Izzzz*4&:Myyyy+s#   A~llV## U[[ Q ll ?#E*Gw>> 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 │ │ └─╼ ... │ └─╼ ... └─╼ ... Nwrite)rbrcrdrerf)sysstdoutrhasattrcallable TypeErrortyper) rapathrbrcrdreendrf_writelines rrrs{J |!! w  $T ##% '  tczrcvjrttj}tj|}|j Dcic]}|g}}|j d}j D]}||}||j|g}|j D];}|j|dk(s||}t|fd} |j| =|StjD cgc]} t| fd}} t|fd}|Scc}wcc} w)zR Determine a minimal set of nodes such that the entire graph is reachable mappingrc"j|SN) in_degreenras rrLz_find_sources..zseooa.@r)keyc"j|Srdegreers rrLz_find_sources..s%,,q/rc"j|Srrrs rrLz_find_sources..s Qr) rQrCnxstrongly_connected_components condensationrWrar]rminconnected_componentssorted) rasccs scc_graphsnsupernode_to_nodesrrrcsccr?ccs ` rrXrXbsA  B44U;<OOE40 /8/@Ab"fAA//), -AB r " ) )! , -//# %B""2&!+(,3$@At$  % N--e4  1 2  &?@ N+B  s  D1D6c ()ddlm}ddlm(m}G(fdd|}t |}d}d}g} t |}|j||d} | tjtjdtjdhvrd}nH| tjtjdtjdhvrd}ntd | |r)tj!} t"j!} n(t$j!} t&j!} t)| j+} t)| j+} g}| D],}| }|Dcgc] }||vs| }}|r|j|.g}| D],}| }|Dcgc] }||vs| }}|r|j|.|D]C)|j)t-)fd |Drd}nt-)fd |DsAd}n|d}|r| n| }d |d zd z}|||}g}g}d}t/}||ddg}|D]))|dk(rd}|)vr)j1|\}}|j1dDcgc]}|j3}}|j5}|j7d d\} }!|!j3}!|j9|Dcgc]}||!fc}n%)j7d d\} }!|!j3}!|j;}"|!|dvr1||"j<|"j>d}#|j|#tA| }$||!|$d}%|"jBrnC|%j>|"j>kr*|j;}"|%j>|"j>kr*|!dk(r|j|"|j|"|j|%|j|%j<|"j<|us|j|"j<|%j<f |rtA|dk(sJ|rtDjFntDjH}&|&}'|'jK||'jM||'S#t$rYwxYwcc}wcc}wcc}wcc}w)aReconstructs 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)chainr9c6eZdZUWed<eed<edzed<y),_parse_network_text..ParseStackFramer?indentNhas_vertical_child)rrrr$intrEsrParseStackFramers  $J&rrNFTzUnexpected first character: c3&K|]}|v ywrr.0itemrs r z&_parse_network_text..sEtt|Ec3&K|]}|v ywrrrs rrz&_parse_network_text..sE$ErrOr)rJrrNrIr+z...)' itertoolsrrPr:r;iternextr]r2rrrrAssertionError StopIterationr&r r.r4r7rZvaluesanyobjectsplitstriprstriprsplitextendr[r?rrVrrDiGraphGraphadd_nodes_fromadd_edges_from)*linesrr;rinitial_line_iteris_asciirQ initial_lines first_line first_chardirected_glyphsundirected_glyphsdirected_itemsundirected_itemsunambiguous_directed_itemsr other_itemsotherother_supersetsunambiguous_undirected_itemsrhbackedge_symbolparsing_line_iteredgesrWis_emptynoparentrl node_part backedge_partubackedge_nodesprefixr?prev modified_prevrcurrrnewr:rs* @@r_parse_network_textrs& &'*' U HKMN+,  Z(]      % %a (  & &q )  H   ! !  ' ' *  ( ( +  H #? |!LM M-557199;+335/779 //12N,3356!#4& .9KUTU]5KK & - -d 3 4 $&  6$ .9KUTU]5KK ( / / 5 6 "T" E(DE EK  E*DE EK  +_1BFF:..4Om->? E EHxHXr4 0 1E!E5 6'? "H  d "'+zz/'B $I}1>1D1DT1JKAaggiKNK!((*I$++C3LFD::rs #$ "$8 9  j/O  J   -  Z(z  1c   xxv"JLr