generate_network_text#
- generate_network_text(graph, with_labels=True, sources=None, max_depth=None, ascii_only=False, vertical_chains=False)[source]#
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.
Each reachable node will be printed exactly once on it’s own line.
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:
- graphnx.DiGraph | nx.Graph
Graph to represent
- with_labelsbool | 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.
- sourcesList
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_depthint | None
The maximum depth to traverse before stopping. Defaults to None.
- ascii_onlyBoolean
If True only ASCII characters are used to construct the visualization
- vertical_chainsBoolean
If True, chains of nodes will be drawn vertically when possible.
- Yields:
- stra 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