问题
I'm wondering how to determine whether a directed graph has a get-stuck vertex, which is defined as a vertex with in-degree n-1 and out-degree 0.
I guess the dumb way would be to print the in-degree and out-degree of every vertex in the graph, but that's O(m+n). I'm interested in an O(n) algorithm. Any ideas?
Thanks!!
回答1:
(I assume that n is vertices and m is edges.)
Note that there is at most one get-stuck vertex in a graph.
Suppose we have an O(n) algorithm. If m is large, we must reach a conclusion without considering every edge.
If we conclude that the get-stuck vertex exists, we are asserting that no unconsidered edge leads out of it, which we can't know.
Therefore I don't think that O(n) is possible.
O(m) is pretty straightforward.
回答2:
I agree with Beta, there is no way you can solve this problem by o(n), because you have to visit each edge at least once to verify that it is a get-stuck node.
来源:https://stackoverflow.com/questions/22752583/graph-theory-algorithm-to-find-vertex-with-out-degree-0-and-in-degree-n-1