views:

108

answers:

1

Hello,

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 block which "closes" the 'IF'.

+1  A: 

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.

Amber
Thanks! It sounds OK, I'll analyze it now. But I don't know if it would work for a "CASE" block where number of out-paths can be larger than nodes I will have to visit for each "parallel" group
ternyk
It is supoosed that my problem can be treated as finding the nearest dominator.
ternyk