Which algorithm do you recommend to find out the nearest node which can be reached from the specific one by all the paths coming out the node. The graph is directed unweight.
I'm trying to analyze control flow diagram and when there is a 'IF' block I want to find the bloc开发者_JAVA百科k which "closes" the 'IF'.
Run a set of breadth-first searches in parallel, one from each out-path of the start node, and each time you examine a node increment its count by one. (Note: "parallel" here means that you should do all of the "distance = 1" evaluations for all searches first, then all of the "distance = 2", et cetera - this ensures that the result you find is the nearest along any path). Make sure you don't loop through cycles in the graph, if any exist.
Stop on the first node you increment to a count that is the same as the number of out-paths from the original node.
Running time is O(N+E) where N is the number of nodes and E is the number of edges downstream from the current node. Most searches will take less time due to early termination.
精彩评论