altgraph
altgraph is a foundational Python library for creating and manipulating directed and undirected graphs (networks). It provides a robust set of tools for graph data structures and basic algorithms, often used as a dependency in projects like PyInstaller. The current version is 0.17.5, with sporadic but active maintenance and releases typically a few times a year.
Warnings
- breaking Version 0.17.0 and newer dropped support for Python 2.x and older Python 3.x versions (3.3, 3.4, 3.5). If you are using these older Python interpreters, you must use altgraph < 0.17.
- gotcha When using `altgraph.Dot` for visualizing graphs, the Graphviz system package must be installed and accessible in your system's PATH. This is an external dependency, not a Python package.
- gotcha altgraph provides fundamental graph structures and basic algorithms. For advanced graph analytics, highly optimized performance on massive graphs, or a richer set of algorithms (e.g., shortest path, centrality), users might need to integrate with more specialized libraries like NetworkX or igraph.
Install
-
pip install altgraph
Imports
- Graph
from altgraph.Graph import Graph
- Dot
from altgraph.Dot import Dot
Quickstart
from altgraph.Graph import Graph
# Create a new graph
g = Graph()
# Add nodes
g.add_node("Start")
g.add_node("Mid")
g.add_node("End")
# Add edges
g.add_edge("Start", "Mid")
g.add_edge("Mid", "End")
g.add_edge("Start", "End", label="direct")
print(f"Nodes: {list(g.nodes())}")
print(f"Edges: {list(g.edges())}")
print(f"Successors of 'Start': {list(g.forw_nodes("Start"))}")
print(f"Predecessors of 'End': {list(g.back_nodes("End"))}")
# Example of iterating over nodes and edges
for node in g.nodes():
print(f"Node: {node}")
for head, tail, data in g.edges():
print(f"Edge: {head} -> {tail} (data: {data})")