如何快速求解单入口节点下单个节点的支配集?
Great question! Since you're working with a tree (not a general directed graph), you can completely skip Tarjan's algorithm—there's a far simpler, more efficient approach tailored exactly to your single-node use case.
Why Trees Make This Easy
In a tree, there's exactly one unique path from the entry node s to any target node v. By definition, a node u is in the dominator set of v if every path from s to v passes through u. Since there's only one path here, the dominator set of v is exactly all nodes along this unique path from s to v (including s and v itself).
Step-by-Step Implementation
Here are two straightforward methods, both with time complexity way better than O(m log n):
Parent Pointer Traversal (Fastest Option)
If your tree is rooted atsand each node stores its direct parent pointer:- Start at your target node
v. - Traverse upwards by following parent pointers until you hit the entry node
s. - Collect every node you pass during this traversal—this is your dominator set.
- Time complexity: O(d), where
dis the depth ofv(length of the path fromstov). For most trees, this is drastically faster than Tarjan's algorithm.
- Start at your target node
DFS/BFS Path Finding
If you don't have parent pointers set up:- Run a standard DFS or BFS starting from
s, tracking the path to each node (use a stack or a parent-tracking array). - Once you reach
v, extract the full path fromstovthat you tracked. - This path is exactly the dominator set for
v. - Time complexity: O(n) in the worst case (e.g., a chain tree), but still way more efficient than building an entire dominator tree for a single node.
- Run a standard DFS or BFS starting from
Quick Note for General Directed Graphs
If you ever need to do this for a general directed graph (not a tree), there's still a lighter alternative to Tarjan's full dominator tree build:
- First find all nodes reachable from
s(call this setR), and all nodes that can reachv(call this setR'). - Your candidate dominators are the intersection
R ∩ R'—only these nodes could possibly dominatev. - For each candidate
u, check if removingudisconnectssfromv. If yes,uis in the dominator set.
This avoids the overhead of building a full dominator tree, and can be faster for single-node queries depending on the graph structure.
For your tree-specific scenario though, the path-traversal method is optimal—simple to code, and with minimal runtime cost.
内容的提问来源于stack exchange,提问作者user11235

