Skip to main content

HypergraphZ - A Hypergraph Implementation in Zig

GitHub Actions Workflow Status PyPI

HypergraphZ is a directed hypergraph implementation in Zig (https://en.wikipedia.org/wiki/Hypergraph):

  • Each hyperedge can contain zero, one (unary) or multiple vertices.
  • Each hyperedge can contain vertices directed to themselves one or more times.

For Python users

How it works

The Python package is a thin binding over the same Zig core used by the native library — there is no Python reimplementation of the graph logic.

┌─────────────────────────────┐
│  Python (hypergraphz)       │  high-level API — Hypergraph, VertexQuery, …
├─────────────────────────────┤
│  ctypes                     │  FFI bridge — no C extension, no compilation step for users
├─────────────────────────────┤
│  C ABI (hgz_c_api.zig)      │  flat exported functions — hgz_add_vertex, hgz_find_shortest_path, …
├─────────────────────────────┤
│  HypergraphZ (Zig core)     │  all algorithms, memory management, type-safe generics
└─────────────────────────────┘

The shared library (libhypergraphz.so / .dylib / .dll) is compiled once and shipped inside the wheel. Data crosses the boundary as JSON (vertex and hyperedge payloads) and as plain integer ID arrays (all structural operations). For bulk insertion, create_vertices and create_hyperedges serialise an entire payload list in one call, reducing FFI crossings from N to 1 — see Performance below.

Installation

pip install hypergraphz
# or with uv
uv add hypergraphz

Pre-built wheels are available for Linux (x86_64, aarch64), macOS (x86_64, arm64), and Windows (x86_64).

Quick start

from hypergraphz import Hypergraph

g = Hypergraph()

# Add vertices and hyperedges
alice = g.create_vertex({"name": "alice", "age": 30})
bob   = g.create_vertex({"name": "bob",   "age": 25})
carol = g.create_vertex({"name": "carol", "age": 35})

collab = g.create_hyperedge({"type": "project"})
g.append_vertices(collab, [alice, bob, carol])

# Build the reverse index before querying
g.build()

# Shortest path
path = g.find_shortest_path(alice, carol)

# Fluent query builders
active = (
    g.vertices()
     .where(lambda d: d["age"] > 28)
     .data()
)

# Structural algorithms
scores, _, _ = g.compute_page_rank()
centrality    = g.compute_centrality()
components    = g.get_connected_components()

# Sub-graph operations return independent Hypergraph objects
sub  = g.get_vertex_induced_subhypergraph([alice, bob])
dual = g.get_dual()

API summary

Category Methods
Lifecycle build(), clear(), save(), load(), clone()
Vertices create_vertex(), get_vertex(), update_vertex(), delete_vertex(), count_vertices(), get_all_vertex_ids()
Hyperedges create_hyperedge(), get_hyperedge(), update_hyperedge(), delete_hyperedge(), count_hyperedges(), get_all_hyperedge_ids()
Relations append_vertices(), prepend_vertices(), insert_vertex(), insert_vertices(), delete_vertex_from_hyperedge(), delete_vertex_at_index()
Degree get_vertex_indegree(), get_vertex_outdegree()
Queries get_hyperedge_vertices(), get_vertex_hyperedges(), get_vertex_neighborhood(), get_intersections(), get_hyperedges_connecting(), get_endpoints(), get_orphan_vertices(), get_orphan_hyperedges()
Boolean is_connected(), is_reachable(), has_cycle(), is_k_uniform()
Traversal find_shortest_path(), find_all_paths(), bfs(), dfs(), random_walk()
Algorithms get_connected_components(), topological_sort(), find_cut_vertices(), compute_centrality(), compute_page_rank(), get_inclusions(), get_nestedness_profile()
Matrices to_incidence_matrix(), to_incidence_matrix_coo(), to_laplacian()
Sub-graphs get_dual(), get_k_skeleton(), get_vertex_induced_subhypergraph(), get_edge_induced_subhypergraph(), get_core(), expand_to_graph(), expand_to_star(), get_line_graph(), get_transitive_closure()
Fluent builders vertices() → VertexQuery, hyperedges() → HyperedgeQuery

Exceptions

All errors map to typed exceptions from hypergraphz:

Exception When raised
NotBuiltError Query requires build() first
VertexNotFoundError Vertex ID does not exist
HyperedgeNotFoundError Hyperedge ID does not exist
CycleDetectedError Operation requires a DAG
NoPathError No directed path exists
IndexOutOfBoundsError Insertion index out of range
NotEnoughVerticesError Operation needs more vertices

Performance

Benchmarks run with pytest-benchmark on an Intel Core i9-13900H, 64 GB RAM, CachyOS Linux (kernel 7.0.5), ReleaseFast shared library, Python 3.14.

Insertion — 1,000 hyperedges × 1,000 vertices

API Mean vs atomic
create_vertex per vertex (atomic) ~4,095 ms 1×
create_vertex per vertex + append_vertices (batch append) ~2,554 ms 1.6×
create_hyperedges + create_vertices (bulk, single FFI call each) ~415 ms ~10×

The dominant cost in the atomic and batch cases is ctypes call overhead (~1–2 µs per crossing). create_hyperedges and create_vertices each replace a per-item loop with a single FFI call, serialising the entire payload list as one JSON array.

Queries (chain / shared-vertex graphs, after build())

Operation Mean
find_shortest_path — chain of 1,000 vertices ~148 µs
find_cut_vertices — chain of 1,000 vertices ~351 µs
get_vertex_indegree × 100 — each vertex in 1,000 hyperedges ~4.7 ms

For Zig users

Zig version

HypergraphZ currently requires Zig 0.17.0-dev.242+5d55999d2.

Usage

Add hypergraphz as a dependency to your build.zig.zon:

zig fetch --save https://github.com/yamafaktory/hypergraphz/archive/v0.1.0.tar.gz

Add hypergraphz as a dependency to your build.zig:

const hypergraphz = b.dependency("hypergraphz", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("hypergraphz", hypergraphz.module("hypergraphz"));

Example

examples/coauthorship.zig models a small research community as a directed hypergraph where vertices are researchers and hyperedges are papers. The first listed author is treated as the corresponding author, giving each hyperedge a natural direction.

zig build example

It walks through seventeen features of the library in sequence:

Section API What it shows
Papers per researcher getVertexHyperedges reverse-index query
Research communities getConnectedComponents two isolated groups with no cross-group papers
Betweenness centrality computeCentrality which researchers bridge the most collaboration paths
Degrees of separation findShortestPath 1–3 hops within a community; unreachable across communities
Dual: paper-centric view getDual swap vertices ↔ hyperedges to see each researcher's papers
Shared authorship getIntersections researchers present on every paper in a given set
Seniority ordering topologicalSort hierarchy implied by the authorship direction
Pairwise expansion expandToGraph 6 papers encode 11 directed pairs in a regular graph
Spectral view toLaplacian normalized Laplacian; block-diagonal under disjoint groups
PageRank computePageRank citation-weighted stationary distribution; sums to 1
Random walk randomWalk seeded weighted walk; stays inside the start's component
Star expansion expandToStar bipartite (researcher ↔ paper) projection; 2-uniform
Line graph getLineGraph papers as vertices, linked when they share an author
K-core decomposition getCore (s, t)-core peeling; trims the lone-paper researcher
Nestedness getInclusions strict-subset pairs across hyperedges; sized profile
Co-author neighborhoods getVertexNeighborhood undirected co-occurrence neighbors across all hyperedges

Performance

Benchmarks run with zig build bench -Doptimize=ReleaseFast on an Intel Core i9-13900H, 64 GB RAM, CachyOS Linux (kernel 7.0.5).

Insertion — 1,000 hyperedges × 1,000 vertices

API Time
createVertex per vertex (atomic) 77.7 ms
createVertex per vertex + appendVerticesToHyperedge (batch append) 70.4 ms
createHyperedges + createVertices (bulk, single call each) 47.6 ms
build() — construct reverse index over all 1,000,000 vertices 227.8 ms

Queries (chain / shared-vertex graphs, after build())

Operation Time
getVertexIndegree × 100 — each vertex in 1,000 hyperedges 422 µs
findShortestPath — chain of 1,000 vertices 232 µs
findCutVertices — chain of 1,000 vertices 414 µs

Documentation

The latest online documentation can be found here.

Metadata

Release files for hypergraphz 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hypergraphz 0.1.6
File Size Uploaded Provenance Actions
hypergraphz-0.1.6.tar.gz 54.6 kB With provenance Details for hypergraphz-0.1.6.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for hypergraphz 0.1.6
File Actions
hypergraphz-0.1.6-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for hypergraphz-0.1.6-py3-none-win_amd64.whl
hypergraphz-0.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for hypergraphz-0.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
hypergraphz-0.1.6-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for hypergraphz-0.1.6-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
hypergraphz-0.1.6-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for hypergraphz-0.1.6-py3-none-macosx_11_0_arm64.whl

Total release size: 3.6 MB

Release files / hypergraphz-0.1.6.tar.gz

Download URL hypergraphz-0.1.6.tar.gz
Size 54.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4a0b2664babdb8f3b69d1ad43d2223a07380ec32cd3098dbe56ed914e4a2c804
BLAKE2b-256 checksum
How to use checksums
996a136c8041f61befd9b426a26f82487d8d08379858721e6c2b58d92912a174
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Release files / hypergraphz-0.1.6-py3-none-win_amd64.whl

Download URL hypergraphz-0.1.6-py3-none-win_amd64.whl
Size 15.1 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
0eaff899ecb24e29a5e5d83d2589b7d4092d172806cf501d7bc6d440c07bf5e1
BLAKE2b-256 checksum
How to use checksums
68de94c02f07fa4c5fbd256ab5dc8109fc7fa1e07d3b43ef841cb34547e7ac75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Release files / hypergraphz-0.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL hypergraphz-0.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.7 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
68e8effa8972c6283277f065317b33af1fd5bc512fbd5769977cef9381939ce8
BLAKE2b-256 checksum
How to use checksums
765e7bf4e5631d837f2760e541eb08a025232e4dd386e01b7dcdc6478732cdbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Release files / hypergraphz-0.1.6-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL hypergraphz-0.1.6-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.6 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
f878a90376a7df5cb26d931d543fde865f1aefed3301127e2e9663c53449c064
BLAKE2b-256 checksum
How to use checksums
8cad8f69abbb194e44f0d1b83126fbff57e8f93d4e23aa6b78c89469a5bcbf3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Release files / hypergraphz-0.1.6-py3-none-macosx_11_0_arm64.whl

Download URL hypergraphz-0.1.6-py3-none-macosx_11_0_arm64.whl
Size 310.4 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
24ea37889bd432f717505aadcc14cb5d515222161cf52190730eff232b9ea879
BLAKE2b-256 checksum
How to use checksums
e6937f7b8a3d443f3c6fbb661e47bd87cd297c15596e7ba1fb58e28429b10e71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Security for hypergraphz 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for hypergraphz-0.1.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.6 This release

5 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page