{"id":5635,"library":"grandalf","title":"Grandalf: Graph and Drawing Algorithms Framework","description":"Grandalf is a pure Python package designed for experimenting with graph drawing algorithms. It currently implements two main layouts: the Sugiyama hierarchical layout and a force-driven (energy minimization) approach. Its primary function is to compute node (x,y) coordinates and route edges, providing the structural layout information without performing the actual graphical rendering, which is left to external graphics toolkits. The current version is 0.8, released in January 2023.","status":"maintenance","version":"0.8","language":"en","source_language":"en","source_url":"https://github.com/bdcht/grandalf","tags":["graph","drawing","algorithms","layout","visualization"],"install":[{"cmd":"pip install grandalf","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for core functionality, listed as install_requires.","package":"pyparsing"},{"reason":"Suggested for the directed-constrained layout, optional.","package":"numpy","optional":true},{"reason":"Suggested for importing graphs from Graphviz dot files, optional.","package":"ply","optional":true}],"imports":[{"note":"These are fundamental components for defining graphs.","symbol":"Vertex, Edge, Graph","correct":"from grandalf.graphs import Vertex, Edge, Graph"},{"note":"These are the main layout algorithms provided by Grandalf.","symbol":"SugiyamaLayout, ForceDirectedLayout","correct":"from grandalf.layouts import SugiyamaLayout, ForceDirectedLayout"},{"note":"Often used internally or for specific graph representations with layouts like Sugiyama.","symbol":"digraph_core","correct":"from grandalf.digraph import digraph_core"}],"quickstart":{"code":"from grandalf.graphs import Vertex, Edge, Graph\nfrom grandalf.layouts import SugiyamaLayout\n\n# Define nodes (vertices)\nV = [Vertex(data) for data in range(10)]\n\n# Define edges for a simple graph\nX = [(0, 1), (0, 2), (1, 3), (2, 3), (4, 5), (4, 6), (5, 7), (6, 7), (3, 8), (7, 9)]\nE = [Edge(V[u], V[v]) for u, v in X]\n\n# Create a graph instance\ng = Graph(V, E)\n\n# Assign placeholder dimensions (width, height) to vertices;\n# x,y will be set by the layout algorithm.\nfor v in V:\n    v.view = [0, 0, 20, 20] # [x, y, width, height]\n\n# Iterate through graph components (e.g., for Sugiyama, it processes connected components)\nfor gr in g.sugiyama():\n    # Instantiate the Sugiyama layout algorithm for the current component\n    lay = SugiyamaLayout(gr)\n\n    # Initialize and run the layout steps\n    lay.init_all()\n    lay.algo_graph_layered()\n    lay.algo_node_coordinatization()\n    lay.algo_edge_routing()\n\n    print(f\"--- Layout for Graph Component ---\")\n    for v in gr.V:\n        # After layout, v.view[0] and v.view[1] contain the calculated (x, y) coordinates\n        print(f\"  Vertex {v.data}: (x={v.view[0]}, y={v.view[1]})\")\n","lang":"python","description":"This quickstart demonstrates how to define a graph, assign initial dimensions to its vertices, and then apply the Sugiyama layout algorithm. After the layout is computed, each `Vertex` object's `view` attribute will be updated with calculated `(x, y)` coordinates, representing its position on the 2D plane. Grandalf only provides these coordinates; actual drawing requires integration with a separate graphics toolkit."},"warnings":[{"fix":"Be prepared to implement your own drawing logic using the (x, y) coordinates provided by `v.view[0]` and `v.view[1]` after running a layout algorithm.","message":"Grandalf focuses solely on computing graph layouts (node coordinates and edge routing). It does not provide any graphical rendering capabilities. Users must integrate it with their preferred graphics toolkit (e.g., Matplotlib, PyQt, Tkinter, etc.) to visualize the generated layouts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider performance implications for very large graphs or production systems requiring highly optimized drawing; evaluate alternatives if speed or advanced features are critical.","message":"The library is primarily for 'experimentations' and has a 'Development Status :: 3 - Alpha' on PyPI. While functional for graphs up to thousands of nodes, it may not be as fast or feature-rich as mature C++ libraries like Graphviz or OGDF. It's best suited for scenarios where a simple, pure-Python, and hackable graph layout solution is preferred.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Re-instantiate and re-run the layout algorithm (e.g., `SugiyamaLayout(new_gr)`) if the graph topology changes after an initial layout computation.","message":"Layout classes operate on a `graph_core` which is separate from the original `Graph`. If you modify the underlying graph (add/remove vertices/edges), you typically need to create a new layout instance to recompute the positions, as the layout objects do not automatically react to graph mutations.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}