dag icon indicating copy to clipboard operation
dag copied to clipboard

Stable ordered ancestors/descendants

Open jeremybeard opened this issue 3 years ago • 3 comments

I'm looking to produce a list of the vertices of a DAG, where a vertex in the list is somewhere after all of its ancestors, but where the list doesn't change across calls. I can use getLeaves and getOrderedAncestors to fairly easily produce the list without walking the graph myself, but the list can change across calls because sibling vertices are not ordered.

Would a getStableOrderedAncestors and getStableOrderedDescendants make sense here? Analogous to sort.Sort and sort.Stable. The sibling vertices could perhaps be ordered by comparing IDs.

jeremybeard avatar Aug 19 '22 15:08 jeremybeard

Hey @jeremybeard, I think, that shouldn't be to hard. For (e.g.) GetOrderedAncestors() to return "stable" slices, one would only need to sort in:

  • https://github.com/heimdalr/dag/blob/d38d4f9b677a2d7337d75429e1b1014a7a670a6f/dag.go#L596 and
  • https://github.com/heimdalr/dag/blob/d38d4f9b677a2d7337d75429e1b1014a7a670a6f/dag.go#L606

I think.

seboghpub avatar Aug 25 '22 08:08 seboghpub

I took a quick stab (ignore the auto-formatting from my IDE) at it, and that looks right to me.

Is this something you'd like to be contributed? I tried to roll the change into walkAncestors with a new bool parameter, but the existing unstable iteration over vertices is done on a map, whereas the sorted vertices is a slice, so I'm not sure how to elegantly combine the two without a performance hit to the unstable case.

jeremybeard avatar Aug 25 '22 13:08 jeremybeard