Which algorithm to find the nearest node reachable from the other one by all the outoging paths

纵饮孤独 提交于 2019-12-24 08:47:32

问题


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:


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.



来源:https://stackoverflow.com/questions/2565863/which-algorithm-to-find-the-nearest-node-reachable-from-the-other-one-by-all-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!