问题
I'm looking for an efficient Union-Find (aka Disjoint Set) data structure for my directed graph, which has cycles and forests. Given such a graph G
, I want to to answer following queries:
- Can I reach node
v
fromu
? - From which nodes,
u
is not reachable?
For a single "source" node u
, I can run DFS search and answer whether I can reach v
from it. A single DFS search will have an upper-bound cost of m + n
in the worst-case, where m
and n
are the number of vertices and edges in the graph, respectively. However, I have many such "source" nodes and I'm wondering whether there's a better approach than running DFS separately from all m
"source" nodes (which has an upper-bound cost of m * (m + n)
in the worst case.)
It seems like both of these questions could be efficiently answered by an Union-Find data-structure for directed graph. To my surprise, I could not find any solution after significant online searches. Am I thinking the problem in wrong direction?
I've found this answer but could not comprehend it.
来源:https://stackoverflow.com/questions/44789738/union-find-disjoin-set-data-structure-for-directed-graph