A minimal, Swift-native force-directed graph library optimized for interactive use cases with small graphs (≤500 nodes).
Documentation is here
Unlike standard graph libraries that use object-graph connections, Graphs uses a high-performance simulation engine built on cache-efficient data structures, making it ideal for fluid, real-time SwiftUI applications.
-
Switchable Connectivity: Manage multiple
EdgeSetcollections within a singlePopulationGraphto represent different connectivity patterns. - Performance-First Architecture: Utilizes a Structure-of-Arrays (SoA) layout for simulation states, ensuring CPU cache efficiency during high-frequency physics updates.
-
SwiftUI Integration: Native
@Observableand@MainActorsupport viaGraphSimulationfor seamless rendering inGraphView. -
Integer-Based Edges: Edges reference nodes by integer indices (source/target) for
$O(1)$ lookups, avoiding the overhead of object-reference traversal. - Per-Component Centering: Disconnected subgraphs each pull toward the global centroid independently, with inter-cluster repulsion to keep components from collapsing together.
- Off-Screen Indicator: A "Nodes off screen" badge fades in automatically when any node drifts outside the visible canvas area.
- JSON Graph Format (JGF): Import graphs from the standard JSON Graph Format, including an extended form with multiple named edge sets.
The package is organized into specialized modules to separate data modeling from physics calculations:
- Models: Core types like
Node,Edge, andPopulationGraph. - Simulation: Management of the
SimulationStateandGraphSimulationrunner. - Forces: Specialized functions for Many-Body, Edge, Center, and Collision physics.
- Views: SwiftUI components including
GraphViewandGraphInspectorView.
The library implements a classic force-directed layout using standalone force functions applied each tick in order:
-
Many-Body Force:
$O(N^2)$ N-body repulsion. -
Edge Force:
$O(E)$ spring forces along edges. - Component Center Force: per-component pull toward the global centroid, plus inverse-square repulsion between component centroids to prevent clusters from collapsing. Delegates to a simple center force for single-component graphs.
-
Collision Force:
$O(N^2)$ prevents node overlap.
Simulation energy is controlled by an alpha value (1.0 → 0.0). Call simulation.reheat(to:) to restart layout after graph mutations or drag interactions. The GraphInspectorView exposes sliders for repulsion, link strength, center pull, and component separation.
import Graphs
// 1. Initialize your graph
var graph = PopulationGraph()
let nodeA = graph.addNode(label: "Alpha")
let nodeB = graph.addNode(label: "Beta")
// 2. Connect nodes within an EdgeSet
graph.connect(nodeA, to: nodeB, distance: 30.0, weight: 1.0)
// 3. Start the simulation for your SwiftUI View
@State private var simulation = GraphSimulation(graph: graph)
var body: some View {
GraphView(simulation: simulation)
}DocC Documentation: All public APIs are fully documented for Swift DocC.
- Standalone Forces: Force calculations are implemented as standalone functions rather than methods to maintain a clean separation of concerns.
- Centralized Constants: Simulation and display defaults are managed in GraphConstants.
- File Formats: Supports the custom
.pgraphspecification viaGraphImporter(see the popgraph R library) and the standard JSON Graph Format (JGF) viaJGFImporter, including an extended multi-edge-set variant.
To update DocC content for GitHub, run the following and push the updates
swift package --allow-writing-to-directory ./docs \
generate-documentation --target Graphs \
--output-path ./docs \
--transform-for-static-hosting \
--hosting-base-path Graphs