On Sun, 2023-05-07 at 19:36 -0700, Carlos Sobrinho wrote:
> Yep, I tried doing something like that but the Remove was trickier so
> I went with another approach. I add all the nodes and edges info on a
> map[string]*node and set a dirty flag. I don't expose the graph so
> it's easy to just recalculate the orphan Nodes during the two ops:
> Sort (topo.SortStabilized) and Cluster (after sort is done, split the
> slice into [][]node in a way that they don't depend on each other, so
> second level depends on first, third on second, etc. Not sure if
> gonum has something for this but I couldn't find one).
>
> What I do is:
> - when a new node comes, add to the cache, set all the edges to the
> existing nodes and all the unknown edges to a single unknown (U)
> node. If any edge was added/changed, return true
> - keep doing this until no other node edge is changed.
> - recursively delete from the graph all nodes that have a link from
> unknown. The nodes are still on the map cache so they can be re-added
> if needed if dirty becomes true again.
>
> In your example, it wouldn't work when you have O1-N2-N3 because it
> would just add all the edges for N2 to N3 and the whole O1-N2-N3 link
> should not on the graph unless O1 becomes N.
>
> They're probably a few other ways of doing this but this seems to
> works very well for my needs: a ton of write 100-200 nodes, sort +
> calculate orphans and cluster.
> Thanks!
I think it would help if the overall intended behaviour were specified
here.
So what you seem to be wanting is a reachability criterion. Is that
correct? If it is, I'd not bother with removing any nodes and just
instead have a flag in nodes that distinguishes O from N (I take it
that in the example above it's G-O1-N2-N3 and not O1-N2-N3-G, where G
is the rest of the graph). Then you can use DFS from the starting node
(this presumably exists since it's a requirement for the rest to make
sense); here you DFS from the origin node, marking visited nodes and
accessible don't extend past O nodes. I'd make a derived graph that
considers nodes that are O or non-accessible to be non-existent for the
purposes of the graph.Graph API, this allows you to cache the work done
by DFS.
This encapsulates (what I think you are describing as) the behaviour.
Dan