简体   繁体   中英

Finding all the nodes between two certain vertices in a directed graph

I am looking for an algorithm to find all the nodes between two certain nodes of a directed graph. For example, the nodes between nodes "a" and "j" in the graph shown below are:

b c d e f g h i

PS The graph is directed and edges are upward (down to top).

在此处输入图片说明

You're looking for the set of nodes where the start node s can reach the node and that node can reach the destination node t. One way to do this is to do a DFS from s to find all nodes reachable from s and a reverse DFS from t to find all nodes that can reach t, then to take the intersection of those two sets. If you maintain the sets by storing mark bits in the nodes themselves, this runs in linear time.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM