### Graphviz Style Attributes Examples Source: https://altgraph.readthedocs.io/en/latest/dot.html Provides examples of valid attributes for controlling graph layout and appearance. These attributes can be passed to the style(), node_style(), and edge_style() methods. ```text rankdir = 'LR' (draws the graph horizontally, left to right) ranksep = number (rank separation in inches) style = 'filled' | 'invisible' | 'diagonals' | 'rounded' shape = 'box' | 'ellipse' | 'circle' | 'point' | 'triangle' style = 'dashed' | 'dotted' | 'solid' | 'invis' | 'bold' arrowhead = 'box' | 'crow' | 'diamond' | 'dot' | 'inv' | 'none' | 'tee' | 'vee' weight = number (the larger the number the closer the nodes will be) ``` -------------------------------- ### Example Usage Source: https://altgraph.readthedocs.io/en/latest/dot.html Demonstrates a typical workflow for creating a graph, generating its dot representation, and then displaying or saving it. ```APIDOC ## Example Usage ### Description This example demonstrates a typical workflow for creating a graph using `altgraph.Graph`, generating its dot representation using `altgraph.Dot`, and then displaying or saving it. ### Code ```python from altgraph import Graph, Dot # create a graph edges = [ (1,2), (1,3), (3,4), (3,5), (4,5), (5,4) ] graph = Graph.Graph(edges) # create a dot representation of the graph dot = Dot.Dot(graph) # display the graph (requires graphviz to be installed and in PATH) dot.display() # save the dot representation into the mydot.dot file dot.save_dot(file_name='mydot.dot') # save dot file as gif image into the graph.gif file (requires graphviz) dot.save_img(file_name='graph', file_type='gif') ``` ``` -------------------------------- ### Basic Graph Creation and Dot File Generation Source: https://altgraph.readthedocs.io/en/latest/dot.html This snippet demonstrates the fundamental usage of the Dot module for creating a graph, generating its dot representation, and saving it to a file or image. Ensure graphviz is installed and accessible. ```python from altgraph import Graph, Dot # create a graph edges = [ (1,2), (1,3), (3,4), (3,5), (4,5), (5,4) ] graph = Graph.Graph(edges) # create a dot representation of the graph dot = Dot.Dot(graph) # display the graph dot.display() # save the dot representation into the mydot.dot file dot.save_dot(file_name='mydot.dot') # save dot file as gif image into the graph.gif file dot.save_img(file_name='graph', file_type='gif') ``` -------------------------------- ### Get Incoming Neighbors Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of all nodes connected by incoming edges to a specified node. ```python Graph.inc_nbrs(_node_) ``` -------------------------------- ### Forward Breadth-First Search Subgraph Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a subgraph containing nodes reachable via a breadth-first search from a start node, following outgoing edges. ```python Graph.forw_bfs_subgraph(_start_id_) ``` -------------------------------- ### Graph.back_bfs(_start_[, _end_]) Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of nodes starting at _start_ in some bread first search order (following incoming edges). When _end_ is specified iteration stops at that node. ```APIDOC ## Graph.back_bfs(_start_[, _end_]) ### Description Returns a list of nodes starting at _start_ in some bread first search order (following incoming edges). When _end_ is specified iteration stops at that node. ### Method Not specified (assumed to be a method call on a Graph object) ### Parameters #### Path Parameters - **_start_** (type unspecified) - Required - The starting node for the BFS. - **_end_** (type unspecified) - Optional - The node at which to stop the BFS. ``` -------------------------------- ### shortest_path Source: https://altgraph.readthedocs.io/en/latest/graphalgo.html Finds a single shortest path between a specified start node and end node. The input conventions are the same as the `dijkstra()` function, and the output is a list of nodes representing the shortest path. ```APIDOC ## shortest_path ### Description Find a single shortest path from the given start node to the given end node. The input has the same conventions as `dijkstra()`. ### Method `shortest_path(_graph_, _start_, _end_) ### Parameters * `_graph_`: The graph object. * `_start_`: The starting node. * `_end_`: The end node. ### Returns A list of the nodes in order along the shortest path. ``` -------------------------------- ### Graph.forw_bfs(_start_[, _end_]) Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of nodes starting at _start_ in some bread first search order (following outgoing edges). When _end_ is specified iteration stops at that node. ```APIDOC ## Graph.forw_bfs(_start_[, _end_]) ### Description Returns a list of nodes starting at _start_ in some bread first search order (following outgoing edges). When _end_ is specified iteration stops at that node. ### Method Not specified (assumed to be a method call on a Graph object) ### Parameters #### Path Parameters - **_start_** (type unspecified) - Required - The starting node for the BFS. - **_end_** (type unspecified) - Optional - The node at which to stop the BFS. ``` -------------------------------- ### Forward Breadth-First Search Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of nodes found through a breadth-first search starting from a specified node, following outgoing edges. The search can stop at an end node. ```python Graph.forw_bfs(_start_[, _end_]) ``` -------------------------------- ### Get All Neighbors Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of nodes connected by either incoming or outgoing edges to a specified node. ```python Graph.all_nbrs(_node_) ``` -------------------------------- ### Backward Breadth-First Search Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of nodes found through a breadth-first search starting from a specified node, following incoming edges. The search can stop at an end node. ```python Graph.back_bfs(_start_[, _end_]) ``` -------------------------------- ### Backward Breadth-First Search Subgraph Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a subgraph containing nodes reachable via a breadth-first search from a start node, following incoming edges. ```python Graph.back_bfs_subgraph(_start_id_) ``` -------------------------------- ### dijkstra Source: https://altgraph.readthedocs.io/en/latest/graphalgo.html Computes shortest paths from a start node to all nodes nearer than or equal to an optional end node using Dijkstra's algorithm. Assumes edge data represents edge length and handles positive and some negative edge lengths. ```APIDOC ## dijkstra ### Description Find shortest paths from the start node to all nodes nearer than or equal to the _end_ node. The edge data is assumed to be the edge length. Dijkstra’s algorithm is only guaranteed to work correctly when all edge lengths are positive. This code does not verify this property for all edges (only the edges examined until the end vertex is reached), but will correctly compute shortest paths even for some graphs with negative edges, and will raise an exception if it discovers that a negative edge has caused it to make a mistake. ### Method `dijkstra(_graph_, _start_[, _end_])` ### Parameters * `_graph_`: The graph object. * `_start_`: The starting node. * `_end_`: Optional. The end node. If provided, the search stops when this node is reached. ``` -------------------------------- ### Get Outgoing Neighbors Source: https://altgraph.readthedocs.io/en/latest/graph.html Returns a list of all nodes connected by outgoing edges from a specified node. ```python Graph.out_nbrs(_node_) ``` -------------------------------- ### Iterative Depth-First Search Source: https://altgraph.readthedocs.io/en/latest/graph.html Yields nodes in a depth-first traversal starting from a specified node. Traversal can stop at an end node and can be forward or backward. ```python Graph.iterdfs(_start_[, _end_[, _forward_]]) ``` -------------------------------- ### Iterative Depth-First Search with Data Source: https://altgraph.readthedocs.io/en/latest/graph.html Yields associated data for nodes in a depth-first traversal, starting from a specified node. It skips nodes without data or those failing a condition. ```python Graph.iterdata(_start_[, _end_[, _forward_[, _condition_]]]) ``` -------------------------------- ### Graph.iterdata(_start_[, _end_[, _forward_[, _condition_]]]) Source: https://altgraph.readthedocs.io/en/latest/graph.html Yield the associated data for nodes in a depth first traversal starting at the _start_ node. This method will not yield values for nodes without associated data. If _end_ is specified traversal stops when reaching that node. If _condition_ is specified and the condition callable returns False for the associated data this method will not yield the associated data and will not follow the edges for the node. If forward is True (the default) edges are traversed in forward direction, otherwise they are traversed in reverse direction. ```APIDOC ## Graph.iterdata(_start_[, _end_[, _forward_[, _condition_]]]) ### Description Yield the associated data for nodes in a depth first traversal starting at the _start_ node. This method will not yield values for nodes without associated data. If _end_ is specified traversal stops when reaching that node. If _condition_ is specified and the condition callable returns False for the associated data this method will not yield the associated data and will not follow the edges for the node. If forward is True (the default) edges are traversed in forward direction, otherwise they are traversed in reverse direction. ### Method Not specified (assumed to be a method call on a Graph object) ### Parameters #### Path Parameters - **_start_** (type unspecified) - Required - The starting node for the DFS. - **_end_** (type unspecified) - Optional - The node at which to stop the traversal. - **_forward_** (boolean) - Optional - If True (default), traverse edges in the forward direction. If False, traverse in reverse. - **_condition_** (callable) - Optional - A callable that takes associated data and returns False to stop yielding data and following edges for that node. ``` -------------------------------- ### Graph.iterdfs(_start_[, _end_[, _forward_]]) Source: https://altgraph.readthedocs.io/en/latest/graph.html Yield nodes in a depth first traversal starting at the _start_ node. If _end_ is specified traversal stops when reaching that node. If forward is True (the default) edges are traversed in forward direction, otherwise they are traversed in reverse direction. ```APIDOC ## Graph.iterdfs(_start_[, _end_[, _forward_]]) ### Description Yield nodes in a depth first traversal starting at the _start_ node. If _end_ is specified traversal stops when reaching that node. If forward is True (the default) edges are traversed in forward direction, otherwise they are traversed in reverse direction. ### Method Not specified (assumed to be a method call on a Graph object) ### Parameters #### Path Parameters - **_start_** (type unspecified) - Required - The starting node for the DFS. - **_end_** (type unspecified) - Optional - The node at which to stop the traversal. - **_forward_** (boolean) - Optional - If True (default), traverse edges in the forward direction. If False, traverse in reverse. ``` -------------------------------- ### Customizing Output Source: https://altgraph.readthedocs.io/en/latest/dot.html Illustrates how to customize the appearance of the overall graph, nodes, and edges using style methods. ```APIDOC ## Customizing the Output ### Description The graph drawing process can be customized by passing valid **dot** parameters for the nodes and edges. This section provides examples of customizing the overall graph, individual nodes, and specific edges. ### Examples ```python # customizing the way the overall graph is drawn dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75) # customizing node drawing dot.node_style(1, label='BASE_NODE',shape='box', color='blue' ) dot.node_style(2, style='filled', fillcolor='red') # customizing edge drawing dot.edge_style(1, 2, style='dotted') dot.edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90') dot.edge_style(4, 5, arrowsize=2, style='bold') ``` .. note:: `dotty` (invoked via :py:func:`~altgraph.Dot.display`) may not be able to display all graphics styles. To verify the output save it to an image file and look at it that way. ``` -------------------------------- ### Customizing Graph, Node, and Edge Styles Source: https://altgraph.readthedocs.io/en/latest/dot.html Shows how to apply custom styles to the overall graph, individual nodes, and specific edges using the style(), node_style(), and edge_style() methods. Refer to graphviz documentation for available attributes. ```python # customizing the way the overall graph is drawn dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75) # customizing node drawing dot.node_style(1, label='BASE_NODE',shape='box', color='blue' ) dot.node_style(2, style='filled', fillcolor='red') # customizing edge drawing dot.edge_style(1, 2, style='dotted') dot.edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90') dot.edge_style(4, 5, arrowsize=2, style='bold') ``` -------------------------------- ### Directed and Non-directed Graphs Source: https://altgraph.readthedocs.io/en/latest/dot.html Shows how to create both directed and non-directed graphs using the `graphtype` parameter. ```APIDOC ## Directed and Non-directed Graphs ### Description The `Dot` class can be used for both directed and non-directed graphs by passing the `graphtype` parameter during initialization. ### Example ```python # create directed graph (default) dot_directed = Dot.Dot(graph, graphtype="digraph") # create non-directed graph dot_non_directed = Dot.Dot(graph, graphtype="graph") ``` ``` -------------------------------- ### ObjectGraph.msgin() Source: https://altgraph.readthedocs.io/en/latest/objectgraph.html Prints an incoming debug message if debug output is enabled. ```APIDOC ## ObjectGraph.msgin() ### Description Prints an incoming debug message if debug output is enabled. ### Method `msgin(message)` ### Parameters #### Path Parameters - **message** (str) - Required - The incoming message to print. ``` -------------------------------- ### Creating Directed and Non-Directed Graphs Source: https://altgraph.readthedocs.io/en/latest/dot.html Illustrates how to specify whether the graph should be directed or non-directed using the 'graphtype' parameter during Dot object initialization. The default is 'digraph' (directed). ```python # create directed graph(default) dot = Dot.Dot(graph, graphtype="digraph") # create non-directed graph dot = Dot.Dot(graph, graphtype="graph") ``` -------------------------------- ### Graph Constructor Source: https://altgraph.readthedocs.io/en/latest/graph.html Constructs a new empty Graph object. Optionally, it can be initialized with a list of edges. ```APIDOC ## Graph Constructor ### Description Constructs a new empty `Graph` object. If the optional _edges_ parameter is supplied, updates the graph by adding the specified edges. All of the elements in _edges_ should be tuples with two or three elements. The first two elements of the tuple are the source and destination node of the edge, the optional third element is the edge data. The source and destination nodes are added to the graph when they aren’t already present. ### Method __init__ ### Parameters * **_edges_** (list of tuples) - Optional - A list of tuples, where each tuple represents an edge. Tuples can have two elements (source, destination) or three elements (source, destination, edge_data). ``` -------------------------------- ### Dot.display Source: https://altgraph.readthedocs.io/en/latest/dot.html Displays the current graph via dotty. Optionally processes the dot file with the 'neato' command before displaying. This method blocks until the dotty command exits. ```APIDOC ## Dot.display ### Description Displays the current graph via dotty. If the _mode_ is "neato", the dot file is processed with the neato command before displaying. This method will not return until the dotty command exits. ### Method (Not specified, likely a method call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python dot_instance.display(mode="neato") ``` ### Response #### Success Response (200) (No specific response details provided, implies execution completion) #### Response Example (No example provided) ``` -------------------------------- ### Graph.get_hops(_start_[, _end_[, _forward_]]) Source: https://altgraph.readthedocs.io/en/latest/graph.html Computes the hop distance to all nodes centered around a specified node. First order neighbours are at hop 1, their neigbours are at hop 2 etc. Uses `forw_bfs()` or `back_bfs()` depending on the value of the forward parameter. If the distance between all neighbouring nodes is 1 the hop number corresponds to the shortest distance between the nodes. ```APIDOC ## Graph.get_hops(_start_[, _end_[, _forward_]]) ### Description Computes the hop distance to all nodes centered around a specified node. First order neighbours are at hop 1, their neigbours are at hop 2 etc. Uses `forw_bfs()` or `back_bfs()` depending on the value of the forward parameter. If the distance between all neighbouring nodes is 1 the hop number corresponds to the shortest distance between the nodes. ### Method Not specified (assumed to be a method call on a Graph object) ### Parameters #### Path Parameters - **_start_** (type unspecified) - Required - The node to center the hop computation around. - **_end_** (type unspecified) - Optional - If specified, computation stops at this node. - **_forward_** (boolean) - Optional - If True, use `forw_bfs()`. If False, use `back_bfs()`. ``` -------------------------------- ### altgraph.GraphUtil.generate_random_graph Source: https://altgraph.readthedocs.io/en/latest/graphutil.html Generates a Graph instance with a specified number of nodes and edges, with options for self-loops and multi-edges. ```APIDOC ## altgraph.GraphUtil.generate_random_graph ### Description Generates and returns a `Graph` instance with _node_num_ nodes randomly connected by _edge_num_ edges. When _self_loops_ is present and True, there can be edges that point from a node to itself. When _multi_edge_ is present and True, there can be duplicate edges. This method raises `GraphError