generate_network_text#
- generate_network_text(graph, with_labels=True, sources=None, max_depth=None, ascii_only=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 three 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.
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.
- 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
- Yields:
- stra line of generated text