简体   繁体   中英

Is DFS (Depth-first search) algorithm applied on an adjacency matrix the ideal solution to list all root-to-leaf paths of a graph?

I am creating a function in Ruby to list all the possible paths that a business process may be executed. Look at the diagram below that represents a sample process:

在此处输入图片说明

I want the output to be like:

[
  [[Task 1],[Task 2], [Task 4], [Task 6]],
  [[Task 1],[Task 3],[Task 5],[Task 6]]
]

So, I created a method to represent it as an adjacency matrix. It works just fine.

Now I want to work on an algorithm to extract all those paths. I was gonna implement the DFS, but I'm not so sure anymore if this is the right way to approach it since I'm not doing an actual search - I am only listing the paths.

The problem is np-hard because the number of possible paths between two nodes is exponential to the number of nodes since the example given is a DAG instead of a tree. so no way around having a worst-case exponential runtime. DFS/BFS would be the best way to go please refer to
Finding all paths between two nodes on a DAG
Enumerating all paths in a directed acyclic graph

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