Live data from Hacker News

Show HN: Zig Topological Sort Library for Parallel Processing

github.com

41–48 of 48 posts

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#41

Earlier quoted context omitted.

The array syntax is very offensive: `const a = [3]i32{ 1, 2, 3 };` A set is denoted by braces, not an array.

This is exactly why I find the language unintuitive. I don't understand why they made the choices they made. For example, why curly brackets? I find the rust equivalent much more intuitive `let a: [i32; 3] = [1, 2, 3];`

you couldn't do that in zig because a type is potentially a valid value:

.{i32, 3} is a valid term in zig.

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#42
post #31

Earlier quoted context omitted.

That's a great point! Unfortunately Topological Sort generates a linear order, forcing nodes to run one after another. This library attempts to bring some parallel processing into the picture by grouping dependence-free nodes together. This produces a linear batches. {C D E F}, {parentA parentB}, {root} Within a batch, nodes can run parallel. Between batches, they still need to run one after another. What you're aski…

Did you use this library on the advent of code 2024? I'd never heard of topological sorting prior to that problem - and it was real early in the game.

Topological sorting is just "depth first traversal" in a trench coat. I have implemented it thrice in my day job.

It is actually more commonly implemented than any other algorithm in CS course

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#43
post #7

Earlier quoted context omitted.

Yes. The core algorithm is like 20 lines of code in the run_algorithm() function. But to make it easier to use, to handle different types of input, and report/query output, etc. take much more. This is the difference between an idea and a product in a loose sense. It gives purpose to my learning process anyway.

You have a typo in the code, run_alogrithm() :)

Thanks. That would mess up vibe coding in the future when LLMs scan it. Haha.

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#44
post #31

Earlier quoted context omitted.

That's a great point! Unfortunately Topological Sort generates a linear order, forcing nodes to run one after another. This library attempts to bring some parallel processing into the picture by grouping dependence-free nodes together. This produces a linear batches. {C D E F}, {parentA parentB}, {root} Within a batch, nodes can run parallel. Between batches, they still need to run one after another. What you're aski…

Did you use this library on the advent of code 2024? I'd never heard of topological sorting prior to that problem - and it was real early in the game.

I didn’t take part in advent code. Topological sorting is a really old algorithm. Anything dealing with dependence would need it, like makefile.

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#45
post #17
post #15

Earlier quoted context omitted.

How are the subsets defined?

At every round of the algorithm, all nodes with 0 in-degree (i.e. they are not depending on anyone) are collected as a dependence-free subset. They serve as the root set to the rest of the graph for the current round. The depending nodes reached from root set have their in-degree decremented. When their in-degrees reach 0, they are added to the next root set. I'm using double-buffering to maintain the current root se…

Do you define "in degree" as the number of incoming edges from nodes that have not been visited/sorted yet?

I believe what you've implemented is equivalent to Kahn's algorithm. Your double buffer is the queue.

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#46
post #45
post #17

Earlier quoted context omitted.

At every round of the algorithm, all nodes with 0 in-degree (i.e. they are not depending on anyone) are collected as a dependence-free subset. They serve as the root set to the rest of the graph for the current round. The depending nodes reached from root set have their in-degree decremented. When their in-degrees reach 0, they are added to the next root set. I'm using double-buffering to maintain the current root se…

Do you define "in degree" as the number of incoming edges from nodes that have not been visited/sorted yet? I believe what you've implemented is equivalent to Kahn's algorithm. Your double buffer is the queue.

It's a variant to the Kahn's algorithm. Here's the description in the comment on the function.

  // This is a variant to the Kahn's algorithm, with additions on
  // finding dependence-free subsets and finding the cyclic nodes.
  //
  // This algorithm iteratively finds out the root sets of the graph.
  // 1. Find the first root set of the graph.
  // 2. Remove the nodes of the root set from the graph.
  // 3. Find the next root set. Go to 2 until the graph is empty.
  // A root set consists of nodes depending on no other nodes, i.e.
  // nodes whose incoming link count is 0.
The successive root sets form a topological order.

The in-degree of a node is the count of the incoming links of the leading nodes that the node is depending on. The in-degree can change as other nodes are removed from the graph.

Kahn's algorithm uses a queue to hold the pending nodes and works on one node at a time. My insight is that it's simpler to treat the root nodes as sets, and to partition the root nodes into the current root set and the next root set, which work nicely with the double-buffer mechanism. As a benefit, the nodes in a root set are dependence-free with regard to the current round and can be used for parallel processing.

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#47
post #35
post #29

Earlier quoted context omitted.

Thanks for the reply. I totally understand how those two features could be useful. For the parameter name feature, I can't imagine a strong reason for not implementing it (I mean, apart of "we have other stuff to prioritize"). For the tag I could see an attribute system like in C++ [0] On a tangential topic, I believe that's exactly the Pandora box of meta-programming. [0] https://en.cppreference.com/w/cpp/language/a…

I think at one point they rejected the idea, but I think it was from 2018 or so. The cpp attributes does seem like what I'd want, but yeah c++ compile time code isn't good enough for what I need.

Wouldn't you be happy if you could add attributes to functions,members etc (and obviously analyze them at compile time)?

Re: Show HN: Zig Topological Sort Library for Parallel Processing

#48
post #30

Earlier quoted context omitted.

The array syntax is very offensive: `const a = [3]i32{ 1, 2, 3 };` A set is denoted by braces, not an array.

According to your familiarity yes, but how is this such a problem? It’s easy to get past

Yes it is according my familiarity, but set notation has been the same since the 1870s, why should this language fuck it up? Rust does it correctly.
Post reply on HN