# Is it possible to efficiently maintain a directed graph where nodes unreachable from the root are deleted?

I have an infinite set of possible nodes $$V$$ and a "root node" $$r \in V$$. I would like to maintain a directed graph with the invariant that all nodes in the graph are reachable from $$r$$. That is, the graph starts with a set of nodes $$\{r\} \subset V$$ and arcs $$\varnothing \subset V^2$$, and the allowed operations are as follows:

1. Query the number of nodes reachable from $$r$$.
2. Create an arc from a node $$u$$ in the graph to a node $$v$$ not in the graph (if it helps, you can assume that $$v$$ has never been in the graph), adding $$v$$ to the graph in the process.
3. Create an arc from a node $$u$$ in the graph to a node $$v$$ also in the graph.
4. Delete an arc $$(u, v)$$ from the graph, in the process deleting all nodes and arcs now unreachable from $$r$$ (or, equivalently, doing nothing else, as long as the assumption on (2) holds about new nodes never having previously been in the graph, and if (1) can still be implemented efficiently).

An ideal solution would be constant-time for (1), (2), and (3); and for (4), the time complexity would be proportional to the number of nodes deleted. It is trivial to implement a data structure that is constant-time for (1), (2), and (3) with time complexity of (4) proportional to the number of all nodes in the graph at the time; I do not consider that a successful solution for this theoretical question.

I found Alexander Svozil's 2018 answer about dynamic connectivity, which cites (among others) this paper:

Decremental Strongly-Connected Components and Single-Source Reachability in Near-Linear Time - Aaron Bernstein, Maximilian Probst, Christian Wulff-Nilsen -https://arxiv.org/abs/1901.03615 - accepted at STOC 2019

This seems close to what I want, but it's only decremental and not fully dynamic as my setting is here. The cited papers about the fully dynamic setting seem to be just theoretical lower bounds and not actual algorithms; does this mean that this data structure cannot be made efficient? Crucially, though, my setting is slightly different from that framing of fully dynamic strongly connected-components and single-source reachability: in that problem, an arc could be deleted which affects reachability of many nodes, and then immediately re-added to make all those nodes reachable again. In my setting, those nodes are gone, and any new arcs must build up gradually rather than making many nodes reachable all at once. This seems to suggest to me that my setting could possibly be more similar to the decremental setting than the fully dynamic one, but I don't understand the details here nearly well enough to tell whether that's the case; hence my question.

For context, this is just a simplified version of the problem of garbage collection (GC): dynamically allocate/modify/deallocate objects that may point to other objects, and continuously free memory used by objects that become unreachable. I understand that garbage collection is a well-studied topic, but I'm having trouble finding the answer to my specific theoretical question here. For instance, Wikipedia describes the two most common GC strategies:

• Tracing implements arc deletion by periodically doing graph search from $$r$$, then deleting all nodes not found by the search. This visits all nodes in the graph, not just the deleted ones, so it seems that it could revisit many nodes over and over while not deleting them. This is similar the "trivial" solution I mentioned above, which I do not consider sufficiently performant (in a theoretical sense, not a practical sense).
• Reference counting doesn't fully solve this problem in the presence of cycles.

Thus, my question is: How can I efficiently implement this dynamic graph data structure? Or, where can I find a rigorous argument that it is not possible?

• If operation 4 takes time proportional to the number of deleted nodes, it needs to run in constant time when 0 nodes are deleted. This requires a data-structure that can detect whether an arc "disconnects" the graph with constant time insertion,query, and deletion. This may already be impossible. Commented Aug 28 at 11:57
• Just as an FYI, reference counting for cycle collection is totally a thing. See, for example: pages.cs.wisc.edu/~cymen/misc/interests/Bacon01Concurrent.pdf Commented Aug 28 at 13:34
• @Discretelizard for sure, and thanks for pointing that out! I realize my question is a bit vague; extra logarithmic factors on the time complexity would also be fine (although I'm guessing that would not really help, and it'd still be impossible regardless). Commented Aug 28 at 15:33