top | item 41488738

Show HN: HypergraphZ – A Hypergraph Implementation in Zig

65 points| yamafaktory | 1 year ago |github.com

16 comments

order

samatman|1 year ago

I see that this is a second implementation, the first being in Rust: https://github.com/yamafaktory/hypergraph

I've found that Zig is an excellent tool for implementing data-structure-oriented libraries. Comptime genericity is simple to understand and use, providing a C interface is very easy, and libraries take an allocator, so any memory-safety issues are the consumer's problem. If you want to use it from a memory-safe language, well, all of those have C FFIs so far as I know, Rust very much included, so you can.

A hypergraph is clearly a data structure which demands a lot of cyclic references, no getting around that, so I'm curious: can you compare and contrast the experience of implementing this in Rust vs. Zig?

yamafaktory|1 year ago

Thanks for the feedback! Developing/prototyping is a very pleasant experience in Zig compared to Rust. It's obviously a different approach and some might find the tooling less mature but I quickly realized that the build system (as per 0.13.0) is very solid. You can build the docs, run the tests, etc. I'm a Neovim user and honestly the LSP (ZLS) is stable and you can additionally get the compilation errors (you need to create a check step in your build file). In tests, using with the testing allocator to catch memory leaks guides you while prototyping.

n42|1 year ago

This is a well put description of my experience when implementing the same data structures in both Rust and Zig. it probably would not be a good idea, but sometimes I wish I had some kind of 'inline Zig' macro in Rust to pull out when doing that type of work

klyrs|1 year ago

> A hypergraph is clearly a data structure which demands a lot of cyclic references...

Does it? The easiest data structure is a 2d array with rows corresponding to nodes and columns corresponding to edges. If nodes aren't allowed to touch an edge more than once, it's just a matrix of bools. No references needed!

reaanb2|1 year ago

How does this compare to a relational database, where hyperedges are represented by tables and vertices by values?

yamafaktory|1 year ago

Internally both the hyperedges and the vertices are stored as an AutoArrayHashmap with the associated data and the relations ( https://github.com/yamafaktory/hypergraphz/blob/9f87e705afd7...). Then the implementation diverge since a given hyperedge can hold zero to n vertices, with self-loops (ArrayList). For vertices, we just need to keep track of the hyperedges (AutoArrayHashmap).

bigtuna711|1 year ago

Really cool! I would be interested in seeing a hypergraph isomorphism algorithm implemented in Zig.

redbell|1 year ago

I don't know why, but I feel Zigergraph would be a meaningful name :)

yamafaktory|1 year ago

It was initially called HyperZig but after talking with some "ziguanas", using the exact word "hypergraph" in the same felt more accurate.