ITADN
swift-primitives/swift-graph-primitives
swift-primitives/swift-graph-primitives · 文件
文件最后提交记录最后更新时间
README.md

Graph Primitives

Development Status CI

Graph.Sequential<Tag, Payload> — an immutable directed graph with sequentially-allocated nodes. Payloads live in a dense array where each node's identity is its index, so node lookup is O(1) and traversal is cache-friendly. You build a graph with a Builder — allocate nodes with payloads, then build() — and the result is immutable.

On top of the graph sits a rich traversal and analysis surface: depth-first and breadth-first traversal, topological ordering, strongly-connected components, cycle detection, forward and backward reachability, and dead-node analysis. Node identity is phantom-typed by Tag, so a node from one graph cannot be used against another.


Key Features

  • Dense, immutable, O(1) lookup — payloads in a contiguous array indexed by node identity.
  • Builder constructionallocate nodes, then build() an immutable graph.
  • Traversals — depth-first, breadth-first, topological.
  • Analyses — strongly-connected components, cycle detection, forward/backward reachability, dead nodes.
  • Tag-phantom-typed nodes — node identities of different graphs are distinct types.

Quick Start

import Graph_Primitives

enum Tag {}
var builder = Graph.Sequential<Tag, Int>.Builder()
let a = builder.allocate(10)
let b = builder.allocate(20)
let graph = builder.build()

// `graph` then exposes the traversal + analysis surface — depth/breadth-first,
// topological order, strongly-connected components, cycle detection, reachability,
// and dead-node analysis (each driven by an adjacency function over the payloads).

Installation

Add the dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/swift-primitives/swift-graph-primitives.git", branch: "main")
]

Add a product to your target:

.target(
    name: "App",
    dependencies: [
        .product(name: "Graph Primitives", package: "swift-graph-primitives")
    ]
)

The package is pre-1.0 — depend on branch: "main" until 0.1.0 is tagged. Requires Swift 6.3 and macOS 26 / iOS 26 / tvOS 26 / watchOS 26 / visionOS 26 (or the corresponding Linux / Windows toolchain).


Architecture

ProductContentsWhen to import
Graph PrimitivesUmbrella — the graph, its Builder, and all traversals/analysesMost consumers
Graph Sequential PrimitivesGraph.Sequential, its Builder, and the operation namespacesThe graph type + construction only
Graph Index / Adjacency / Traversal / Remappable PrimitivesNode identity / adjacency payload / traversal markers / node remappingA single foundational sub-namespace
Graph DFS / BFS / Topological PrimitivesDepth-first / breadth-first / topological traversalA single traversal
Graph SCC / Cycles / Reachable / Dead PrimitivesStrongly-connected components / cycles / reachability / dead-node analysisA single analysis

Platform Support

PlatformCIStatus
macOS 26YesFull support
LinuxYesFull support
WindowsYesFull support
iOS/tvOS/watchOSSupported
Swift EmbeddedPending (nightly-toolchain follow-up)


Community

License

Apache 2.0. See LICENSE.md.